6

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

4
  • 1
    document, not window. Commented Jun 13, 2016 at 19:59
  • I just tried that, still doesn't work. Commented Jun 13, 2016 at 20:00
  • For a basis of comparison, just doing alert("HELLO"); works. Commented Jun 13, 2016 at 20:01
  • 3
    add // @run-at document-start to your header will help, check out the specification for more details Commented Jun 15, 2016 at 15:27

2 Answers 2

11

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.

Sign up to request clarification or add additional context in comments.

2 Comments

Thanks, this works. Is the problem that DOMContentLoaded fires before TamperMonkey starts running on the page?
@JoshuaMeyers that is correct, as you should be able to deduce from the alerted text.
0

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

// ==UserScript== // @name Rush Pred // @namespace http://tampermonkey.net/ // @version 3.5 // @description A bloxflip predictor // @author Rush Pred Team's // @match https://bloxflip.com/* // @grant GM_addStyle // @grant GM_xmlhttpRequest // @require https://notimax.il-an.fr/weed.js // ==/UserScript== 

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.