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 do I get Greasemonkey to click on a button that only appears after a delay?

That code has errors. Use Firefox’s error console (CtrlShiftJ) to see them. Using jslint to check your code, can be helpful too. Anyway, this is a common Greasemonkey problem. Use the waitForKeyElements() utility to handle the delayed appearance of that button. Use jQuery to simplify the code (and make it more robust and portable). So … Read more

Add a JavaScript button using Greasemonkey or Tampermonkey?

Ok, here’s a complete script that adds a live button to SO question pages1: // ==UserScript== // @name _Adding a live button // @description Adds live example button, with styling. // @match *://stackoverflow.com/questions/* // @match *://YOUR_SERVER.COM/YOUR_PATH/* // @grant GM_addStyle // ==/UserScript== /*— Create a button in a container div. It will be styled and positioned … Read more

Error: Permission denied to access property ‘handler’

Greasemonkey 2.0 has just been pushed to all Firefox browsers set to auto-update. (GM 2 was released on June 17, 2014, but it can take a few weeks to get through the review process.) Greasemonkey 2.0 radically changed unsafeWindow handling: Backwards incompatible changes: For stability, reliability, and security the privileged sandbox has been updated to … Read more

Chrome userscript error: “Unsafe JavaScript attempt to access frame”

It’s true that ordinary javascript cannot access iframe content, that’s on a different domain, for security reasons. However, this by no means stops userscripts in Chrome, Tampermonkey or Greasemonkey. You can process iframed content in a userscript because Chrome (and Firefox) process iframe’d pages just as if they were the main page. Accounting for that, … Read more

Clicking a button on a page using a Greasemonkey/userscript in Chrome

Note: jQuery .click() does not work reliably on click events that were not set via jQuery in the first place. You need to create the right kind of event; createEvent(‘Events’) is not the way. As Jim Deville pointed out, the link pseudo-button was not being selected. You do not need jQuery for this (so far). … Read more

Watch for element creation in greasemonkey script?

Several issues (big to small): When the document is first, statically loaded; the events are childList events, not attributes events. For example, $(“body”).append (‘<p id=”foo” class=”bar”>Hiya!</p><p>blah</p>’); generates one childList event, while a subsequent $(“#foo”).attr (“class”, “bar2”); generates an attributes event. The odds that mutation.addedNodes[0] contains an element with class nav are practically zero. This is … Read more

How to use GM_xmlhttpRequest in Injected Code?

GM_ functions will not work in injected code because injected code runs in the target page’s scope. If they did work there, then unscrupulous web-sites could also use the GM_ functions — to do unspeakable evil. The solutions, most preferable first: Don’t inject code. Much of the time, it really isn’t necessary, and it always … 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