在非 HTML 页面上运行 Greasemonkey 脚本
我有以下相当基本的greasemonkey 脚本:
var newloc = location.href.replace(/^(.*)-xyz-(.*)$/i, "$1$2");
if (newloc != location.href)
location.href = newloc;
也就是说,它基本上从URL 中删除“-xyz-”并再次加载页面。因此,如果您导航到“www.example.com/a-xyz-b/”,它将重新加载“www.example.com/ab/”页面。
现在,如果页面是 HTML 页面,则脚本可以正常工作。但是,如果我打开 .jpg 文件或非 HTML 文件,则脚本根本不会运行。
这只是greatmonkey的限制吗?仅当页面实际上是文本/html 时它才有效?完成此功能的替代方法是什么?
I have the following fairly basic greasemonkey script:
var newloc = location.href.replace(/^(.*)-xyz-(.*)$/i, "$1$2");
if (newloc != location.href)
location.href = newloc;
That is, it basically strips out "-xyz-" from the URL and loads the page again. So if you navigate to "www.example.com/a-xyz-b/" it'll reload the page at "www.example.com/ab/".
Now, the script work fine if the page is an HTML page. But if I open a .jpg file or something that's not HTML then the script does not run at all.
Is this just a limitation of greasemonkey? That it only works if the page is actually text/html? What is an alternative way this functionality could be done?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
是的,Greasemoney 会触发
DOMContentLoaded
事件,该事件似乎不会在媒体对象(无 DOM)上触发。通过在父/引用页面上触发并更改那里的链接来解决这个问题。
或者,如果文件名位于本地计算机上,请使用文本编辑器或批处理作业重命名/重写链接/名称。
如果这些解决方法都不可行,请发布如何将这些 URL 提供给 FireFox 的具体详细信息(如果不是 FF,请命名正在使用的浏览器)。
Yes, Greasemoney fires on the
DOMContentLoaded
event, which doesn't seem to trigger on media objects (no DOM).Get around this by firing on the parent/referrer pages and changing the links there.
Or, if the file names are on the local machine, use a text editor or batch job to rename/rewrite the links/names.
If neither of these workarounds is viable, post the specific details of how you are feeding these URLS to FireFox (name the browser in use if it's not FF).