Search code examples
javascriptbookmarks

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


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?


Solution

  • 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!