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

♀尐吖头ヾ 提交于 2020-05-31 03:53:29

问题


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

API/bookmarks/create

my code is :

	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>

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

Uncaught ReferenceError: browser is not defined

I run other code this :

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);

from :here I got the same error.

Update question :

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?


回答1:


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

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

Hope this helps!




回答2:


Aside from missing script calls within the manifest file, which after checking the commented gist link appears to have been resolved (good catch @DavidR!), 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 might just allow ya past the current issues.

For completeness sake the following is what I perpose you do to that index.html file...

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


</body>
</html>

... and I'll also suggest renaming the manifest file to have the webmanifest extension because of reasons related to the specification.



来源:https://stackoverflow.com/questions/51473031/i-got-a-error-browser-is-not-defined-in-the-console-when-i-try-create-bookmark

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!