|
| 1 | +// Polyfill globalThis.browser instead of using globalThis.chrome because FireFox's |
| 2 | +// globalThis.chrome APIs don't yet support the promise-based APIs from Manifest v3. |
| 3 | +// Also, use globalThis because window isn't defined in service workers. |
| 4 | +if (!globalThis.browser) { |
| 5 | + globalThis.browser = chrome; |
| 6 | +} |
| 7 | + |
| 8 | +browser.omnibox.onInputEntered.addListener(handleOmniboxInput); |
| 9 | + |
| 10 | +function handleOmniboxInput(rawText) { |
| 11 | + |
| 12 | + const text = rawText.trim(); |
| 13 | + if (!text.length) { |
| 14 | + return; |
| 15 | + } |
| 16 | + const prefix = text[0]; |
| 17 | + switch (prefix) { |
| 18 | + case '+': |
| 19 | + saveBookmark(text); |
| 20 | + break; |
| 21 | + case '-': |
| 22 | + removeBookmark(text); |
| 23 | + break; |
| 24 | + default: |
| 25 | + openBookmark(text); |
| 26 | + } |
| 27 | +} |
| 28 | + |
| 29 | +async function saveBookmark(text) { |
| 30 | + |
| 31 | + const name = text.substr(1); |
| 32 | + if (!name) { |
| 33 | + notifyNameMissing('add', '+'); |
| 34 | + return; |
| 35 | + } |
| 36 | + const tabs = await browser.tabs.query({ active: true, currentWindow: true }); |
| 37 | + const { url } = tabs[0]; |
| 38 | + const newBookmarkCreated = await Bookmarks.save(name, url); |
| 39 | + const verb = newBookmarkCreated ? 'added' : 'updated'; |
| 40 | + notify(`Bookmark ${verb}`, `Successfully ${verb} the bookmark "${name}".`); |
| 41 | +} |
| 42 | + |
| 43 | +async function removeBookmark(text) { |
| 44 | + |
| 45 | + const name = text.substr(1); |
| 46 | + if (!name) { |
| 47 | + notifyNameMissing('remove', '-'); |
| 48 | + return; |
| 49 | + } |
| 50 | + const matchingBookmarkRemoved = await Bookmarks.remove(name); |
| 51 | + if (matchingBookmarkRemoved) { |
| 52 | + notify('Bookmark removed', `Successfully removed the bookmark "${name}".`); |
| 53 | + return; |
| 54 | + } |
| 55 | + notifyNotFound(name); |
| 56 | +} |
| 57 | + |
| 58 | +async function openBookmark(name) { |
| 59 | + |
| 60 | + const url = await Bookmarks.get(name); |
| 61 | + if (url) { |
| 62 | + browser.tabs.create({ url }); |
| 63 | + return; |
| 64 | + } |
| 65 | + notifyNotFound(name); |
| 66 | +} |
| 67 | + |
| 68 | +function notify(title, message) { |
| 69 | + |
| 70 | + browser.notifications.create('', { |
| 71 | + type: 'basic', |
| 72 | + title, |
| 73 | + message, |
| 74 | + iconUrl: 'img/icon_48.png' |
| 75 | + }); |
| 76 | +} |
| 77 | + |
| 78 | +function notifyNameMissing(verb, prefix) { |
| 79 | + |
| 80 | + notify('You forgot to include a bookmark name!', `To ${verb} a bookmark, include a name after the "${prefix}", like "${prefix}fav".`); |
| 81 | +} |
| 82 | + |
| 83 | +function notifyNotFound(name) { |
| 84 | + |
| 85 | + notify('Bookmark not found', `You don't have any bookmarks with the name "${name}" yet.`) |
| 86 | +} |
| 87 | + |
| 88 | +class Bookmarks { |
| 89 | + |
| 90 | + static async remove(title) { |
| 91 | + |
| 92 | + const bookmark = await this._getBookmark(title); |
| 93 | + if (bookmark) { |
| 94 | + await browser.bookmarks.remove(bookmark.id); |
| 95 | + return true; |
| 96 | + } |
| 97 | + return false; |
| 98 | + } |
| 99 | + |
| 100 | + /** @returns {string|null} The URL for the matching bookmark, or null if none matches. */ |
| 101 | + static async get(title) { |
| 102 | + |
| 103 | + const bookmark = await this._getBookmark(title); |
| 104 | + return bookmark && bookmark.url; |
| 105 | + } |
| 106 | + |
| 107 | + /** @returns {boolean} true if a new bookmark was created or false if an existing one was updated. */ |
| 108 | + static async save(title, url) { |
| 109 | + |
| 110 | + const existingBookmark = await this._getBookmark(title); |
| 111 | + if (existingBookmark) { |
| 112 | + await browser.bookmarks.update(existingBookmark.id, { url }); |
| 113 | + return false; |
| 114 | + } |
| 115 | + const parentId = await this._getFolderId(); |
| 116 | + await browser.bookmarks.create({ parentId, title, url }); |
| 117 | + return true; |
| 118 | + } |
| 119 | + |
| 120 | + static _cachedFolderId = 0; |
| 121 | + static FOLDER_NAME = 'Omnibookmarks'; |
| 122 | + |
| 123 | + static async _getBookmark(title) { |
| 124 | + |
| 125 | + const bookmarks = await this._getChildren(); |
| 126 | + const bookmarkOrNull = bookmarks.find(b => b.title === title) || null; |
| 127 | + return bookmarkOrNull; |
| 128 | + } |
| 129 | + |
| 130 | + static async _getChildren() { |
| 131 | + |
| 132 | + const id = await this._getFolderId(); |
| 133 | + const children = await browser.bookmarks.getChildren(id); |
| 134 | + return children; |
| 135 | + } |
| 136 | + |
| 137 | + static async _getFolderId() { |
| 138 | + |
| 139 | + if (!this._cachedFolderId) { |
| 140 | + // Search to allow the Omnibookmarks folder to be manually moved somewhere else. |
| 141 | + const results = await browser.bookmarks.search({ title: this.FOLDER_NAME }) |
| 142 | + const existingFolder = results[0]; |
| 143 | + if (existingFolder) { |
| 144 | + this._cachedFolderId = existingFolder.id; |
| 145 | + } else { |
| 146 | + const newFolder = await browser.bookmarks.create({ title: this.FOLDER_NAME }); |
| 147 | + this._cachedFolderId = newFolder.id; |
| 148 | + } |
| 149 | + } |
| 150 | + return this._cachedFolderId; |
| 151 | + } |
| 152 | +} |
0 commit comments