Firefox 附加组件 运行 可以用于非 HTML 请求,例如图像吗?

Can Firefox add-ons run for non-HTML requests such as images?

我正在按照有关为 Firefox 开发 WebExtension 的说明进行操作。我让它为 HTML 页工作。但是,当请求的 "page" 是图像时,我也希望我的脚本 运行 。这可能吗?

这是我的:

manifest.json:

{
    "manifest_version": 2,
    "name": "Test",
    "version": "1.0",
    "applications": {
        "gecko": {
            "id": "alert@example.com",
            "strict_min_version": "42.0",
            "strict_max_version": "50.*",
            "update_url": "https://example.com/updates.json"
        }
    },
    "description": "Just does an alert.",

    "icons": {
        "48": "icons/border-48.png"
    },

    "content_scripts": [
        {
            "matches": ["<all_urls>"],
            "js": ["alert.js"]
        }
    ]

}

alert.js:

alert("ok");

按照说明安装插件后(转到 "about: debugging"),我收到所有 HTML 页面的 JavaScript 警报。但是如果我导航到图像(例如通过右键单击图像并选择 "View Image"),则不会出现警报。

这是 Firefox 中的一个错误,在 Firefox 54 中大部分但未完全解决(目前 Firefox Beta). Your script (mostly) works as expected (shows the alert when viewing an image from the context menu selection "View Image") in Firefox Beta and Firefox Developer Edition

我在测试 Beta 版和 Developer Edition 时遇到的问题是,当使用前进和后退按钮离开和返回时,内容脚本 间歇性地 没有注入到“查看图片”页面。

可以确定您的内容脚本是使用 tabs.executeScript(), and the events from the webNavigation, webRequest and tabs API 加载的。但是,对任何页面上的任何内容脚本进行通用操作是......复杂的(完整的通用实现需要的代码比 Stack Overflow 答案中允许的最大字符数更多)。

WebExtensions 正在积极开发中

WebExtensions API 仍在开发中。每个版本的 Firefox 都改进了工作。如果您遇到问题,您应该使用 Firefox Developer Edition, or Firefox Nightly 测试您的 WebExtension add-on。您还应该仔细记下您希望使用的功能需要哪个版本的 Firefox。此信息包含在 MDN 文档页面的“浏览器兼容性”部分。