使用用户脚本作为常规 JavaScript?
如果我有一个在网上找到的用户脚本,并且我想将其作为普通的 javascript(即不是用户脚本)合并到网站中,我该怎么做?
这看起来很简单,但我对 javascript 和用户脚本很陌生,希望有人可以帮助我。
例如,我找到了这个用户脚本:“将 UPS 和 FedEx 跟踪号码转换为链接"...
这将解析页面的正则表达式匹配任何 UPS/FEDEX/USPS 跟踪号码,并将其转换为指向相应承运商跟踪网站的链接以跟踪该号码。
这作为用户脚本非常有效,但我希望它在我正在处理的网站上自动运行(不需要访问者下载/安装用户脚本)。
我如何将此用户脚本用作常规 JavaScript?
If I have a userscript that I've found online, and I'd like to incorporate it into a website as a normal javascript (i.e. not userscript), how can I do that?
This seems like it would be something pretty simple but I am new to javascript and userscript, hopefully somebody can help me out.
For example, I found this userscript: "Convert UPS and FedEx Tracking Numbers to Links"...
Which will parse a page for regular expressions matching any UPS/FEDEX/USPS tracking number, and convert them to links to the respective carriers' tracking website to track that number.
This works great as a userscript, but I would like it to be automatic on the site I am working on (not require visitors to download/install a userscript).
How can I use this userscript as a regular javascript?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
用户脚本通常在页面 DOM 加载后执行,以确保页面内容可以完整访问。因此,您需要某种机制来实现这一目标。但这有点棘手,因为旧版本的 IE 需要特殊处理。
手动完成大约需要 30 行代码。像 jQuery 这样的库提供了一个 Ready() 方法,如下所示:
UserScripts are usually executed after the page DOM is loaded, to make sure that the page contents can be completely accessed. So, you need some kind of mechanism to achieve that. But this is a bit tricky since older versions of IE need special treatment.
Doing it manually would require about 30 lines of code. Libraries like jQuery offer a
ready()
method like so:它应该只是像任何其他 Javascript 文件一样链接它:
将其保存为
foo.js
,然后在您的 html 中,It should just be a matter of linking it like any other Javascript file:
Save it as
foo.js
, then in your html,您可以将其放入 JS 文件中并将该文件包含在您的页面中。
用户脚本包含普通的 Javascript 代码,但由插件执行,而不是在页面上执行。
You can just put it in a JS file and include the file in your page.
UserScripts contain normal Javascript code, but are executed by a plugin rather than being on the page.