Injecting JS functions into the page from a Greasemonkey script on Chrome

The only way to communicate with the code running on the page in Chrome is through the DOM, so you’ll have to use a hack like inserting a <script> tag with your code. Note that this may prove buggy if your script needs to run before everything else on the page. EDIT: Here’s how the …

Read more

Run Greasemonkey script on the same page, multiple times?

The simplest, most robust way is to use the waitForKeyElements() utility. Here is a complete script that uses jQuery and waitForKeyElements to alter Amazon search results: // ==UserScript== // @name _Amazon Search, alter results // @include http://www.amazon.com/s/* // @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 …

Read more

Unique element ID, even if element doesn’t have one

The answer is no, there isn’t an internal id you can access. Opera and IE (maybe Safari?) support .sourceIndex (which changes if DOM does) but Firefox has nothing of this sort. You can simulate source-index by generating Xpath to a given node or finding the index of the node from document.getElementsByTagName(‘*’) which will always return …

Read more

How/Where to store data in a Chrome Tampermonkey script other than localStorage?

Since you are using Tampermonkey (Chrome) and Greasemonkey (Firefox). Go ahead and use GM_setValue(). It cannot be cleared by Facebook or by any other website. It has the advantage of storing values cross-domain, as well. ~~~ Beware that the bog-standard GM_setValue() is somewhat problematic on Firefox. It can cause a script instance to crash on …

Read more

Can jQuery selectors be used with DOM mutation observers?

Yes, you can use jQuery selectors on data returned to mutation observer callbacks. See this jsFiddle. Suppose you had HTML like so and you set an observer, like so: var targetNodes = $(“.myclass”); var MutationObserver = window.MutationObserver || window.WebKitMutationObserver; var myObserver = new MutationObserver(mutationHandler); var obsConfig = { childList: true, characterData: true, attributes: true, subtree: …

Read more

How to debug Greasemonkey script with the Firebug extension?

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. Update: This answer is now obsolete. If you open about:config and set extensions.firebug.filterSystemURLs to false then you can use Firebug to debug the Greasemonkey script …

Read more

How can I intercept XMLHttpRequests from a Greasemonkey script?

The accepted answer is almost correct, but it could use a slight improvement: (function(open) { XMLHttpRequest.prototype.open = function() { this.addEventListener(“readystatechange”, function() { console.log(this.readyState); }, false); open.apply(this, arguments); }; })(XMLHttpRequest.prototype.open); Prefer using apply + arguments over call because then you don’t have to explicitly know all the arguments being given to open which could change!