diff --git a/bookmark-it/README.md b/bookmark-it/README.md new file mode 100644 index 0000000..28c87e0 --- /dev/null +++ b/bookmark-it/README.md @@ -0,0 +1,20 @@ +# bookmark-it + +> This example uses APIs that are available from Firefox 47 onwards. + +## What it does + +Displays a simple button in the menu bar that toggles a bookmark for the currently active tab. + +To display the button, the extension registers a [browserAction](https://developer.mozilla.org/en-US/Add-ons/WebExtensions/API/browserAction) in the manifest. + +A background script will listen for tab events and update the browserAction icon correspondingly. It also listens for `browserAction.onClicked` events to create or remove a bookmark when the user has clicked the icon. + +## What it shows + +* how to use the various `bookmarks` functions + * create a bookmark + * remove a bookmark + * search bookmarks by url +* how to register a browserAction +* how to listen for tab changes diff --git a/bookmark-it/background.js b/bookmark-it/background.js new file mode 100644 index 0000000..6b07a58 --- /dev/null +++ b/bookmark-it/background.js @@ -0,0 +1,64 @@ +var currentTab; +var currentBookmark; + +/* + * Updates the browserAction icon to reflect whether the current page + * is already bookmarked. + */ +function updateIcon() { + chrome.browserAction.setIcon({ + path: currentBookmark ? { + 19: "icons/star-filled-19.png", + 38: "icons/star-filled-38.png" + } : { + 19: "icons/star-empty-19.png", + 38: "icons/star-empty-38.png" + }, + tabId: currentTab.id + }); +} + +/* + * Add or remove the bookmark on the current page. + */ +function toggleBookmark() { + if (currentBookmark) { + chrome.bookmarks.remove(currentBookmark.id); + currentBookmark = null; + updateIcon(); + } else { + chrome.bookmarks.create({title: currentTab.title, url: currentTab.url}, function(bookmark) { + currentBookmark = bookmark; + updateIcon(); + }); + } +} + +chrome.browserAction.onClicked.addListener(toggleBookmark); + +/* + * Switches currentTab and currentBookmark to reflect the currently active tab + */ +function updateTab() { + chrome.tabs.query({active: true, currentWindow: true}, function(tabs) { + if (tabs[0]) { + currentTab = tabs[0]; + + chrome.bookmarks.search({url: currentTab.url}, (bookmarks) => { + currentBookmark = bookmarks[0]; + updateIcon(); + }); + } + }); +} + +// TODO listen for bookmarks.onCreated and bookmarks.onRemoved once Bug 1221764 lands + +// listen to tab URL changes +chrome.tabs.onUpdated.addListener(updateTab); + +// listen to tab switching +chrome.tabs.onActivated.addListener(updateTab); + +// update when the extension loads initially +updateTab(); diff --git a/bookmark-it/icons/LICENSE b/bookmark-it/icons/LICENSE new file mode 100644 index 0000000..4014f4c --- /dev/null +++ b/bookmark-it/icons/LICENSE @@ -0,0 +1 @@ +All images in this folder were created by Johann Hofmann. The creator waives all rights to the images under the CC0 Public Domain Dedication. https://creativecommons.org/publicdomain/zero/1.0/ diff --git a/bookmark-it/icons/bookmark-it.png b/bookmark-it/icons/bookmark-it.png new file mode 100644 index 0000000..6b41a93 Binary files /dev/null and b/bookmark-it/icons/bookmark-it.png differ diff --git a/bookmark-it/icons/bookmark-it@2x.png b/bookmark-it/icons/bookmark-it@2x.png new file mode 100644 index 0000000..95ac86d Binary files /dev/null and b/bookmark-it/icons/bookmark-it@2x.png differ diff --git a/bookmark-it/icons/star-empty-19.png b/bookmark-it/icons/star-empty-19.png new file mode 100644 index 0000000..6321a6a Binary files /dev/null and b/bookmark-it/icons/star-empty-19.png differ diff --git a/bookmark-it/icons/star-empty-38.png b/bookmark-it/icons/star-empty-38.png new file mode 100644 index 0000000..46a4d71 Binary files /dev/null and b/bookmark-it/icons/star-empty-38.png differ diff --git a/bookmark-it/icons/star-filled-19.png b/bookmark-it/icons/star-filled-19.png new file mode 100644 index 0000000..bee01b9 Binary files /dev/null and b/bookmark-it/icons/star-filled-19.png differ diff --git a/bookmark-it/icons/star-filled-38.png b/bookmark-it/icons/star-filled-38.png new file mode 100644 index 0000000..f8edd21 Binary files /dev/null and b/bookmark-it/icons/star-filled-38.png differ diff --git a/bookmark-it/manifest.json b/bookmark-it/manifest.json new file mode 100644 index 0000000..99a8eae --- /dev/null +++ b/bookmark-it/manifest.json @@ -0,0 +1,33 @@ +{ + "manifest_version": 2, + "name": "Bookmark it!", + "version": "1.0", + "description": "A simple bookmark button", + "homepage_url": "https://github.com/mdn/webextensions-examples/tree/master/bookmark-it", + "icons": { + "48": "icons/bookmark-it.png", + "96": "icons/bookmark-it@2x.png" + }, + + "applications": { + "gecko": { + "id": "bookmark-it-extension@mozilla.org", + "strict_min_version": "47.0a1" + } + }, + + "permissions": [ + "bookmarks", + "tabs" + ], + + "browser_action": { + "default_icon": "icons/star-empty-38.png", + "default_title": "Bookmark it!" + }, + + "background": { + "scripts": ["background.js"] + } + +}