Chrome 扩展:为什么我在“后台页面”的控制台中出现此错误?
我不明白,但似乎无法识别 api 的谷歌浏览器,也许。
清单:
{
"name": "First Extension",
"version": "1.0",
"description": "The first extensione tha I made",
"permissions":["contextMenus"],
"background_page": "background_page.html"
}
背景页面:
<!doctype html>
<html>
<head>
<title>Background Page</title>
<script src="script.js"></script>
</head>
<body>
</body>
</html>
Javascript (script.js):
if (window.webkitNotifications) {
console.log("Notifications are supported!");
}
else {
console.log("Notifications are not supported for this Browser/OS version yet.");
}
/////////// BROWSER CONTEXT MENU ////////////////
var arr_context = ["all", "page", "frame", "selection", "link", "editable", "image", "video", "audio"];
for(i=0; i<arr_context.length; i++){
var title = arr_context[i];
var menu1 = chrome.contextMenus.create({"type":"normal", "title":"Menu "+title, "contexts":arr_context, "onclick":callBack });
}
function callBack(info, tab){ console.log(info.menuItemId+"; URL: "+tab.url); }
函数“callBack(info, tab)”不写任何内容。后台页面出现错误,错误为:Uncaught TypeError: Cannot call method 'create' of undefined
。
所有的文件只有这些。
好的,我解决了,在互联网上阅读了错误,因为我没有访问该页面 从“扩展”选项卡,但比双击background_page.html...“很重吗?” -.-°
I don't understand but it seems like not recognize api's google chrome, maybe.
Manifest:
{
"name": "First Extension",
"version": "1.0",
"description": "The first extensione tha I made",
"permissions":["contextMenus"],
"background_page": "background_page.html"
}
Background page:
<!doctype html>
<html>
<head>
<title>Background Page</title>
<script src="script.js"></script>
</head>
<body>
</body>
</html>
Javascript (script.js):
if (window.webkitNotifications) {
console.log("Notifications are supported!");
}
else {
console.log("Notifications are not supported for this Browser/OS version yet.");
}
/////////// BROWSER CONTEXT MENU ////////////////
var arr_context = ["all", "page", "frame", "selection", "link", "editable", "image", "video", "audio"];
for(i=0; i<arr_context.length; i++){
var title = arr_context[i];
var menu1 = chrome.contextMenus.create({"type":"normal", "title":"Menu "+title, "contexts":arr_context, "onclick":callBack });
}
function callBack(info, tab){ console.log(info.menuItemId+"; URL: "+tab.url); }
Function "callBack(info, tab)" don't write anything. In background page there's an error, ERROR is: Uncaught TypeError: Cannot call method 'create' of undefined
.
The all files are only these.
OK, I solved, read the mistake was made, on internet because I was not accessing the page
from Extension Tab, but than double click on background_page.html.... Is' much heavy it? -.-°
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
除了已经说明的内容之外,如果您仍然发现 chrome.contextMenus 为“未定义”,请记住卸载并重新安装您的扩展!可能会发生一些奇怪的缓存;有些东西会在您开发时更新(即您可以更改代码,然后在重新触发扩展时看到更改),而另一些则需要完全重新安装 - HTH。
In addition to what has already been stated, if you still find that chrome.contextMenus is 'undefined', remember to uninstall-reinstall your extension! There may be some odd caching occurring; some things update while your developing (ie. you can change code then see changes on retriggering your extension), and others require a complete reinstall - HTH.