最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

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

programmeradmin0浏览0评论

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: ""
});

createBookmark.then(onCreated);
<!DOCTYPE html>
<html>
<head>
	<title>test</title>
</head>
<body>

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

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/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?

Share Improve this question edited Jul 23, 2018 at 7:01 asked Jul 23, 2018 at 6:49 user9983619user9983619 5
  • Hi Simon, If I'm not wrong. You are trying to develop a chrome extension right? – David R Commented Jul 23, 2018 at 6:53
  • yep, I try create a chrome extension that is not my full code from my project just a part from their , but I am trying create in that part a code that it permit me createa folder in my bookmarks from chrome and add a site my extension its work is add sites when users give click in a buttom – user9983619 Commented Jul 23, 2018 at 6:56
  • Can you please update your post with your manifest.json file content? – David R Commented Jul 23, 2018 at 6:56
  • yep give a moment i will going to edit the question – user9983619 Commented Jul 23, 2018 at 6:58
  • @DavidR already – user9983619 Commented Jul 23, 2018 at 7:01
Add a ment  | 

2 Answers 2

Reset to default 2

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!

Aside from missing script calls within the manifest file, which after checking the mented 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 pleteness 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.

与本文相关的文章

发布评论

评论列表(0)

  1. 暂无评论