浏览器插件 - 通用注入模版JS

//TIP:先通过Tampermonkey编写为可用脚本,再套用此通用模版,再拖到Chrome安装为扩展即可。

/* 通用注入原型3:*/

switch (window.location.pathname)
{
    case "???":
        inject(YeScript.newFunc);
        break;
}
function inject(func)
{
    if (typeof (func) != 'function')
        return;
    YEJS = "//================== [YEJS START] =======================\n(";
    YEJS += func;
    YEJS += ")();";
    YEJS += "\n//================== [YEJS ENDED] =======================";
    script = document.createElement('script');
    script.type = 'text/javascript';
    script.id = 'YEJS';
    script.innerHTML = YEJS;
    var scriptTag = document.getElementById('YEJS');
    if (scriptTag) document.body.removeChild(scriptTag);
    document.body.appendChild(script);
}

/* 通用注入原型2:*/

var reallyJs = (function YeDoIt()
{
//通过将实际的待注入网页的脚本写到这里即可。
}.toString());

YEJS = "//================== [YEJS START] =======================\n";
YEJS += reallyJs;
YEJS += "\nYeDoIt();";
YEJS += "\n//================== [YEJS ENDED] =======================";
script = document.createElement('script');
script.id = 'YEJS';
script.type = 'text/javascript';
script.innerHTML = YEJS;
var scriptTag = document.getElementById('YEJS');
if (scriptTag) document.body.removeChild(scriptTag);
document.body.appendChild(script);

  

/* 通用注入原型1:*/

var YEJS="\
//================== [YEJS START] =======================\n\
将要注入的JS:
1.需要替换所有[双引号]为[单引号] 或\"引号!
2.需要替换所有\n为\\n\\\n
3.不留空行或空行也要用\n\符号代替!
4.将正则表达式中的\d\w等改为\\d\\w才有效!
\n//================== [YEJS ENDED] =======================";

//======================================================
// 直接注入JS到网页中,可自由操纵原网页的任何脚本!
//======================================================
script = document.createElement('script');
script.type = 'text/javascript';
script.id = 'YEJS';
script.innerHTML = YEJS;
var scriptTag = document.getElementById('YEJS');
if (scriptTag) document.body.removeChild(scriptTag);
document.body.appendChild(script);
//======================================================
// 插件的JS与原网页的JS唯一的交流通道为网页DOM树!
//======================================================

  

转载于:https://www.cnblogs.com/AsionTang/p/3442081.html

猜你喜欢

转载自blog.csdn.net/weixin_34009794/article/details/93270801