My Very Simple Greasemonkey Script Is Not Running

My very simple Greasemonkey script is not running?

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.

Universal Greasemonkey debug steps:

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

  1. First make sure that the script is even firing for the page in question.
    While browsing that page, click on the down-triangle next to the Greasemonkey icon (Alternatively, you can Open Tools -> Greasemonkey on the Firefox menu.) and verify that the expected script name appears and is checked. EG:

    Greasemonkey states

  2. See if there are any relevant messages/errors on Firefox's Browser Console.

    Activate the console by pressing CtrlShiftJ, or equivalent.

    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:

    Sample Browser console results

  3. Open about:config, search for capability.policy.default.Window.alert and delete or reset the value, if it is found.

  4. Uninstall the Greasemonkey script.
  5. Completely clear the browser cache.
  6. Shutdown Firefox completely. Use Task Manager, or equivalent, to verify that there is no Firefox thread/task/process in memory.
  7. Restart Firefox.
  8. Install the Greasemonkey script afresh.
  9. If it still doesn't work, create a new Firefox profile or try a different computer altogether.

Additional issues:

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

  2. @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.)

  3. @namespace does not control where the page runs. The only thing @namespace does is allow more than one script to have the same name (as long as their @namespaces are different). See the @namespace documentation.

  4. Avoid using alert() for debugging. It's annoying and can mask timing problems.

    Use console.log(). You can see the results, and helpful error messages (hint, hint) on the Browser Console.

  5. Google almost always uses/redirects to www.google.com (For English USA users). So, // @include  https://google.com will almost never work like you want.

    Recommend you use:

    // @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.


Putting it all together:

  1. Uninstall your script.
  2. Restart Firefox.
  3. 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!");
  4. Visit Google and note the results on Firefox's Browser Console.

  5. If there is still a problem, follow all of the debug steps above.
  6. If there is still a problem, Open a new question and supply ALL of the following:
    1. The three versions, mentioned above.
    2. The relevant errors and messages you get on the Browser Console.
    3. The exact code and steps needed to duplicate the problem. Make an MCVE for this!
    4. A short summary of what you have tried to solve the problem.

How to determine why a Greasemonkey script is not running

you can write logs everywhere in your scripts to get better traces of what if being done in them:

GM_log("Hello, World!");

http://wiki.greasespot.net/GM_log

More info:
http://wiki.greasespot.net/Greasemonkey_Manual:Other_Useful_Tools#JavaScript_Console

Another tip: Take a look at the whole greasemonkey wiki. They have a lot of good stuff in there:
http://wiki.greasespot.net/Main_Page

Greasemonkey script not working

var interval;

function go()
{
if (document.getElementsByClassName('tmask')[0]) {
document.getElementsByClassName('tmask')[0].style.display = 'none';
document.getElementsByClassName('tbox')[0].style.display = 'none';
clearInterval(interval);
}
}

window.addEventListener( 'load', function( event )
{
if( window.top == window.self ) //don't run in frames
{
interval = setInterval( go, 500 );
}
}, false );

My guess is you're not waiting for the page to load, thus your script runs before 'tmask' actually exists.

I have to refresh the page for my Greasemonkey script to run?

See "addEventListener only working at page refresh?" for more information and a similar scenario.

Page elements, that your script expects, are no doubt appearing after the load event has fired. Additionally, from your comments, it sounds like whole sections of the page are swapped out by AJAX, but the AJAX is polite enough to change the URL hash. This means you'll want to fire off the hashchange event.

Don't use addEventListener ("load"... in this case. Use the waitForKeyElements() utility in conjunction with hashchange.

Without refactoring the whole script to use jQuery (which would give clearer and more robust code), replace everything before function addLinks() {..., with:

// ==UserScript==
// @name Job Aids
// @description Aid in closing tickets
// @include https://techaccess.ad.qintra.com/WorkJobs/WorkJobs.aspx*
// @namespace camzilla.net
// @version 1.1.20121128
// @require http://ajax.googleapis.com/ajax/libs/jquery/1.7.2/jquery.min.js
// @require https://gist.github.com/raw/2625891/waitForKeyElements.js
// @grant GM_addStyle
// ==/UserScript==
/*- The @grant directive is needed to work around a design change
introduced in GM 1.0. It restores the sandbox.
*/

//-- Pages are "loaded" via AJAX...
window.addEventListener ("hashchange", fireOnNewPage, false);

waitForKeyElements ("#crasCircuitLoss", crasResults);
waitForKeyElements ("#finalTestInsightNo", finalResults);
waitForKeyElements ("#flatRateJacks", closingComments);
waitForKeyElements ("#ToneSlopeInsightNo", toneSlopeResults);
waitForKeyElements ("div[data-bind="CurrentJob.addr"]", addLinks);

function fireOnNewPage () {
switch (location.hash.toLowerCase() ) {
case "#finaltest":
case "#threetoneslope":
case "#codes":
case "#cras":
case "#jobinfo":
//-- No action needed, waitForKeyElements() handles this.
break;
default:
if (getCookie("updater") == null) {
var d = new Date();

setCookie("updater", d.getTime(), 1);
try {
updateCheck();
} catch(err) {
// alert('Update checking failed');
}
}
break;
}
}
fireOnNewPage (); //-- Initial run on initial, full page load.

Greasemonkey script not executed when unusual content loading is being used

Aren't they using ajax to load content in a div? You can find the element which is being updated by using Firebug for example.

When you click something and the URL changes, but with a # on the URL and after this some text, it means the text is not a path, it's a parameter, the browser won't change the page you are, so since GreaseMonkey inject the script on the page loads it won't inject again, because the page is not reloading.
As in your example the URL facebook.com/#!/sk=messages is not navigating away from facebook.com/ it will not fire window.load event.
So you need to find which element is being changed and add an event listener to that element, you can do is using Firebug as I mentioned before.

After you find out what element is getting the content, you have to add an event listener to that element and not the page (GreaseMonkey adds only on the window load event).

So in you GM script you would have ("air code")

document.getElement('dynamic_div').addEvent('load', /*your script*/);

Tampermonkey hello word script not working

Well, this was such a basic thing.
I installed it also in Firefox, to see if something was wrong with my configuration.
After clicking around, I realized the firefox version changes color when clicked, and then got that if its grey, its off, if its colorful, it works.
Therefore, the reason it was not working in Chrome, is just that I had to click on the Tampermonkey icon then click the "enabled" tick...
Noobs mistake, but undocumented one.
Hope this helps someone in the future.



Related Topics



Leave a reply



Submit