问题
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