javascript - 是否可以以编程方式设置用户脚本可以运行的网页?
问题描述
我目前正在开发一个用户脚本,可以在我离开公司后使用几年。目标受众具有基本的计算机知识——足以填写一个简单的配置菜单。
我担心脚本可能会中断,因为 IT 部门可能会移动我在@include
. 我可以记录如何修复脚本,但我想针对我知道非常无能的 IT 部门进行防御性编程。我知道我可以做到以下几点:
- 允许用户在配置页面中指定 URL。
- 将 设置
@include
为非常通用的 ashttps://*.edu/*
并在执行命令之前使用if
语句来确定我们在用户指定的 URL 上。window.location.href
关于这个解决方案的一些担忧:我希望脚本可以在机构之间轻松重用,这就是我*.edu
提到的原因而不是specificschool.edu
. 上面的配置将使脚本在任何网站的每个页面上执行,但由于该语句.edu
,这些操作只有在网页上时才会发生。if
这似乎非常低效,但我知道这可能是在便于携带和效率之间进行权衡。
有没有更好的方法来做到这一点?
解决方案
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.
推荐阅读
- c# - 从 asp.net MVC 中的复选框值中获取 0
- android - google vision textRecognizer.isOperational() 方法总是返回 false
- linux - kprobes 相对于 kretprobes 的优势
- javascript - 元素类型无效:react-transition-group 中的预期字符串
- reactjs - React Native 不会在 setState 处呈现下一个数组项
- javascript - 具有两个功能的 async/await JavaScript
- drupal - 如何添加页眉区域?
- html - Safari 中的 CSS 旋转动画中断
- css - 在十月 CMS 中链接到图像作为背景图像属性的语法
- jquery-select2 - 错误消息未正确显示并且选择了错误的输入