Rectangle 27 21

If alerts() are not firing, chances are you may have clicked Firefox's Prevent this page from creating additional dialogs option, or set a browser preference (older versions of Firefox), or Firefox may have become unstable in memory.

(With one step added for problems with alert().)

First make sure that the script is even firing for the page in question.

click on the down-triangle next to the Greasemonkey icon

Here's a screenshot showing how both messages and errors appear in the Browser Console -- caused by both the web page and the Greasemonkey script:

about:config
capability.policy.default.Window.alert

Completely clear the browser cache.

  • Shutdown Firefox completely. Use Task Manager, or equivalent, to verify that there is no Firefox thread/task/process in memory.

Install the Greasemonkey script afresh.

Please supply your versions of three things: (1) The OS, (2) Firefox, (3) Greasemonkey or Tampermonkey or Scriptish, etc.

@include * means that the script will fire for every page! This is almost always a poor practice. (There are some exceptions, but your case is not one.)

@namespace does not control where the page runs.

@namespace
alert()
console.log()

see the results, and helpful error messages (hint, hint) on the Browser Console.

www.google.com
//@includehttps://google.com
// @match  *://www.google.com/*

as a starting point.

In Firefox Greasemonkey, you can also use the magic .tld to support most of Google's international domains, like so:

// @include  http://www.google.tld/*
// @include  https://www.google.tld/*

Use both lines. Note that this does not perform as well as the @match line does. So, if you only care about one nation/locale, just use @match.

  • Install this script: // ==UserScript== // @name Google Hello // @namespace John Galt // @description Basic Google Hello // @match *://www.google.com/* // @version 1 // @grant none // ==/UserScript== console.log ("Hi Google!");

Visit Google and note the results on Firefox's Browser Console.

  • If there is still a problem, follow all of the debug steps above.

supply ALL of the following:

  • The relevant errors and messages you get on the Browser Console.
  • The exact code and steps needed to duplicate the problem. Make an MCVE for this!
  • A short summary of what you have tried to solve the problem.

javascript - My very simple Greasemonkey script is not running? - Stac...

javascript debugging firefox greasemonkey
Rectangle 27 23

EDIT: This post was created prior to the implementation of the @run-at key in Greasemonkey - as noted by Blaise, from Greasemonkey 0.9.8 you may now have the script executed as soon as the page begins to load.

@run-at document-start is described in the wiki as follows:

Start is new as of version 0.9.8. The script will run before any document begins loading, thus before any scripts run or images load.

Note that this means having the script run (potentially) prior to the creation of the DOM and may lead to some inconsistent/unusual behaviour. You will need (a little) more than just this one-line drop-in.

@run-at document-start

That bug has been fixed and you can now run scripts before the page loads by adding this to the meta section: @run-at document-start

Cheers Blaise - I have updated my answer to reflect the changes in Greasemonkey since first posting the answer. I am not sure how well @run-at document-start works with the script compiler as I have not been involved with Greasemonkey for quite a while now.

javascript - how to run greasemonkey script before the page content is...

javascript firefox firefox-addon greasemonkey
Rectangle 27 11

