我一直试图通过在Edge平台上开发一些扩展来弄清自己的手。到目前为止进展顺利,直到我遇到了障碍。我的后台脚本中有一个生成上下文菜单的方法,单击该方法会向该上下文脚本发送一条消息。一切正常,除了上下文脚本没有显示调用事件侦听器的迹象。
这是我的代码: manifest.json:
{
"name":"Edge",
"author":"Auth",
"version":"1.0",
"description":"none",
"permissions":["*://*/","tabs","bookmarks","unlimitedStorage","contextMenus"],
"browser_action":{
"default_icon":{
"20":"images/Edge.png",
"40":"images/Edge40.png"
},
"default_title":"Edge"
},
"background":{
"persistent":true,
"scripts":["back.js"]
},
"content_scripts":[{
"matches":["<all_urls>"],
"js":["cs.js"],
"run_at":"document_end"
}]
}
后台脚本:
browser.contextMenus.create({
id: "Show Notes",
title: "Show Notes",
contexts: ['link']
});
browser.contextMenus.create({
id:"Make Notes",
title:"Make Notes",
contexts:['all']
});
browser.contextMenus.onClicked.addListener(function(info, tab) {
alert("s")
alert(info.menuItemId)
alert(info.linkUrl)
browser.tabs.query({active: true}, function(tabs){
browser.tabs.getCurrent(function(tab){
browser.tabs.sendMessage(tab.id,{
"id":info.menuItemId,
"link":info.linkUrl
})
})
})
alert("sent")
});
内容脚本:
browser.runtime.onMessage.addListener(lis)
function lis(request, sender, sendResponse) {
alert("msg")
if(request.id=="Show Notes"){
alert("received")
note.style.display="block"
}
if(request.id=="Make Notes"){
note.style.display="block"
alert("received")
}
return true
}
后台脚本中的所有警报消息都可以正常工作,但是内容脚本甚至无法弹出单个窗口。
我已阅读并尝试实现许多答案,但其中一些没有用。 可能出什么问题了? 谢谢!
答案 0 :(得分:0)
As Edge doesn't support Promises, you would have to rely on callback mechanism. Please synchronize your tabs.query
or tabs.getCurrent
with tabs.sendMessage
. For the given code you will be seeing an error in the background page's console stating "id as undefined". Synchronizing the above two methods would help fix this. Also the bookmark
permission in the manifest.json
is useless as Edge currently doesn't support bookmarks. (Just wanted you to know this incase you missed it).