Firefox插件收听单击扩展按钮

发布于 2025-01-20 15:44:25 字数 4030 浏览 3 评论 0 原文

我正在制作一个与每个网站兼容的深色主题插件。

我正在尝试使我的插件的按钮表现得像当前活动选项卡的开/关开关一样。


第一次尝试:

这是您可以从 doc

如果您提供弹出窗口,则在用户单击时打开弹出窗口 按钮,以及在弹出窗口中运行的JavaScript可以处理 用户与它的互动。如果您不提供弹出窗口,请单击 当事件被派往扩展名的背景脚本时 用户单击按钮。

因此,我应该收到一个“点击”事件,对吗?

清单

{
  "manifest_version": 2,
  "name": "DarkTheme",
  "version": "1.0",
  "description": "Create a dark theme for any webpage.",
  "icons": {
    "48": "dark.png"
  },
  "browser_action": {
    "default_icon": "dark.png",
    "default_title": "DarkTheme"
  },
  "content_scripts": [
    {
      "matches": [
        "<all_urls>"
      ],
      "js": [
        "dark.js"
      ]
    }
  ]
}

dark.js

document.addEventListener('click', () => console.log('clicked'))

没有收到任何事件。


第二次尝试

我找到在这里我应该使用 browser.browseraction 而不是。让我们尝试一下:

dark.js

browser.browserAction.onClicked.addListener(() => console.log('happened'));

什么也没收到...


第三尝试:

如果您仔细阅读文档,您会意识到,单击事件似乎只发送到背景脚本。因此,我们在这里走:

{
  "manifest_version": 2,
  "name": "DarkTheme",
  "version": "1.0",
  "description": "Create a dark theme for any webpage.",
  "icons": {
    "48": "dark.png"
  },
  "browser_action": {
    "default_icon": "dark.png",
    "default_title": "DarkTheme"
  },
  "background": {
    "scripts": [
      "button.js"
    ]
  },
  "content_scripts": [
    {
      "matches": [
        "<all_urls>"
      ],
      "js": [
        "dark.js"
      ]
    }
  ]
}

button.js

document.addEventListener('click', () => console.log('clicked'))

我看不到任何日志...


第四尝试:

我找到这个主题这表明我应该使用executecript。

在这里,我做了什么:

{
  "manifest_version": 2,
  "name": "DarkTheme",
  "version": "1.0",
  "description": "Create a dark theme for any webpage.",
  "icons": {
    "48": "dark.png"
  },
  "background": {
    "scripts": [
      "button.js"
    ]
  },
  "permissions": [
    "tabs"
  ],
  "browser_action": {
    "default_icon": "dark.png",
    "default_title": "DarkTheme"
  }
}
let disabled = false;
// On click, we disable the plugin
browser.browserAction.onClicked.addListener(tabId => {
  disabled = !disabled
  // This works
  browser.browserAction.setIcon({ path: disabled ? '/light.png' : '/dark.png' });
  // This works
  if (disabled) return browser.tabs.reload()
  // This doesn't work.
  else browser.tabs.executeScript(tabId, {
    file: '/dark.js',
    allFrames: true,
  });
});
// This should run the script without waiting for the user to press the button, but it doesn't work either.
browser.tabs.executeScript(undefined, {
  file: '/dark.js',
  allFrames: true,
})

以这种方式加载脚本似乎不起作用。我没有收到来自Dark.js的日志

Tabid可选

 整数。运行脚本的选项卡的ID。

默认为当前窗口的活动选项卡。
 

剩余的问题:

  1. 将脚本加载在每个网页上,就好像它在 content_scripts

    中一样
  2. 加载脚本时,禁用是false。

I'm making a dark theme plugin compatible with every websites.

I'm trying to make the button of my plugin behave like an on/off switch for the current active tab.


First attempt:

Here is what you can read from the doc

If you supply a popup, then the popup is opened when the user clicks
the button, and your JavaScript running in the popup can handle the
user's interaction with it. If you don't supply a popup, then a click
event is dispatched to your extension's background scripts when the
user clicks the button.

So, I should receive a 'click' event, right?

Manifest

{
  "manifest_version": 2,
  "name": "DarkTheme",
  "version": "1.0",
  "description": "Create a dark theme for any webpage.",
  "icons": {
    "48": "dark.png"
  },
  "browser_action": {
    "default_icon": "dark.png",
    "default_title": "DarkTheme"
  },
  "content_scripts": [
    {
      "matches": [
        "<all_urls>"
      ],
      "js": [
        "dark.js"
      ]
    }
  ]
}

dark.js

document.addEventListener('click', () => console.log('clicked'))

No event received.


Second attempt

I found here that I should be using browser.browserAction instead. Let's try:

dark.js

browser.browserAction.onClicked.addListener(() => console.log('happened'));

