当我尝试在 chrome 中创建书签时,控制台出现错误 "browser is not defined"

I got a error "browser is not defined" in the console when I try create Bookmark in chrome

我正在尝试创建书签(在本例中 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:

中的代码时遇到了这个错误
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);

来自 :here 我得到了同样的错误。

更新问题:

我的manifest.json此刻:

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

知道吗,因为我有这个错误,我正在尝试创建一个文件夹并通过代码在我的书签中添加一个站点?

根据您最近的编辑和查看您的 manifest.json,您似乎缺少几个 permissions 字段,

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

{
    "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"
    ]
}

希望对您有所帮助!

除了 manifest 文件中缺少脚本调用外,在检查评论的要点 link 后似乎已解决(很好的捕获 !), however, both versions of the question's code are still missing a reference to the manifest file from within the html's <head> block, eg. something like <link rel="manifest" href="/manifest.webmanifest"> as suggested by Mozilla 可能只是让你过去问题。

为了完整起见,以下是我建议您对该 index.html 文件执行的操作...

<!DOCTYPE html>
<html>
<head>
    <link rel="manifest" href="/manifest.webmanifest">
    <title>test</title>
</head>
<body>


</body>
</html>

...我还建议重命名 manifest 文件以具有 webmanifest 扩展名,因为 reasons 与规范相关。