Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

TamperMonkey userscript doesn't fire DOMContentLoaded event

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");
  });
like image 455
Joshua Meyers Avatar asked Sep 09 '26 15:09

Joshua Meyers


1 Answers

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.

like image 161
Patrick Roberts Avatar answered Sep 11 '26 07:09

Patrick Roberts