How to convert a bookmarklet into a Greasemonkey userscript?

空扰寡人 提交于 2019-11-27 02:00:16

问题


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?


回答1:


The easiest way to do this:

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

  2. Strip the leading javascript: off.   Result: alert ('Hi Boss!');

  3. 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!');
    
  4. Open Hello World.user.js with Firefox (CtrlO ).   Greasemonkey will prompt to install the script.

  5. Now the bookmarklet code will run automatically on whatever pages you specified with the @include and @exclude directives.

  6. 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.




回答2:


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: 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 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, try obj.wrappedJSObject.something/obj.wrappedJSObject.something().



来源:https://stackoverflow.com/questions/6906025/how-to-convert-a-bookmarklet-into-a-greasemonkey-userscript

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!