Nothing received...


Third attempt:

If you read the doc carefully, you realize that the click event seems to be sent only to background script. So here we go:

{
  "manifest_version": 2,
  "name": "DarkTheme",
  "version": "1.0",
  "description": "Create a dark theme for any webpage.",
  "icons": {
    "48": "dark.png"
  },
  "browser_action": {
    "default_icon": "dark.png",
    "default_title": "DarkTheme"
  },
  "background": {
    "scripts": [
      "button.js"
    ]
  },
  "content_scripts": [
    {
      "matches": [
        "<all_urls>"
      ],
      "js": [
        "dark.js"
      ]
    }
  ]
}

button.js

document.addEventListener('click', () => console.log('clicked'))

I don't see any log...


Fourth attempt:

I found This topic that suggest I should use executeScript.

Here id what I did:

{
  "manifest_version": 2,
  "name": "DarkTheme",
  "version": "1.0",
  "description": "Create a dark theme for any webpage.",
  "icons": {
    "48": "dark.png"
  },
  "background": {
    "scripts": [
      "button.js"
    ]
  },
  "permissions": [
    "tabs"
  ],
  "browser_action": {
    "default_icon": "dark.png",
    "default_title": "DarkTheme"
  }
}
let disabled = false;
// On click, we disable the plugin
browser.browserAction.onClicked.addListener(tabId => {
  disabled = !disabled
  // This works
  browser.browserAction.setIcon({ path: disabled ? '/light.png' : '/dark.png' });
  // This works
  if (disabled) return browser.tabs.reload()
  // This doesn't work.
  else browser.tabs.executeScript(tabId, {
    file: '/dark.js',
    allFrames: true,
  });
});
// This should run the script without waiting for the user to press the button, but it doesn't work either.
browser.tabs.executeScript(undefined, {
  file: '/dark.js',
  allFrames: true,
})

Loading the script this way doesn't seem to work. I receive no log from dark.js.

tabId Optional

integer. The ID of the tab in which to run the script.

Defaults to the active tab of the current window.

Remaining problems:

  1. Loading the script on every web page as if it was in content_scripts

  2. Load script when disabled is false.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(1

梦在深巷 2025-01-27 15:44:25

我终于做到了!

清单

{
  "manifest_version": 2,
  "name": "DarkTheme",
  "version": "1.0",
  "description": "Create a dark theme for any webpage.",
  "icons": {
    "48": "dark.png"
  },
  "background": {
    "scripts": [
      "button.js"
    ]
  },
  "permissions": [
    "tabs"
  ],
  "browser_action": {
    "default_icon": "dark.png",
    "default_title": "DarkTheme"
  },
  "content_scripts": [
    {
      "matches": [
        "<all_urls>"
      ],
      "js": [
        "dark.js"
      ]
    }
  ]
}

let disabled = false;
browser.browserAction.onClicked.addListener(tab => {
  disabled = !disabled
  browser.browserAction.setIcon({ path: disabled ? '/light.png' : '/dark.png' });
  if (disabled) return browser.tabs.reload(tab.id).then(() => {
    browser.tabs.executeScript(tab.id, {
      code: 'window.disabled = true;',
      allFrames: true,
    })
  })
  else browser.tabs.executeScript(tab.id, {
    code: 'window.disabled = false;'
  }).then(() => {
    browser.tabs.executeScript(tab.id, {
      file: '/dark.js',
      allFrames: true,
    });
  })
});

这是 禁用您的插件。

I finally did it!

Here is the manifest.json

{
  "manifest_version": 2,
  "name": "DarkTheme",
  "version": "1.0",
  "description": "Create a dark theme for any webpage.",
  "icons": {
    "48": "dark.png"
  },
  "background": {
    "scripts": [
      "button.js"
    ]
  },
  "permissions": [
    "tabs"
  ],
  "browser_action": {
    "default_icon": "dark.png",
    "default_title": "DarkTheme"
  },
  "content_scripts": [
    {
      "matches": [
        "<all_urls>"
      ],
      "js": [
        "dark.js"
      ]
    }
  ]
}

And here is the backgroung script:

let disabled = false;
browser.browserAction.onClicked.addListener(tab => {
  disabled = !disabled
  browser.browserAction.setIcon({ path: disabled ? '/light.png' : '/dark.png' });
  if (disabled) return browser.tabs.reload(tab.id).then(() => {
    browser.tabs.executeScript(tab.id, {
      code: 'window.disabled = true;',
      allFrames: true,
    })
  })
  else browser.tabs.executeScript(tab.id, {
    code: 'window.disabled = false;'
  }).then(() => {
    browser.tabs.executeScript(tab.id, {
      file: '/dark.js',
      allFrames: true,
    });
  })
});

In the dark.js script, you only have to do if (window.disabled) return before your code to disable your plugin.

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