1

この投稿のとおり、動的に作成されたWebページのタグ属性をユーザースクリプトでログに記録します

属性を取得し、次のように提案された配列も含めました

addJS_Node ("var arr=[];");

エラーが発生しなかったため、src属性をarr配列に正常にプッシュしました。

今、私は配列を表示する場所が混乱しています。

私のコードは次のとおりです

// ==UserScript==
// @name        Tags Monitoring
// @namespace   PC
// @run-at      document-start
// ==/UserScript==
//--- Intercept and log document.createElement().tagattributes
addJS_Node ("var arr=[];"); 
function LogNewTagCreations ()
{   
   var oldDocumentCreateElement = document.createElement;
  document.createElement       = function (tagName) 
  {
      var elem = oldDocumentCreateElement.apply (document, arguments);  
      if(tagName=='script')      
         GetScriptAttributes (elem); 
     return elem;
  }   
}
function GetScriptAttributes (elem, tagNum, timerIntVar) 
{
   /*--- Because the tags 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;
   }   
   /*-- Getting the required attributes */
   if (elem.src) 
   {
      doneWaiting ();
      arr.push(elem.src);
        console.log (
        tagNum," has a src attribute of:", elem.src,"=========",arr.length
        );
   }    
   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  
);

 addJS_Node("document.onreadystatechange = function (){if (document.readyState ==       'complete'){console.log(arr.length);}}");

//--- 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);    
}

動的に作成されたタグの数を出力したいので、配列を追加しました。最初にスクリプトタグのみを試してみましたが、配列が完成したことを確認して、配列の長さを印刷できるようにするにはどうすればよいですか?

4

1 に答える 1

0

あなたの問題はあなたのコードではないようです。
あなたはJavaScriptをまったく知らないだけです。

簡単なコードとチュートリアルから始めることを強くお勧めします。
最初のスクリプトをコーディングしてみてください。コード
について疑問がある場合は、お気軽に質問してください。

Brock Adams から提供されたコードは完全に機能しています。
それをGreasemonkeyにコピペしただけです。
出力は次のとおりです。

ここに画像の説明を入力

そして、ウェブページの動的に作成されたタグ属性をユーザースクリプトでログに記録するに戻り、彼の答えを正しいものとしてマークしてください。

于 2012-04-23T12:38:50.547 に答える