gpt4 book ai didi

javascript - Microsoft Edge 扩展选项卡内容

转载 作者:行者123 更新时间:2023-12-02 14:13:09 29 4
gpt4 key购买 nike

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

如有任何帮助,我们将不胜感激。谢谢。

最佳答案

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

关于javascript - Microsoft Edge 扩展选项卡内容,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39320175/

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com