TamperMonkey 用户脚本不会触发 DOMContentLoaded 事件
TamperMonkey userscript doesn't fire DOMContentLoaded event
这是一个 TamperMonkey 用户脚本。为什么 "HELLO" 不弹出?我是 运行 Google Chrome Ubuntu。
// ==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");
});
使用这个:
// ==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。
这是一个 TamperMonkey 用户脚本。为什么 "HELLO" 不弹出?我是 运行 Google Chrome Ubuntu。
// ==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");
});
使用这个:
// ==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。