Functions and variables declared in your GreaseMonkey scripts (and Chrome's user scripts) are kept separate from the ones declared by the web page, for obvious reasons. For GM scripts in Firefox, you can access global variables via unsafeWindow.

The best approach for safety and compatibility is to inject your functions into the page using a script element.I use the following snippet in my user scripts:

function addFunction(func, exec) {
  var script = document.createElement("script");
  script.textContent = "-" + func + (exec ? "()" : "");
  document.body.appendChild(script);
}

The "-" here makes certain the function is parsed as an expression so that exec can be used to immediately execute upon adding it.You call the function like so:

function myFunction () {
    return Grooveshark.playNextSong();
}

// Inject the function and execute it:
addFunction(myFunction, true);

google chrome extension - UserScripts & Greasemonkey: calling a websit...

javascript google-chrome-extension greasemonkey userscripts
Rectangle 27 11

Functions and variables declared in your GreaseMonkey scripts (and Chrome's user scripts) are kept separate from the ones declared by the web page, for obvious reasons. For GM scripts in Firefox, you can access global variables via unsafeWindow.

The best approach for safety and compatibility is to inject your functions into the page using a script element.I use the following snippet in my user scripts:

function addFunction(func, exec) {
  var script = document.createElement("script");
  script.textContent = "-" + func + (exec ? "()" : "");
  document.body.appendChild(script);
}

The "-" here makes certain the function is parsed as an expression so that exec can be used to immediately execute upon adding it.You call the function like so:

function myFunction () {
    return Grooveshark.playNextSong();
}

// Inject the function and execute it:
addFunction(myFunction, true);

google chrome extension - UserScripts & Greasemonkey: calling a websit...

javascript google-chrome-extension greasemonkey userscripts
Rectangle 27 1

Or if you simply want to debug your script then you can use Chrome with Tampermonkey.

Just remember that if install from file system then you need to enable access for Tampermonkey. See video in the FAQ on how to enable this: http://tampermonkey.net/faq.php#Q204

jquery - How to debug Greasemonkey script on Firefox 30? - Stack Overf...

jquery twitter-bootstrap debugging firefox greasemonkey
Rectangle 27 1

The only way I found to do it actually (v.42) is to use the remote debugging system. If you want to reproduce:

  • distant debug the profile in 1..While selecting go "Open app" > "RUNTIME APPS", select main process.

jquery - How to debug Greasemonkey script on Firefox 30? - Stack Overf...

jquery twitter-bootstrap debugging firefox greasemonkey
Rectangle 27 1

Or if you simply want to debug your script then you can use Chrome with Tampermonkey.

Just remember that if install from file system then you need to enable access for Tampermonkey. See video in the FAQ on how to enable this: http://tampermonkey.net/faq.php#Q204

jquery - How to debug Greasemonkey script on Firefox 30? - Stack Overf...

jquery twitter-bootstrap debugging firefox greasemonkey
Rectangle 27 1

The only way I found to do it actually (v.42) is to use the remote debugging system. If you want to reproduce:

  • distant debug the profile in 1..While selecting go "Open app" > "RUNTIME APPS", select main process.

jquery - How to debug Greasemonkey script on Firefox 30? - Stack Overf...

jquery twitter-bootstrap debugging firefox greasemonkey
Rectangle 27 8

Normally XMLHttpRquest, and that includes jQuery's higher-level API around it, does not allow unrestricted cross-site requests but is limited by the same-origin policy and CORS.

As @epascarello already pointed out, you may use GM_xmlhttpRequest (Scriptish) which allows you to perform any cross-site XHR even when the server does not implement CORS or allows the origin site. It also comes with some other goodies.

You should add a @grant GM_xmlhttpRequest meta data block to your user script, or your script may break in the future.

Since you mentioned Chrome extensions: Firefox extensions can perform cross-site XHR as well. E.g. most user scripts should be easily portable to an SDK add-on using PageMod and enabling certain permissions analog to what you'd do in a Chrome extension.

if only there was a cross browser method for doing this that would work on scripts and extensions, it would make things a lot easier.

javascript - How do I allow Cross-Origin Requests from greasemonkey sc...

javascript google-chrome firefox cross-domain localhost
Rectangle 27 9

The reason that exampleFunction was undefined is because Chrome userscripts operate in a sandbox ("isolated world"). Note that Greasemonkey scripts often operate in a sandbox too, but yours is currently running with an implicit @grant none. If your script were to use a GM_ function, it would stop working in Firefox too.

To make this script work on both browsers (and a few others, as well), use Script Injection similar to this answer.

window.onload

To get around that, add // @run-at document-end to the metadata block.

// ==UserScript==
// @name            SomeName
// @namespace       http://example.com/userscripts
// @description     Greets the world
// @include         http://example.com/*
// @run-at          document-end
// @grant           none
// ==/UserScript==

function GM_main () {
    window.onload = function () {
        console.log(exampleFunction);
        alert("LOADED!");
    }
}

addJS_Node (null, null, GM_main);

//-- This is a standard-ish utility function:
function addJS_Node (text, s_URL, funcToRun, runOnLoad) {
    var D                                   = document;
    var scriptNode                          = D.createElement ('script');
    if (runOnLoad) {
        scriptNode.addEventListener ("load", runOnLoad, false);
    }
    scriptNode.type                         = "text/javascript";
    if (text)       scriptNode.textContent  = text;
    if (s_URL)      scriptNode.src          = s_URL;
    if (funcToRun)  scriptNode.textContent  = '(' + funcToRun.toString() + ')()';

    var targ = D.getElementsByTagName ('head')[0] || D.body || D.documentElement;
    targ.appendChild (scriptNode);
}

It's not always working... And I can't determine the reason of that

How is it "not working"? What are the symptoms exactly? Use the script from the answer with no changes but the @include line. What is the URL of the page where the script appears not to work? Is that page taking a long time to completely load? onload will not fire until the last large image, etc., has finished downloading.

Try it by yourself: remove "console.log(exampleFunction);" and leave "alert" from the script. Staying on this page, click on your profile link and refresh that page several times. How is it working for you? I don't get onload event in half of cases at all

@aljesco, I did that and it works every single page load or refresh, without fail. What version of Chrome are you using? What OS? What are your symptoms and error messages? Temporarily disable all other extensions and try again.

javascript - window.onload works in Firefox+Greasemonkey script but no...

javascript google-chrome cross-browser greasemonkey userscripts
Rectangle 27 9

The reason that exampleFunction was undefined is because Chrome userscripts operate in a sandbox ("isolated world"). Note that Greasemonkey scripts often operate in a sandbox too, but yours is currently running with an implicit @grant none. If your script were to use a GM_ function, it would stop working in Firefox too.

To make this script work on both browsers (and a few others, as well), use Script Injection similar to this answer.

window.onload

To get around that, add // @run-at document-end to the metadata block.

// ==UserScript==
// @name            SomeName
// @namespace       http://example.com/userscripts
// @description     Greets the world
// @include         http://example.com/*
// @run-at          document-end
// @grant           none
// ==/UserScript==

function GM_main () {
    window.onload = function () {
        console.log(exampleFunction);
        alert("LOADED!");
    }
}

addJS_Node (null, null, GM_main);

//-- This is a standard-ish utility function:
function addJS_Node (text, s_URL, funcToRun, runOnLoad) {
    var D                                   = document;
    var scriptNode                          = D.createElement ('script');
    if (runOnLoad) {
        scriptNode.addEventListener ("load", runOnLoad, false);
    }
    scriptNode.type                         = "text/javascript";
    if (text)       scriptNode.textContent  = text;
    if (s_URL)      scriptNode.src          = s_URL;
    if (funcToRun)  scriptNode.textContent  = '(' + funcToRun.toString() + ')()';

    var targ = D.getElementsByTagName ('head')[0] || D.body || D.documentElement;
    targ.appendChild (scriptNode);
}

It's not always working... And I can't determine the reason of that

How is it "not working"? What are the symptoms exactly? Use the script from the answer with no changes but the @include line. What is the URL of the page where the script appears not to work? Is that page taking a long time to completely load? onload will not fire until the last large image, etc., has finished downloading.

Try it by yourself: remove "console.log(exampleFunction);" and leave "alert" from the script. Staying on this page, click on your profile link and refresh that page several times. How is it working for you? I don't get onload event in half of cases at all

@aljesco, I did that and it works every single page load or refresh, without fail. What version of Chrome are you using? What OS? What are your symptoms and error messages? Temporarily disable all other extensions and try again.

javascript - window.onload works in Firefox+Greasemonkey script but no...

javascript google-chrome cross-browser greasemonkey userscripts
Rectangle 27 9

The reason that exampleFunction was undefined is because Chrome userscripts operate in a sandbox ("isolated world"). Note that Greasemonkey scripts often operate in a sandbox too, but yours is currently running with an implicit @grant none. If your script were to use a GM_ function, it would stop working in Firefox too.

To make this script work on both browsers (and a few others, as well), use Script Injection similar to this answer.

window.onload

To get around that, add // @run-at document-end to the metadata block.

// ==UserScript==
// @name            SomeName
// @namespace       http://example.com/userscripts
// @description     Greets the world
// @include         http://example.com/*
// @run-at          document-end
// @grant           none
// ==/UserScript==

function GM_main () {
    window.onload = function () {
        console.log(exampleFunction);
        alert("LOADED!");
    }
}

addJS_Node (null, null, GM_main);

//-- This is a standard-ish utility function:
function addJS_Node (text, s_URL, funcToRun, runOnLoad) {
    var D                                   = document;
    var scriptNode                          = D.createElement ('script');
    if (runOnLoad) {
        scriptNode.addEventListener ("load", runOnLoad, false);
    }
    scriptNode.type                         = "text/javascript";
    if (text)       scriptNode.textContent  = text;
    if (s_URL)      scriptNode.src          = s_URL;
    if (funcToRun)  scriptNode.textContent  = '(' + funcToRun.toString() + ')()';

    var targ = D.getElementsByTagName ('head')[0] || D.body || D.documentElement;
    targ.appendChild (scriptNode);
}

It's not always working... And I can't determine the reason of that

How is it "not working"? What are the symptoms exactly? Use the script from the answer with no changes but the @include line. What is the URL of the page where the script appears not to work? Is that page taking a long time to completely load? onload will not fire until the last large image, etc., has finished downloading.

Try it by yourself: remove "console.log(exampleFunction);" and leave "alert" from the script. Staying on this page, click on your profile link and refresh that page several times. How is it working for you? I don't get onload event in half of cases at all

@aljesco, I did that and it works every single page load or refresh, without fail. What version of Chrome are you using? What OS? What are your symptoms and error messages? Temporarily disable all other extensions and try again.

javascript - window.onload works in Firefox+Greasemonkey script but no...

javascript google-chrome cross-browser greasemonkey userscripts
Rectangle 27 1

If you look in Firefox's Browser Console (ControlShiftJ), you should see an error like:

As a rule, it's best to @require jQuery for your userscripts, and that avoids all kinds of sneaky conflict problems -- especially when you also use a @grant other than none.

Side note: There's no point in using document.addEventListener("DOMContentLoaded".... Unless you have set @run-at document-start, the script runs at that point, and after jQuery is ready, by default.

Thank you very much, @requireing jQuery was all it took. Did not think of this as the culprit because of all the other jQuery code was working.

Yep, that's part of why it's called "time-bomb" code.

javascript - jQuery in a Greasemonkey script – click event is not firi...

javascript jquery firefox greasemonkey
Rectangle 27 3

I use const instead of var in my Greasemonkey scripts, but it is because they will run only on Firefox... Name convention can be indeed the way to go, too (I do both!).

Are there constants in JavaScript? - Stack Overflow

javascript constants
Rectangle 27 3

I use const instead of var in my Greasemonkey scripts, but it is because they will run only on Firefox... Name convention can be indeed the way to go, too (I do both!).

Are there constants in JavaScript? - Stack Overflow

javascript constants
Rectangle 27 2

Here is a JavaScript workaround I used to restore console API after it was set to empty function by a script on the page (works in Firefox 46, tested in Firebug and in greasemonkey script):

function restoreConsole() {
    var i = document.createElement('iframe');
    i.style.display = 'none';
    document.body.appendChild(i);
    window.console = i.contentWindow.console;
    i.parentNode.removeChild(i);
}

javascript - Firefox Web Console Disabled? - Stack Overflow

javascript html firefox
Rectangle 27 2

Here is a JavaScript workaround I used to restore console API after it was set to empty function by a script on the page (works in Firefox 46, tested in Firebug and in greasemonkey script):

function restoreConsole() {
    var i = document.createElement('iframe');
    i.style.display = 'none';
    document.body.appendChild(i);
    window.console = i.contentWindow.console;
    i.parentNode.removeChild(i);
}

javascript - Firefox Web Console Disabled? - Stack Overflow

javascript html firefox
Rectangle 27 24

Updatier: The Mene+Shuman fix now is busted with Firefox 30 and Firebug 2. Firefox 31 may provide workarounds (will investigate). In the meantime, use the "General workaround strategies" listed below.

If you open about:config and set extensions.firebug.filterSystemURLs to false then you can use Firebug to debug the Greasemonkey script just like any other.

This works irregardless of the @grant mode.

Because Greasemonkey operates in a sandbox, Firebug cannot see it. There is no easy way around this.

debugging - How to debug Greasemonkey script with the Firebug extensio...

debugging firebug greasemonkey
Rectangle 27 24

Updatier: The Mene+Shuman fix now is busted with Firefox 30 and Firebug 2. Firefox 31 may provide workarounds (will investigate). In the meantime, use the "General workaround strategies" listed below.

If you open about:config and set extensions.firebug.filterSystemURLs to false then you can use Firebug to debug the Greasemonkey script just like any other.

This works irregardless of the @grant mode.

Because Greasemonkey operates in a sandbox, Firebug cannot see it. There is no easy way around this.

debugging - How to debug Greasemonkey script with the Firebug extensio...

debugging firebug greasemonkey
Rectangle 27 3

Firebug 1.x works with Greasemonkey 1.x well, while I can debug userscripts installed in Greasemonkey using Firebug to set a breakpoint. But after I upgrade my Firefox to 30 and the Firebug was upgraded to 2.0. It seems does not work any more.

Not that surprising, the method of script discovery changed pretty fundamentally in 2.0... Previously we were iterating over all scripts everywhere and trying to tie them somewhere, now we start from the web page and hook script creation for it, which is much more sane.

The way to get this fixed (presuming it doesn't work in the built-in debugger) is to file it against Devtools at https://bugzilla.mozilla.org, or against Greasemonkey; there's very little we can do on the Firebug side I believe.

javascript - How to debug Greasemonkey scripts in Firebug? - Stack Ove...

javascript firefox firebug greasemonkey