首页 > 解决方案 > 是否可以以编程方式设置用户脚本可以运行的网页?

问题描述

我目前正在开发一个用户脚本,可以在我离开公司后使用几年。目标受众具有基本的计算机知识——足以填写一个简单的配置菜单。

我担心脚本可能会中断,因为 IT 部门可能会移动我在@include. 我可以记录如何修复脚本,但我想针对我知道非常无能的 IT 部门进行防御性编程。我知道我可以做到以下几点:

关于这个解决方案的一些担忧:我希望脚本可以在机构之间轻松重用,这就是我*.edu提到的原因而不是specificschool.edu. 上面的配置将使脚本在任何网站的每个页面上执行,但由于该语句.edu,这些操作只有在网页上时才会发生。if这似乎非常低效,但我知道这可能是在便于携带和效率之间进行权衡。

有没有更好的方法来做到这一点?

标签: javascripttampermonkeyuserscriptsdefensive-programminggreasemonkey-4

解决方案


Allow the user to specify URLs in a configuration page.

As mentioned by wOxxOm, user-script manager e.g. TamperMonkey, GreaseMonkey, ViolentMonkey & FireMoneky (Firefox only), have user settings that users can enter, although that often requires some prior knowledge.

Set the @include to be very generic as https://.edu/ and use an if statement to determine we're on the user-specified URL using window.location.href before executing commands.

That is also possible (although inefficient) but may run into the same issues if the web site changes in such a way that would make the 'if statement' obsolete.

Is there any better way to do this?

Yes...script update system.

If the script is placed on an accessible server (GreasyFork, Github, etc) then an update URL can be set and then script can be automatically or manually updated by the users wherever it fails to function or an update is required.


推荐阅读