mirror of
https://github.com/mdn/webextensions-examples.git
synced 2026-04-16 06:18:35 +02:00
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
This commit is contained in:
@@ -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.
|
||||
|
||||
@@ -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();
|
||||
|
||||
@@ -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": {
|
||||
|
||||
Reference in New Issue
Block a user