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

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

Why is usage of the downloadURL & updateURL keys called unusual and how do they work?

Use of those keys is discouraged mainly by Greasemonkey’s lead developer. Most others, including the Tampermonkey team feel no need for such a warning. Also note that those directives are not always required for auto-updates to work. Some reasons why he would say that it was unusual and that “most” scripts should omit it: In … Read more

How to override the alert function with a userscript?

Update: For modern versions of Tampermonkey, Violentmonkey, Greasemonkey (but strongly recommended to avoid GM 4+): You can intercept alert() in most cases by using @run-at document-start. For example, load this script and then visit the test page: // ==UserScript== // @name _Overwrite Alert // @match *://output.jsbin.com/* // @grant none // @run-at document-start // ==/UserScript== var alrtScope; … Read more

Add parameters to the URL (redirect) via a Greasemonkey/Tampermonkey/Userscript

The script should do these things: Detect if the current URL is already to the compact site. Load the compact version of the page if necessary. Beware of “anchor” URLS (they end with “fragments” or “hashes” (#…) ) and account for them. Keep the unwanted pages out of the browser history so that the back … Read more

How can two instances of a userscript communicate between frames?

The two script instances can communicate with each other using postMessage(). Regarding: This would be needed to sync the instances, so for example to have the iframe one to execute its task only after the webpage one completed, and vice versa. That’s what is shown in this other answer. But Chrome has bugs in how … Read more

Native javascript equivalent of jQuery :contains() selector

This should do in modern browsers: function contains(selector, text) { var elements = document.querySelectorAll(selector); return [].filter.call(elements, function(element){ return RegExp(text).test(element.textContent); }); } Then use it like so: contains(‘p’, ‘world’); // find “p” that contain “world” contains(‘p’, /^world/); // find “p” that start with “world” contains(‘p’, /world$/i); // find “p” that end with “world”, case-insensitive …

How can I load a shared web worker with a user-script?

You can use fetch(), response.blob() to create an Blob URL of type application/javascript from returned Blob; set SharedWorker() parameter to Blob URL created by URL.createObjectURL(); utilize window.open(), load event of newly opened window to define same SharedWorker previously defined at original window, attach message event to original SharedWorker at newly opened windows. javascript was tried … Read more