[英]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: 最简单的方法是:
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!');
Strip the leading javascript:
off. 剥离领先的
javascript:
关闭。 Result: alert ('Hi Boss!');
结果:
alert ('Hi Boss!');
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!');
Open Hello World.user.js with Firefox ( Ctrl O ). 使用Firefox( Ctrl O )打开Hello World.user.js 。 Greasemonkey will prompt to install the script.
Greasemonkey将提示您安装脚本。
Now the bookmarklet code will run automatically on whatever pages you specified with the @include
and @exclude
directives. 现在,bookmarklet代码将自动在您使用
@include
和@exclude
指令指定的任何页面上运行。
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: 重要:
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 . 在那种情况下,您可以使用诸如waitForKeyElements之类的技术/实用程序 。
See also, Choosing and activating the right controls on an AJAX-driven site . 另请参见在AJAX驱动的网站上选择和激活正确的控件 。
If you still can't get your new script to work, be sure to read My very simple Greasemonkey script is not running? 如果仍然无法使新脚本正常工作,请确保阅读我的非常简单的Greasemonkey脚本未运行? .
。 Follow the steps and include the specified information in any question you open about problems with the new script.
请执行以下步骤,并在打开的有关新脚本问题的任何问题中包括指定的信息 。
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: 一开始最重要的事情:
window.setTimeout("my_func()", 1000);
window.setTimeout("my_func()", 1000);
window.setTimeout(my_func, 1000);
window.setTimeout(my_func, 1000);
or window.setTimeout(function(){doSomething(); doSomethingOther();}, 1000);
window.setTimeout(function(){doSomething(); doSomethingOther();}, 1000);
element.onclick
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.