firefox addon page-mod - 当 url 不匹配时
我希望能够在 url 匹配某些模式时激活小部件,但问题是我还想在 page-mod 规则与 url 不匹配时禁用小部件。 因此,如果我打开了几个选项卡,并且在它们之间切换,如果活动选项卡的 URL 与规则不匹配,或者在其他情况下激活它,我应该能够以某种方式禁用该小部件。小部件的状态(开/关)应该在加载页面和切换选项卡时更改。
我已经为此苦苦挣扎了一段时间,但仍然没有找到解决方案。
这就是我现在的处境:
// Activates on matching one of the site domains, but I also want to deactivate
// it when it does not match
var pageMod = require("page-mod");
pageMod.PageMod({
include: ["*.site1.com","*.site2.com"],
onAttach: function() {
alert("Widget activated!");
});
});
感谢您的帮助!
I want to be able to activate a widget if a url matches some pattern, but the problem is I also want to disable the widget when page-mod rule doesn't match the url.
So if I have few tabs open and if I switch between them I should be able to somehow disable the widget if an active tab's url doesn't match the rule, or in other case activate it. The state of widget(on/off) should be changed on loading pages and switching through tabs.
I've been struggling with this for a while and still haven't found a solution.
This is where I'm at right now:
// Activates on matching one of the site domains, but I also want to deactivate
// it when it does not match
var pageMod = require("page-mod");
pageMod.PageMod({
include: ["*.site1.com","*.site2.com"],
onAttach: function() {
alert("Widget activated!");
});
});
Thank you for any help!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
如果我正确理解您想要执行的操作,那么
page-mod
是错误的解决方案 - 您只想收听活动选项卡。使用选项卡
模块 为此,监听ready
(已加载新 URL)和activate
(活动选项卡已更改)事件:您的
updateActiveTab()
函数会需要检查tab.url
并激活或停用小部件。如果您想使用类似于为page-mod
指定的模式,那么您需要使用内部match-pattern
模块,如下所示:但是当然你可以只使用正则表达式或其他东西像这样测试
tab.url
,您不必使用match-pattern
模块。免责声明:代码示例只是为了使该方法更容易理解,尚未经过测试。
If I understand correctly what you are trying to do then
page-mod
is the wrong solution - you simply want to listen to the active tab. Usetabs
module for that, listen toready
(new URL loaded) andactivate
(active tab changed) events:Your
updateActiveTab()
function would need to checktab.url
and activate or deactivate the widget then. If you want to use patterns for that like the ones you specify forpage-mod
then you need to use the internalmatch-pattern
module, like this:But of course you can just use a regular expression or something like this to test
tab.url
, you don't have to use thematch-pattern
module.Disclaimer: The code examples are only there to make the approach easier to understand, they haven't been tested.