Is there a easy way to do this. And is there anything that needs to be changed due to differences in how it is ran?
2 Answers
The easiest way to do this:
Run the bookmarklet code through a URL decoder. so that
javascript:alert%20('Hi%20Boss!')%3B
, for example, becomes:
javascript:alert ('Hi Boss!');
Strip the leading
javascript:
off. Result:alert ('Hi Boss!');
Add this code to the end of your Greasemonkey file. For example, create a file named,
Hello World.user.js, with this code:// ==UserScript== // @name Hello World! // @description My first GM script from a bookmarklet // @include https://stackoverflow.com/questions/* // @grant none // ==/UserScript== alert ('Hi Boss!');
Open Hello World.user.js with Firefox (CtrlO ). Greasemonkey will prompt to install the script.
Now the bookmarklet code will run automatically on whatever pages you specified with the
@include
and@exclude
directives.Update: To ensure maximum compatibility, use the
@grant none
directive that was added in later versions of Greasemonkey and Tampermonkey.
IMPORTANT:
The userscript will run much sooner than you could ever activate a bookmark. Normally, this is not a problem.
But in some cases, you might need to wait for some part of the page to fully load.
In that case, you can use techniques/utilities like waitForKeyElements.See also, Choosing and activating the right controls on an AJAX-driven site .
If you still can't get your new script to work, be sure to read My very simple Greasemonkey script is not running?. Follow the steps and include the specified information in any question you open about problems with the new script.

- 1
- 1

- 90,639
- 22
- 233
- 295
-
This is great, but this will run automatically on every page. All of my bookmarklets are things I only want to activate sometimes (like bookmarking the page on Pinboard or looking it up on the Wayback Machine). How would you make it only execute when I want it to? – Nick S Feb 28 '18 at 15:41
-
@NickS, that's what [the `@include`, `@exclude`, and `@match` directives](https://tampermonkey.net/documentation.php#_include) are for. – Brock Adams Feb 28 '18 at 17:55
-
Sorry, let me clarify: I mean I want to be on a page, think "hey, I want to look this up on the Wayback Machine", click *something*, and execute my script (opens a new tab with a Wayback Machine search for the current url). What do I click? How does the script know when to open the tab? – Nick S Feb 28 '18 at 20:18
-
@NickS, this is beyond the scope of comments. Open a new question if needed. But beware that what you're asking has probably been covered in other Q&A's. – Brock Adams Feb 28 '18 at 20:36
-
Okay, that's cool, maybe I will. I just realized one possibility is adding an entry to the right-click context menu, though. – Nick S Mar 01 '18 at 15:48
Here is a very good article to avoid common pitfalls because of differences between "normal" JS and Greasemonkey.
The most important things at the beginning:
Do not use functions as strings, like:
but ratherwindow.setTimeout("my_func()", 1000);
window.setTimeout(my_func, 1000);
orwindow.setTimeout(function(){doSomething(); doSomethingOther();}, 1000);
Do not set
but ratherelement.onclick
element.addEventListener("click", my_func, true);
Some code that normally returns various DOM objects, in Greasemonkey environment returns those objects wrapped in XPCNativeWrapper. This is for security reasons.
Some methods and properties are "transparent" and you can invoke them on wrapped object, but some not. Read in the mentioned article about how to circumvent this; you can also use (this is not recommended generally, but for testing etc.) wrappedJSObject property. It is, when
obj.something
/obj.something()
doesn't work in Greasemonkey, tryobj.wrappedJSObject.something
/obj.wrappedJSObject.something()
.