我收到一个错误“浏览器未定义".当我尝试在Chrome中创建书签时在控制台中 [英] I got a error "browser is not defined" in the console when I try create Bookmark in chrome

查看:108
本文介绍了我收到一个错误“浏览器未定义".当我尝试在Chrome中创建书签时在控制台中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试创建书签(在这种情况下为chrome)

I am trying create a bookmarks (on this case in chrome)

API/bookmarks/create

我的代码是:

	function onCreated(node) {
  console.log(node);
}

var createBookmark = browser.bookmarks.create({
  title: "bookmarks.create() on MDN",
  url: "https://developer.mozilla.org/Add-ons/WebExtensions/API/bookmarks/create"
});

createBookmark.then(onCreated);

<!DOCTYPE html>
<html>
<head>
	<title>test</title>
</head>
<body>

<p>text example</p>
</body>
</html>

在chrome中运行代码时出现此错误:

I got this error when I run the code in chrome:

Uncaught ReferenceError: browser is not defined

我运行其他代码:

function onFulfilled(bookmarkItems) {
  if (bookmarkItems.length) {
    console.log("active tab is bookmarked");
  } else {
    console.log("active tab is not bookmarked");
  }
}

function onRejected(error) {
  console.log(`An error: ${error}`);
}

function checkActiveTab(tab) {
  var searching = browser.bookmarks.search({url: tab.url});
  searching.then(onFulfilled, onRejected);
}

browser.browserAction.onClicked.addListener(checkActiveTab);

来自:此处同样的错误.

更新问题:

此刻我的manifest.json:

my manifest.json in this moment:

 {
    "name": "add site random",
    "version": "1.0",
    "description": "this is my first extension in production",
    "manifest_version": 2
  }

有什么主意,因为我遇到此错误,我正在尝试创建一个文件夹,并根据代码在书签中添加一个网站?

any idea because I have this error I am trying create a folder and add a site in my bookmarks from the code?

推荐答案

从您最近的编辑中,通过查看您的manifest.json,您似乎缺少了几个permissions字段,

From your recent edit and by looking at your manifest.json it looks like you are missing few permissions field,

因此像这样更新您的manifest.json(然后重新打包一个新扩展名)

Hence update your manifest.json like this, (then package a new extension afresh)

{
    "name": "add site random",
    "version": "1.0",
    "description": "this is my first extension in production",
    "manifest_version": 2,
    "background": {
      "scripts": ["YOUR_BACKGROUND_SCRIPT_NAME.js"]
    }, 
    "permissions": [
        "notifications",
        "activeTab"
    ]
}

希望这会有所帮助!

这篇关于我收到一个错误“浏览器未定义".当我尝试在Chrome中创建书签时在控制台中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