内容脚本在Edge扩展中不起作用

时间:2017-01-12 07:40:17

标签: javascript microsoft-edge-extension

扩展名应该隐藏Netflix影片的视频播放器,但执行后没有任何影响。 var hidePlayer的代码如果在f12工具中执行,则可以成功隐藏视频播放器。

content.js

console.log("hi");

的manifest.json

{
"name": "Hello World",
"version": "1.0.0",
"description": "Simple Microsoft Edge Extension",
"author": "Hrishikesh Kale",
"icons": {
   "16": "icons/icon_16.png",
   "32": "icons/icon_32.png",
   "48": "icons/icon_48.png",
   "128": "icons/icon_128.png"
},
"browser_action": {
    "default_icon": {
        "20": "icons/icon_20.png",
        "25": "icons/icon_25.png",
        "30": "icons/icon_30.png",
        "40": "icons/icon_40.png"
    },
    "default_title": "Hello World"
},
"permissions": [
    "contextMenus",
    "tabs",
    "storage",
    "activeTab",
    "<all_urls>"
],
"minimum_edge_version": "37.14316.1000.0",
"background": {
"page": "background.html",
"persistent": true
  },
"content_scripts": [
{
  "matches": ["http://*/*", "https://*/*"],
  "css" : ["css/light.css"],
  "js": ["js/content.js"],
  "run_at": "document_end"
}]
}

background.js

browser.browserAction.onClicked.addListener(function(tab) {
    var hidePlayer = "(function () { "
                     +"    var panel =  document.getElementById('appMountPoint');"
                     +"    if (typeof (panel) != 'undefined' && panel != null) {"
                     +"        if (panel.style.display === 'none') {"
                     +"            panel.style.display = 'block';"
                     +"        } else {"
                     +"            panel.style.display = 'none';"
                     +"        }"
                     +"    }"
                     +"})();";
    browser.tabs.executeScript({
        code: hidePlayer
    });
});

background.html

<!DOCTYPE html>
<html>
<head>
    <title></title>
    <script type="text/javascript" src="js/background.js"></script>
</head>
<body>

</body>
</html>

我已附上项目here。要重现该错误,请在加载扩展程序之前在Edge浏览器中播放Netflix影片。

1 个答案:

答案 0 :(得分:1)

在测试代码时,我发现Edge的操作是间歇性的。 “重新加载”扩展名似乎没有100%有效。显示背景页面有时会进入Edge将打开扩展程序背景页面窗口的模式,但窗口为空白。我最终删除了扩展并重启了Edge多次。我的印象是,Edge扩展支持还没有为黄金时间做好准备(即它是错误的,需要开发工作)。

我发现在使用background scripts条目而不是page条目时,您的扩展程序的操作会更加一致。奇怪的是,使用script条目后返回使用page条目开始工作。

我将 manifest.json 更改为以下内容:

{
    "name": "Hello World",
    "version": "1.0.0",
    "description": "Simple Microsoft Edge Extension",
    "author": "Hrishikesh Kale",
    "icons": {
        "16": "icons/icon_16.png",
        "32": "icons/icon_32.png",
        "48": "icons/icon_48.png",
        "128": "icons/icon_128.png"
    },
    "browser_action": {
        "default_icon": {
            "20": "icons/icon_20.png",
            "25": "icons/icon_25.png",
            "30": "icons/icon_30.png",
            "40": "icons/icon_40.png"
        },
        "default_title": "Hello World"
    },
    "permissions": [
        "contextMenus",
        "tabs",
        "storage",
        "activeTab",
        "<all_urls>"
    ],
    "minimum_edge_version": "37.14316.1000.0",
    "background": {
        "scripts": ["js/background.js"],
        "persistent": true
    },
    "content_scripts": [{
        "matches": ["http://*/*", "https://*/*"],
        "js": ["js/content.js"],
        "run_at": "document_end"
    }]
}