Microsoft Edge Extension选项卡内容

时间:2016-09-04 18:27:10

标签: javascript browser microsoft-edge microsoft-edge-extension

我正在探索Microsoft Edge扩展开发。基本上我正在尝试阅读选项卡的HTML内容。以下是我的解决方案中的代码段。

我的manifest.json看起来像下面

{
    "name" : "HTML Reader",
    "version" : "1.0.0.0",
    "author" : "Stack Memory",
    "browser_action" : 
    {
        "default_icon" : 
        {
            "20" : "icon_20.png",
            "40" : "icon_40.png"
        },
        "default_title" : "Sample extension",
        "default_popup" : "index.html"
    },
    "content_scripts" : [{
            "js" : ["js/index.js"],
            "matches" : ["*://*/*"]
        }
    ],

    "content_security_policy" : "script-src 'self'; object-src 'self'",
    "default_locale" : "en",
    "description" : "This is a sample extension that illustrates the JSON manifest schema",

    "permissions" : 
    [
        "*://*/*", "notifications", "cookies", "tabs", "storage", "contextMenus", "background"
    ],
    "icons" : {
        "128" : "icon_128.png",
        "16" : "icon_16.png",
        "48" : "icon_48.png"
    },
    "minimum_edge_version" : "33.14281.1000.0",
    "web_accessible_resources" : ["icon_48.png"]
} 

我的index.js如下所示

function getDomObject(tab) {
    browser.tabs.sendMessage(tab.id, {
        method: 'getDomContent'
    },function(response) {
        if (browser.runtime.lastError) {
            console.log("Error: ", browser.runtime.lastError);
        } else {
            alert(response.innerText);
        }
    });
}

function onCodeCall(){
    browser.tabs.query({currentWindow: true, active: true}, function(tabs){
        var tab = tabs[0];
        getDomObject(tab);
    });
}

单击扩展按钮时会运行功能onCodeCall

我没有收到任何错误,而且我的回调函数也没有出现。此代码在Chrome中运行良好,但在Edge中失败。

任何帮助将不胜感激。 感谢。

1 个答案:

答案 0 :(得分:2)

tabs.sendMessage只能在扩展上下文中调用,例如后台页面。但是,您在内容脚本中调用它,无论在Microsoft Edge还是Chrome中,这显然无法工作。