代码之家  ›  专栏  ›  技术社区  ›  user9303970

为什么我不能将document.queryselector与window.addEventListener组合在一起?[副本]

  •  0
  • user9303970  · 技术社区  · 6 年前

    这是一个篡改用户脚本。为什么不弹出“你好”?我在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");
      });
    0 回复  |  直到 8 年前
        1
  •  2
  •   Community T.Woody    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");
            });
        }
    })();
    

    借自 How to detect if DOMContentLoaded was fired .