From dead97bc3837764a5228f5e1d573b944a5923a7a Mon Sep 17 00:00:00 2001 From: wbamberg Date: Tue, 9 May 2017 14:23:19 -0700 Subject: [PATCH] Rewrite of bookmark-it, fixing issue 182 and some other problems (#186) * Rewrite of bookmark-it, fixing issue 182 and some other problems * Catch rejected promises * Update with review comments, and some more fixes * Revise fix to be much closer to original * Remove extra semicolon --- bookmark-it/README.md | 2 -- bookmark-it/background.js | 38 +++++++++++++++++++++++++------------- bookmark-it/manifest.json | 2 +- 3 files changed, 26 insertions(+), 16 deletions(-) diff --git a/bookmark-it/README.md b/bookmark-it/README.md index 28c87e0..e2d90cb 100644 --- a/bookmark-it/README.md +++ b/bookmark-it/README.md @@ -1,7 +1,5 @@ # 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. diff --git a/bookmark-it/background.js b/bookmark-it/background.js index 171beaf..9cc9a48 100644 --- a/bookmark-it/background.js +++ b/bookmark-it/background.js @@ -24,14 +24,8 @@ function updateIcon() { function toggleBookmark() { if (currentBookmark) { browser.bookmarks.remove(currentBookmark.id); - currentBookmark = null; - updateIcon(); } else { - var creating = browser.bookmarks.create({title: currentTab.title, url: currentTab.url}); - creating.then(function(bookmark) { - currentBookmark = bookmark; - updateIcon(); - }); + browser.bookmarks.create({title: currentTab.title, url: currentTab.url}); } } @@ -42,14 +36,25 @@ browser.browserAction.onClicked.addListener(toggleBookmark); */ function updateActiveTab(tabs) { + function isSupportedProtocol(urlString) { + var supportedProtocols = ["https:", "http:", "ftp:", "file:"]; + var url = document.createElement('a'); + url.href = urlString; + return supportedProtocols.indexOf(url.protocol) != -1; + } + function updateTab(tabs) { if (tabs[0]) { currentTab = tabs[0]; - var searching = browser.bookmarks.search({url: currentTab.url}); - searching.then((bookmarks) => { - currentBookmark = bookmarks[0]; - updateIcon(); - }); + if (isSupportedProtocol(currentTab.url)) { + var searching = browser.bookmarks.search({url: currentTab.url}); + searching.then((bookmarks) => { + currentBookmark = bookmarks[0]; + updateIcon(); + }); + } else { + console.log(`Bookmark it! does not support the '${currentTab.url}' URL.`) + } } } @@ -57,7 +62,11 @@ function updateActiveTab(tabs) { gettingActiveTab.then(updateTab); } -// TODO listen for bookmarks.onCreated and bookmarks.onRemoved once Bug 1221764 lands +// listen for bookmarks being created +browser.bookmarks.onCreated.addListener(updateActiveTab); + +// listen for bookmarks being removed +browser.bookmarks.onRemoved.addListener(updateActiveTab); // listen to tab URL changes browser.tabs.onUpdated.addListener(updateActiveTab); @@ -65,5 +74,8 @@ browser.tabs.onUpdated.addListener(updateActiveTab); // listen to tab switching browser.tabs.onActivated.addListener(updateActiveTab); +// listen for window switching +browser.windows.onFocusChanged.addListener(updateActiveTab); + // update when the extension loads initially updateActiveTab(); diff --git a/bookmark-it/manifest.json b/bookmark-it/manifest.json index 06c152c..017a650 100644 --- a/bookmark-it/manifest.json +++ b/bookmark-it/manifest.json @@ -1,7 +1,7 @@ { "manifest_version": 2, "name": "Bookmark it!", - "version": "1.0", + "version": "1.1", "description": "A simple bookmark button", "homepage_url": "https://github.com/mdn/webextensions-examples/tree/master/bookmark-it", "icons": {