简体   繁体   English

WebExtension在Firefox中无提示失败,但在Chrome中没有失败

[英]WebExtension failing silently in Firefox but not Chrome

I'm making a WebExtension for Chrome and Firefox that adds more information to GitHub. 我正在为Chrome和Firefox制作一个WebExtension,它将更多信息添加到GitHub。 It's supposed to be faster than existing extensions. 它应该比现有扩展更快。

I have my manifest set up like Mozilla's documentation recommends. 我已经按照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"
            ]
        }
    ]
}

When the page is loaded, the content scripts are injected. 加载页面后,将插入内容脚本。 The file github.js is a light wrapper around GitHub's API, and repository.js is the code to modfy the DOM of the main repository root page. github.js文件是GitHub API的轻量包装,而repository.js是修改主存储库根页面DOM的代码。

The most important code in there is this the preloader, which makes an API request while the page is loading and waits for both events to complete before adding to the DOM. 其中最重要的代码是预加载器,该预加载器在页面加载时发出API请求,并等待这两个事件完成后才添加到DOM。

While this current code works fine in Chrome, in Firefox it simply does nothing. 尽管此当前代码可在Chrome中正常运行,但在Firefox中却无能为力。 I tried testing it by putting console.log("I'm loaded!"); 我尝试通过将console.log("I'm loaded!"); in repository.js . repository.js Nothing is printed. 什么都没有打印。 Why is this code not working in Firefox? 为什么此代码在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!");

This was the solution. 这就是解决方案。

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

All that needed to happen was add permission for the extension to use GitHub's API. 所需要做的就是为扩展添加使用GitHub API的权限。 AFAIK, this is only required for content scripts using XHR. AFAIK,仅使用XHR的内容脚本才需要。

You need to go step by step and first ask yourself if script is really injected in the FF github page: remove everything thing from your contentScript, reload extension and check your FF console. 您需要逐步进行操作,首先要问自己是否确实在FF github页面中注入了脚本:从contentScript中删除所有内容,重新加载扩展并检查FF控制台。 If you see the log then start adding code progressively until it breaks, if not you have a problem with your build content. 如果看到日志,则开始逐步添加代码,直到日志破裂为止;否则,您的构建内容有问题。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM