firefox addon page-mod - 当 url 不匹配时

发布于 2025-01-03 01:12:59 字数 587 浏览 4 评论 0原文

我希望能够在 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

无可置疑 2025-01-10 01:12:59

如果我正确理解您想要执行的操作,那么 page-mod 是错误的解决方案 - 您只想收听活动选项卡。使用 选项卡 模块 为此,监听 ready (已加载新 URL)和 activate (活动选项卡已更改)事件:

var tabs = require("tabs");
tabs.on("ready", function(tab)
{
  if (tab == tabs.activeTab)
    updateActiveTab(tab);
});
tabs.on("activate", function(tab)
{
  updateActiveTab(tab);
});

您的 updateActiveTab() 函数会需要检查 tab.url 并激活或停用小部件。如果您想使用类似于为 page-mod 指定的模式,那么您需要使用内部 match-pattern 模块,如下所示:

var {MatchPattern} = require("match-pattern");
var patterns = [
  new MatchPattern("*.site1.com"),
  new MatchPattern("*.site2.com")
];

function updateActiveTab(tab)
{
  var matches = false;
  for (var i = 0; i < patterns.length; i++)
    if (patterns[i].test(tab.url))
      matches = true;
  if (matches)
    activateWidget();
  else
    deactivateWidget();
}

但是当然你可以只使用正则表达式或其他东西像这样测试 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. Use tabs module for that, listen to ready (new URL loaded) and activate (active tab changed) events:

var tabs = require("tabs");
tabs.on("ready", function(tab)
{
  if (tab == tabs.activeTab)
    updateActiveTab(tab);
});
tabs.on("activate", function(tab)
{
  updateActiveTab(tab);
});

Your updateActiveTab() function would need to check tab.url and activate or deactivate the widget then. If you want to use patterns for that like the ones you specify for page-mod then you need to use the internal match-pattern module, like this:

var {MatchPattern} = require("match-pattern");
var patterns = [
  new MatchPattern("*.site1.com"),
  new MatchPattern("*.site2.com")
];

function updateActiveTab(tab)
{
  var matches = false;
  for (var i = 0; i < patterns.length; i++)
    if (patterns[i].test(tab.url))
      matches = true;
  if (matches)
    activateWidget();
  else
    deactivateWidget();
}

But of course you can just use a regular expression or something like this to test tab.url, you don't have to use the match-pattern module.

Disclaimer: The code examples are only there to make the approach easier to understand, they haven't been tested.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文