What is the difference between @include and @match in userscripts?

You cannot use regular expressions with @match, while you can with @include. However, @include will give your users scarier security warnings about the script applying to all sites. This is even though an @include expression permits you to be more restrictive about the sites a script applies to (e.g. specifying that part of a URL … Read more

How to change a class CSS with a Greasemonkey/Tampermonkey script?

For this, just use the CSS cascade. Add a style sheet to the page with GM_addStyle(). Note: We use the !important flag to cover certain potential conflicts. Use @run-at document-start (or use Stylus, see below) to minimize “flicker” associated with changing styles after the initial render. A complete script: // ==UserScript== // @name _Override banner_url … Read more

Simulating a mousedown, click, mouseup sequence in Tampermonkey?

Send mouse events. Like so: //— Get the first link that has “stackoverflow” in its URL. var targetNode = document.querySelector (“a[href*=’stackoverflow’]”); if (targetNode) { //— Simulate a natural mouse-click sequence. triggerMouseEvent (targetNode, “mouseover”); triggerMouseEvent (targetNode, “mousedown”); triggerMouseEvent (targetNode, “mouseup”); triggerMouseEvent (targetNode, “click”); } else console.log (“*** Target node not found!”); function triggerMouseEvent (node, eventType) { … Read more

Is there any way of passing additional data via custom events?

Yes, you can use a MessageEvent or a CustomEvent. Example usage: //Listen for the event window.addEventListener(“MyEventType”, function(evt) { alert(evt.detail); }, false); //Dispatch an event var evt = new CustomEvent(“MyEventType”, {detail: “Any Object Here”}); window.dispatchEvent(evt);

Uncaught ReferenceError: function is not defined with onclick

Never use .onclick(), or similar attributes from a userscript! (It’s also poor practice in a regular web page). The reason is that userscripts operate in a sandbox (“isolated world”), and onclick operates in the target-page scope and cannot see any functions your script creates. Always use addEventListener()Doc (or an equivalent library function, like jQuery .on()). … Read more

How can I use jQuery in Greasemonkey scripts in Google Chrome?

From “User Script Tip: Using jQuery – Erik Vold’s Blog” // ==UserScript== // @name jQuery For Chrome (A Cross Browser Example) // @namespace jQueryForChromeExample // @include * // @author Erik Vergobbi Vold & Tyler G. Hicks-Wright // @description This userscript is meant to be an example on how to use jQuery in a userscript on … Read more

Using ECMAScript 6

In Chrome, most of the ES6 features are hidden behind a flag called “Experimental JavaScript features”. Visit chrome://flags/#enable-javascript-harmony, enable this flag, restart Chrome and you will get many new features. Arrow functions are not yet implemented in V8/Chrome, so this flag won’t “unlock” arrow functions. Since arrow functions are a syntax change, it is not … Read more

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)