2016-06-13 100 views
0

这是一个TamperMonkey userscript。为什么不打开“HELLO”?我在Ubuntu上运行Google Chrome。TamperMonkey userscript不会触发DOMContentLoaded事件

// ==UserScript== 
 
// @name   New Userscript 
 
// @namespace http://tampermonkey.net/ 
 
// @version  0.1 
 
// @description try to take over the world! 
 
// @author  You 
 
// @match  http://*/* 
 
// @match  https://*/* 
 
// @grant  none 
 
// ==/UserScript== 
 

 
window.addEventListener("DOMContentLoaded", function(event) { 
 
    alert("HELLO"); 
 
    });

+0

['document',not'window'](https://developer.mozilla.org/en-US/docs/Web/Events/DOMContentLoaded)。 –

+0

我刚试过,仍然无法使用。 –

+0

作为比较的基础,只是做'alert(“HELLO”);'工作。 –

回答

1

使用此:

// ==UserScript== 
// @name   New Userscript 
// @namespace http://tampermonkey.net/ 
// @version  0.1 
// @description try to take over the world! 
// @author  You 
// @match  http://*/* 
// @grant  none 
// ==/UserScript== 

(function() { 
    'use strict'; 

    if (document.readyState == "complete" || document.readyState == "loaded" || document.readyState == "interactive") { 
     alert("Already Loaded"); 
    } else { 
     document.addEventListener("DOMContentLoaded", function(event) { 
      alert("Just Loaded"); 
     }); 
    } 
})(); 

How to detect if DOMContentLoaded was fired借来的。

+0

Thanks,this作品。在TamperMonkey开始在页面上运行之前,DOMContentLoaded会触发问题吗? –

+0

@JoshuaMeyers是正确的,因为你应该能够从警报文本中推断出来。 –