0

我正在探索 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 中失败。

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

4

1 回答 1

2

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

于 2016-09-05T09:54:28.153 回答