Files
webextensions-examples/bookmark-it/background.js

70 lines
1.7 KiB
JavaScript

var currentTab;
var currentBookmark;
/*
* Updates the browserAction icon to reflect whether the current page
* is already bookmarked.
*/
function updateIcon() {
browser.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) {
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.browserAction.onClicked.addListener(toggleBookmark);
/*
* Switches currentTab and currentBookmark to reflect the currently active tab
*/
function updateActiveTab(tabs) {
function updateTab(tabs) {
if (tabs[0]) {
currentTab = tabs[0];
var searching = browser.bookmarks.search({url: currentTab.url});
searching.then((bookmarks) => {
currentBookmark = bookmarks[0];
updateIcon();
});
}
}
var gettingActiveTab = browser.tabs.query({active: true, currentWindow: true});
gettingActiveTab.then(updateTab);
}
// TODO listen for bookmarks.onCreated and bookmarks.onRemoved once Bug 1221764 lands
// listen to tab URL changes
browser.tabs.onUpdated.addListener(updateActiveTab);
// listen to tab switching
browser.tabs.onActivated.addListener(updateActiveTab);
// update when the extension loads initially
updateActiveTab();