Open a new tab with custom HTML instead of a URL
You can do this: var newWindow = window.open(); and then do newWindow.document.write(“ohai”);
You can do this: var newWindow = window.open(); and then do newWindow.document.write(“ohai”);
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
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!
@match only works on the protocol/scheme, host, and pathname of a URL. To trigger off the query parameters, you can either use @include or use @match and also test the URL yourself. Note that the @match approach performs faster. With @include, you can use a regex syntax. See, also Include and exclude rules. In this … Read more
Looking at frame length breaks down generally if page A itself has frames (I know this might not be the case for this specific instance). The more reliable and meaningful test would be: if (window!=window.top) { /* I’m in a frame! */ }
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
Refer to the W3C spec for keyboard events. Several boolean attributes are provided to determine if modifier keys were pressed in conjunction with whatever target key you are interested in. They are: ctrlKey — The “Control” key was also pressed. shiftKey — The “Shift” key was also pressed. altKey … Read more
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
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
There is no way to cleanly remove an event handler unless you stored a reference to the event handler at creation. I will generally add these to the main object on that page, then you can iterate and cleanly dispose of them when done with that object.