简体   繁体   English

如何将小书签转换为Greasemonkey用户脚本?

[英]How to convert a bookmarklet into a Greasemonkey userscript?

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? 由于运行方式的差异,是否需要更改任何内容?

The easiest way to do this: 最简单的方法是:

  1. Run the bookmarklet code through a URL decoder . 通过URL解码器运行小书签代码。 so that javascript:alert%20('Hi%20Boss!')%3B , for example, becomes: 因此,例如javascript:alert%20('Hi%20Boss!')%3B变为:
    javascript:alert ('Hi Boss!');

  2. Strip the leading javascript: off. 剥离领先的javascript:关闭。 Result: alert ('Hi Boss!'); 结果: alert ('Hi Boss!');

  3. Add this code to the end of your Greasemonkey file. 将此代码添加到您的Greasemonkey文件的末尾。 For example, create a file named, 例如,创建一个名为的文件,
    Hello World.user.js , with this code: Hello World.user.js ,代码如下:

     // ==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 ( Ctrl O ). 使用Firefox( Ctrl O )打开Hello World.user.js Greasemonkey will prompt to install the script. Greasemonkey将提示您安装脚本。

  5. Now the bookmarklet code will run automatically on whatever pages you specified with the @include and @exclude directives. 现在,bookmarklet代码将自动在您使用@include@exclude指令指定的任何页面上运行。

  6. Update: To ensure maximum compatibility, use the @grant none directive that was added in later versions of Greasemonkey and Tampermonkey. 更新:为了确保最大的兼容性,请使用在更高版本的Greasemonkey和Tampermonkey中添加的@grant none指令。


IMPORTANT: 重要:

Here is a very good article to avoid common pitfalls because of differences between "normal" JS and Greasemonkey. 这是一篇很好的文章, 可以避免由于“普通” JS和Greasemonkey之间的差异而引起的常见陷阱

The most important things at the beginning: 一开始最重要的事情:

  • Do not use functions as strings, like: window.setTimeout("my_func()", 1000); 不要将函数用作字符串,例如: window.setTimeout("my_func()", 1000); but rather window.setTimeout(my_func, 1000); 而是window.setTimeout(my_func, 1000); or window.setTimeout(function(){doSomething(); doSomethingOther();}, 1000); window.setTimeout(function(){doSomething(); doSomethingOther();}, 1000);
  • Do not set element.onclick but rather element.addEventListener("click", my_func, true); 不设置element.onclick而是设置element.onclick element.addEventListener("click", my_func, true);
  • Some code that normally returns various DOM objects, in Greasemonkey environment returns those objects wrapped in XPCNativeWrapper. 一些通常返回各种DOM对象的代码,在Greasemonkey环境中,返回包装在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. 您还可以使用(通常不建议这样做,但是用于测试等)wrappedJSObject属性。 It is, when obj.something / obj.something() doesn't work in Greasemonkey, try obj.wrappedJSObject.something / obj.wrappedJSObject.something() . 是的,当obj.something / obj.something()在Greasemonkey中不起作用时,请尝试obj.wrappedJSObject.something / obj.wrappedJSObject.something()

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 有没有办法自动将Greasemonkey脚本转换为书签? - Is there a way to automatically convert a Greasemonkey script into a bookmarklet? Greasemonkey 用户脚本未正确加载 - Greasemonkey userscript is not loading properly 在PetroleumMonkey用户脚本中路由 - Routing in greasemonkey userscript 如何用Greasemonkey的用户脚本替换JSON字符串中的文本 - How can I replace the text in JSON string with a userscript for Greasemonkey 如何在运行相同 GreaseMonkey 用户脚本的 2 个选项卡之间传递信息? - How to pass information between 2 tabs running same GreaseMonkey userscript? 可以将两个bookmarklet和一个Greasemonkey脚本合并为一个bookmarklet吗? 如果是这样,怎么样? - Can two bookmarklets and a Greasemonkey script be combined into one bookmarklet? If so, how? 用户脚本确实在油脂猴子插件上运行 - userscript did run on greasemonkey addon 如何将用户脚本转换为Firefox加载项 - How to convert userscript to Firefox add-on 如何使用远程页面的构造函数在我的Greasemonkey UserScript中创建一个Object? - How do I use constructor of a remote Page to create an Object in my Greasemonkey UserScript? 加载整页后如何在 iframe 内重复运行 Greasemonkey/Tampermonkey 用户脚本? - How to run Greasemonkey/Tampermonkey userscript repeatedly inside of iframe after full page load?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM