How to convert bookmarklet to Greasemonkey email browser?

Is there an easy way to do this. And is there something that needs to be changed due to differences in how it works?

+6
source share
2 answers

The easiest way to do this:

  • Run the bookmarklet code using the URL decoder . so javascript:alert%20('Hi%20Boss!')%3B , for example, becomes:
    javascript:alert ('Hi Boss!');

  • Remove the leading javascript: Result: alert ('Hi Boss!');

  • Add this code at the end of the Greasemonkey file. For example, create a file with the name,
    Hello World.user.js using 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 ( Ctrl O ). Greasemonkey will offer to install the script.

  • Now the bookmarklet code will be launched automatically on all pages that you specified using the @include and @exclude .

  • Update: To ensure maximum compatibility, use the @grant none directive, which was added in later versions of Greasemonkey and Tampermonkey.


IMPORTANT:

+5
source

Here is a good article to avoid common mistakes due to the differences between "normal" JS and Greasemonkey.

The most important things in the beginning:

  • Do not use functions as strings, for example: window.setTimeout("my_func()", 1000); , but rather window.setTimeout(my_func, 1000); or window.setTimeout(function(){doSomething(); doSomethingOther();}, 1000);
  • Do not set element.onclick , but rather element.addEventListener("click", my_func, true);
  • Some code that usually returns various DOM objects in the Greasemonkey environment returns those objects that are wrapped in an XPCNativeWrapper. This is for security reasons.

    Some methods and properties are transparent, and you can call them on a wrapped object, but some do not. Read the article on how to get around this; you can also use (this is not recommended at all, but for testing, etc.) wrappedJSObject. When obj.something / obj.something() does not work in Greasemonkey, try obj.wrappedJSObject.something / obj.wrappedJSObject.something() .

+1
source

Source: https://habr.com/ru/post/894074/


All Articles