Chrome 扩展:为什么我在“后台页面”的控制台中出现此错误?

发布于 2025-01-08 18:04:19 字数 1422 浏览 1 评论 0原文

我不明白,但似乎无法识别 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

等风也等你 2025-01-15 18:04:19

除了已经说明的内容之外,如果您仍然发现 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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文