【问题标题】:Microsoft Edge Extension tab contentMicrosoft Edge 扩展选项卡内容
【发布时间】:2017-01-12 05:01:40
【问题描述】:

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

我的 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 中失败。

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

【问题讨论】:

  • 能否提供一个简单的示例,包括manifest.jsonbackground.jscontent.js?而你在background.js中调用了上述方法,对吧?

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


【解决方案1】:

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2021-05-13
    • 2019-08-06
    • 2020-06-02
    • 2016-02-24
    • 1970-01-01
    • 1970-01-01
    • 2021-09-24
    • 1970-01-01
    相关资源
    最近更新 更多