Chrome 扩展程序最初几次无法运行
我刚刚构建了一个 chrome 扩展。它使用以下内容更改上下文菜单 -
在内容脚本中
document.addEventListener("mousedown", function(event){
if(event.button == 2) {
if (isNaN(window.getSelection().toString())){
chrome.extension.sendRequest({cmd: "createStringMenu"});
}
else {
chrome.extension.sendRequest({cmd: "createNumberMenu"});
}
}
}, true);
在后台
chrome.extension.onRequest.addListener(function(request) {
if(request.cmd == "createStringMenu") {
chrome.contextMenus.removeAll(function() {
chrome.contextMenus.create({"title": "Send ' %s ' as SMS ", "contexts": ['selection'],"onclick": send_as_sms});
});
} else if(request.cmd == "createNumberMenu") {
chrome.contextMenus.removeAll(function() {
chrome.contextMenus.create({"title": "Send SMS to %s ", "contexts": ["selection"],"onclick": send_sms_to});
});
}
});
每当扩展程序首次在新打开的浏览器上运行或安装扩展程序时(以及网页被刷新),没有创建菜单。然后,就这样了。
我应该怎么办?可能是什么原因造成的?
I have just built a chrome extension. It changes the context menu using the following -
In content script
document.addEventListener("mousedown", function(event){
if(event.button == 2) {
if (isNaN(window.getSelection().toString())){
chrome.extension.sendRequest({cmd: "createStringMenu"});
}
else {
chrome.extension.sendRequest({cmd: "createNumberMenu"});
}
}
}, true);
In Background
chrome.extension.onRequest.addListener(function(request) {
if(request.cmd == "createStringMenu") {
chrome.contextMenus.removeAll(function() {
chrome.contextMenus.create({"title": "Send ' %s ' as SMS ", "contexts": ['selection'],"onclick": send_as_sms});
});
} else if(request.cmd == "createNumberMenu") {
chrome.contextMenus.removeAll(function() {
chrome.contextMenus.create({"title": "Send SMS to %s ", "contexts": ["selection"],"onclick": send_sms_to});
});
}
});
Whenever the extension runs for the first time either on a newly opened browser or when the extension is installed ( and web pages are refreshed) , no menu is created. then onwards, it does.
What should I do? What could be causing it?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
发生这种情况是因为上下文菜单出现在背景页面可以更改该上下文菜单的内容之前。第二次右键时,后台页面已经改变了第一次右键的内容,所以第二次才有效。
It happens because the context menu appears before the background page can change the content of that context menu. The second time you right-clicked, background page has already changed the content in the first time you pressed, so it works in the second time.