Microsoft Edge Extension tab content

我们两清 提交于 2019-12-25 04:27:08

问题


I am exploring a bit on Microsoft Edge extension development. Basically I am trying to read HTML content of a tab. Below is code snippet from my solution.

My manifest.json looks likes below

{
    "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"]
} 

My index.js looks like below

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);
    });
}

Function onCodeCall runs on click of extension button.

I am not getting any error and also my callback function does not hit. This code works fine in Chrome but just fails in Edge.

Any help would be appreciated. Thanks.


回答1:


tabs.sendMessage can only be called in extension context, such as background page. However, you are calling it in content scripts, which obviously won't work, no matter in Microsoft Edge or Chrome.



来源:https://stackoverflow.com/questions/39320175/microsoft-edge-extension-tab-content

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!