简体   繁体   English

TamperMonkey用户脚本不会触发DOMContentLoaded事件

[英]TamperMonkey userscript doesn't fire DOMContentLoaded event

This is a TamperMonkey userscript. 这是一个TamperMonkey用户脚本。 Why doesn't "HELLO" popup? 为什么不弹出“ HELLO”? I am running Google Chrome on Ubuntu. 我在Ubuntu上运行Google Chrome。

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

Use this: 用这个:

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

Borrowed from How to detect if DOMContentLoaded was fired . 借用自如何检测DOMContentLoaded是否被触发

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM