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

Jos*_*ers 2 javascript tampermonkey

这是一个 TamperMonkey 用户脚本。为什么不弹出“HELLO”?我在 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");
  });
Run Code Online (Sandbox Code Playgroud)

Pat*_*rts 7

用这个:

// ==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");
        });
    }
})();
Run Code Online (Sandbox Code Playgroud)

借用了如何检测 DOMContentLoaded 是否被触发。