Javascript 记录网页';使用userscript动态创建标记属性

Javascript 记录网页';使用userscript动态创建标记属性,javascript,dom,userscripts,Javascript,Dom,Userscripts,我能够使用Brock Adams的答案跟踪所有动态创建的标记 现在我想得到标签的属性。我尝试在lognewtagreations()中添加一个if条件,但没有成功。 在本例中,我正在检查脚本标记的属性: if(tagName=="script") { console.log("------------",elem.attributes.src.Value); } 请帮帮我。因为ssrc是在createElement()调用之外设置的,所以调整需要做的工作比这多一

我能够使用Brock Adams的答案跟踪所有动态创建的标记

现在我想得到标签的属性。我尝试在
lognewtagreations()
中添加一个if条件,但没有成功。
在本例中,我正在检查脚本标记的属性:

if(tagName=="script")
    {
        console.log("------------",elem.attributes.src.Value);
    }
请帮帮我。

因为
s
src
是在
createElement()
调用之外设置的,所以调整需要做的工作比这多一点。您必须基本上异步地检查
src
属性

一种方法是使用另一个轮询间隔。将其滚动到(以及一些内务管理),脚本代码变成:

//--- Intercept and log document.createElement().
function LogNewTagCreations () {
    var oldDocumentCreateElement    = document.createElement;

    document.createElement          = function (tagName) {
        var elem = oldDocumentCreateElement.apply (document, arguments);
        console.log ("Dynamically created a(n)", tagName, " tag.  Link: ", elem);

        if (tagName == "script") {
            GetScriptAttributes (elem);
        }

        return elem;
    }
}

function GetScriptAttributes (elem, tagNum, timerIntVar) {
    /*--- Because a <script>s src or text won't be set for some while, we need
        to poll for when they are added.
    */
    GetScriptAttributes.tagNum  = GetScriptAttributes.tagNum || 0;
    if ( ! tagNum) {
        GetScriptAttributes.tagNum++;
        tagNum = GetScriptAttributes.tagNum;
    }

    if (elem.src) {
        doneWaiting ();
        console.log (
            "Script tag", tagNum,
            " has a src attribute of:", elem.src
        );
    }
    else if (elem.textContent) {
        doneWaiting ();
        console.log (
            "Script tag", tagNum,
            " has a JS code of:", elem.textContent
        );
    }
    else {
        if ( ! timerIntVar) {
            var timerIntVar = setInterval (
                function () {
                    GetScriptAttributes (elem, tagNum, timerIntVar);
                },
                50
            );
        }
    }

    function doneWaiting () {
        if (timerIntVar) {
            clearInterval (timerIntVar);
        }
    }
}

/*--- The userscript or GM script will start running before the DOM is available.
    Therefore, we wait...
*/
var waitForDomInterval = setInterval (
    function () {
        var domPresentNode;
        if (typeof document.head == "undefined")
            domPresentNode = document.querySelector ("head, body");
        else
            domPresentNode = document.head;
        if (domPresentNode) {
            clearInterval (waitForDomInterval);
            addJS_Node (GetScriptAttributes.toString() );
            addJS_Node (null, null, LogNewTagCreations);
        }
    },
    1
);

//--- Handy injection function.
function addJS_Node (text, s_URL, funcToRun) {
    var D                                   = document;
    var scriptNode                          = D.createElement ('script');
    scriptNode.type                         = "text/javascript";
    if (text)       scriptNode.textContent  = text;
    if (s_URL)      scriptNode.src          = s_URL;
    if (funcToRun)  scriptNode.textContent  = '(' + funcToRun.toString() + ')()';

    var targ = D.getElementsByTagName ('head')[0] || D.body || D.documentElement;
    targ.appendChild (scriptNode);
}
/---截取并记录document.createElement()。
函数lognewtagreations(){
var oldDocumentCreateElement=document.createElement;
document.createElement=函数(标记名){
var elem=oldDocumentCreateElement.apply(文档、参数);
console.log(“动态创建一个(n)”,标记名,“tag.Link:”,elem);
如果(标记名=“脚本”){
GetScriptAttributes(elem);
}
返回元素;
}
}
函数GetScriptAttributes(elem、tagNum、timerIntVar){
/*---因为一段时间内不会设置src或text,所以我们需要
在添加它们时进行投票。
*/
GetScriptAttributes.tagNum=GetScriptAttributes.tagNum | | 0;
如果(!tagNum){
GetScriptAttributes.tagNum++;
tagNum=GetScriptAttributes.tagNum;
}
if(元素src){
doneWaiting();
console.log(
“脚本标记”,tagNum,
具有src属性:,elem.src
);
}
else if(elem.textContent){
doneWaiting();
console.log(
“脚本标记”,tagNum,
JS代码为:,elem.textContent
);
}
否则{
如果(!timerIntVar){
var timerIntVar=setInterval(
函数(){
GetScriptAttributes(elem、tagNum、timerIntVar);
},
50
);
}
}
函数doneWaiting(){
if(timerIntVar){
清除间隔(timerIntVar);
}
}
}
/*---userscript或GM脚本将在DOM可用之前开始运行。
因此,我们等待。。。
*/
var waitForDomInterval=setInterval(
函数(){
var-domPresentNode;
if(typeof document.head==“未定义”)
domPresentNode=document.querySelector(“head,body”);
其他的
domPresentNode=document.head;
if(domPresentNode){
clearInterval(waitForDomInterval);
addJS_节点(GetScriptAttributes.toString());
addJS_节点(null、null、LogNewTagCreations);
}
},
1.
);
//---方便的注射功能。
函数addJS_节点(文本、s_URL、函数名){
var D=文件;
var scriptNode=D.createElement('script');
scriptNode.type=“text/javascript”;
如果(text)scriptNode.textContent=text;
如果(s_URL)scriptNode.src=s_URL;
if(funcToRun)scriptNode.textContent='('+funcToRun.toString()+')()';
var targ=D.getElementsByTagName('head')[0]| | D.body | | D.documentElement;
target.appendChild(脚本节点);
}

您能解释一下代码吗?我在理解脚本标记的文本内容时遇到问题。动态添加的脚本有两种获取代码的方法
src
是一个,而
textContent
是另一个。发出
警报(“你好”)
,则
textContent
将是
警报(“Hello”)。至于代码,它与您另一个问题中的代码相同,但添加了
GetScriptAttributes()
。该函数是从我偶尔使用的函数修改而来的,对其进行全面解释可能超出了本文的范围。本质上,它为每个添加的脚本元素设置一个计时器。当每个
最终获得
src
或代码文本(可能在创建
之后很久),
GetScriptAttributes()
处理该脚本的结果并停止该脚本的计时器。您应该可以在控制台中看到它的报告(我测试了它,它工作正常)。我能够跟踪所有动态创建的标记,使用Brock Adams的回答“使用userscrip记录网页动态创建的标记属性”。现在我得到了script标记的src属性。我想做的是将所有src属性放在一个数组中,并在警报框中显示该数组,我喜欢下面的方式,这是一个新问题。把这个问题标为完整,我们可以回答下一个问题。但要注意,堆栈溢出既不是代码编写,也不是教程服务。