WebExtension 在 Firefox 中静默失败但不是 Chrome

WebExtension failing silently in Firefox but not Chrome

我正在为 Chrome 和 Firefox 制作一个 WebExtension,以向 GitHub 添加更多信息。它应该比现有的扩展更快。

我按照 Mozilla 文档的建议设置了清单。

{
    "manifest_version": 2,
    "name": "GitHub Extended",
    "version": "0.0.1",
    "description": "Adds information to GitHub normally accessible only by the API.",
    "permissions": [
        "https://github.com/*"
    ],
    "content_scripts": [
        {
            "all_frames": true,
            "run_at": "document_start",
            "matches": [
                "https://github.com/*"
            ],
            "js": [
                "source/github.js",
                "source/repository.js"
            ]
        }
    ]
}

加载页面时,将注入内容脚本。文件 github.js 是 GitHub 的 API 的轻包装,repository.js 是修改主存储库根页面的 DOM 的代码。

其中最重要的代码是预加载器,它在页面加载时发出 API 请求,并等待两个事件完成,然后再添加到 DOM.

虽然这个 current code 在 Chrome 中工作正常,但在 Firefox 中它什么都不做。我尝试通过将 console.log("I'm loaded!"); 放入 repository.js 来测试它。没有打印任何内容。为什么此代码在 Firefox 中不起作用?

function beginPreload() {
    console.log("Test from preload scope!");

    let urlMatch = window.location.pathname.match(/\/([\w-]+)\/([\w-]+)/);
    console.log(urlMatch);

    Promise.all([
        getSortedReleases(urlMatch[1], urlMatch[2]),
        documentReady()
    ]).then((values) => {
        let releaseJson = values[0];

        let actionsEl = document.getElementsByClassName("pagehead-actions")[0];
        let dlCount = 0;

        for (release of releaseJson)
            for (asset of release.assets)
                dlCount += asset.download_count;

        let buttonEl = createDownloadButton(
            releaseJson[0].html_url,
            window.location.pathname + "/releases",
            formatNum(dlCount)
        );

        actionsEl.appendChild(buttonEl);
    });
}

beginPreload();
console.log("Test from global scope!");

你需要一步一步来,首先问问自己是否真的在 FF github 页面中注入了脚本:从你的 contentScript 中删除所有东西,重新加载扩展并检查你的 FF 控制台。如果您看到日志,然后开始逐步添加代码,直到它中断,否则您的构建内容有问题。

这就是解决方案。

"permissions": [
    "https://api.github.com/*"
]

所有需要做的就是为扩展程序添加使用 GitHub 的 API 的权限。据我所知,只有使用 XHR 的内容脚本才需要这样做。