TamperMonkey userscript doesn't fire DOMContentLoaded event

自古美人都是妖i 提交于 2019-12-12 03:09:53

问题


This is a TamperMonkey userscript. Why doesn't "HELLO" popup? I am running Google Chrome on 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");
  });

回答1:


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.



来源:https://stackoverflow.com/questions/37798132/tampermonkey-userscript-doesnt-fire-domcontentloaded-event

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!