From ffb19a6d969aa1b9d7637526c13096a9672085ea Mon Sep 17 00:00:00 2001 From: Bauke Date: Mon, 14 Mar 2022 23:58:05 +0100 Subject: [PATCH] Simplify opening the next queued link by using tabs.update(). Previously we would use messaging to open the next link with either a loaded content script in the current tab or a new tab if the tab can't load a content script. This makes it simpler and also has the added benefit of no longer requiring the permission to access all websites. --- source/background-scripts/browser-action.ts | 21 ++++----------------- source/background-scripts/initialize.ts | 11 ----------- source/content-scripts/initialize.ts | 8 -------- source/manifest.json | 14 +------------- source/options/components/page-main.ts | 9 +++------ source/options/index.ts | 6 ++---- source/types.d.ts | 7 ------- source/utilities/messaging.ts | 17 ----------------- 8 files changed, 10 insertions(+), 83 deletions(-) delete mode 100644 source/content-scripts/initialize.ts delete mode 100644 source/utilities/messaging.ts diff --git a/source/background-scripts/browser-action.ts b/source/background-scripts/browser-action.ts index 03ad82f..f970e8f 100644 --- a/source/background-scripts/browser-action.ts +++ b/source/background-scripts/browser-action.ts @@ -1,6 +1,7 @@ import browser from 'webextension-polyfill'; import {Settings} from '../settings/settings.js'; +import {updateBadge} from '../utilities/badge.js'; let timeoutId: number | undefined; @@ -9,7 +10,6 @@ export async function browserActionClicked(): Promise { // When the extension icon is initially clicked, create a timeout for 500ms // that will open the next queue item when it expires. - // If the icon is clicked again in those 500ms, open the options page instead. if (timeoutId === undefined) { timeoutId = window.setTimeout(async () => { timeoutId = undefined; @@ -21,27 +21,14 @@ export async function browserActionClicked(): Promise { return; } - const tabs = await browser.tabs.query({ - active: true, - currentWindow: true, - }); - - const message: Queue.Message = { - action: 'queue open url', - data: nextItem, - }; - - try { - await browser.tabs.sendMessage(tabs[0].id!, message); - } catch { - await browser.tabs.create({active: true, url: nextItem.url}); - } - + await browser.tabs.update({url: nextItem.url}); await settings.removeQueueItem(nextItem.id); + await updateBadge(settings); }, 500); return; } + // If the icon is clicked again in those 500ms, open the options page instead. window.clearTimeout(timeoutId); timeoutId = undefined; await browser.runtime.openOptionsPage(); diff --git a/source/background-scripts/initialize.ts b/source/background-scripts/initialize.ts index e9fdf9a..f7495d8 100644 --- a/source/background-scripts/initialize.ts +++ b/source/background-scripts/initialize.ts @@ -1,7 +1,5 @@ import browser from 'webextension-polyfill'; -import {Settings} from '../settings/settings.js'; -import {updateBadge} from '../utilities/badge.js'; import {History} from '../utilities/history.js'; import {browserActionClicked} from './browser-action.js'; import {initializeContextMenus} from './context-menus.js'; @@ -19,13 +17,4 @@ browser.runtime.onInstalled.addListener(async () => { } }); -browser.runtime.onMessage.addListener( - async (request: Queue.Message) => { - if (request.action === 'queue update badge') { - const settings = await Settings.fromSyncStorage(); - await updateBadge(settings); - } - }, -); - initializeContextMenus(); diff --git a/source/content-scripts/initialize.ts b/source/content-scripts/initialize.ts deleted file mode 100644 index 7ce23e6..0000000 --- a/source/content-scripts/initialize.ts +++ /dev/null @@ -1,8 +0,0 @@ -import {initializeMessaging, sendMessage} from '../utilities/messaging.js'; - -async function initializeScripts() { - initializeMessaging(); - await sendMessage('queue update badge'); -} - -void initializeScripts(); diff --git a/source/manifest.json b/source/manifest.json index d70d8d7..dd1d479 100644 --- a/source/manifest.json +++ b/source/manifest.json @@ -7,8 +7,7 @@ "permissions": [ "contextMenus", "storage", - "tabs", - "*://*/*" + "tabs" ], "content_security_policy": "script-src 'self'; object-src 'self'; style-src 'unsafe-inline'", "web_accessible_resources": [ @@ -31,17 +30,6 @@ "background-scripts/initialize.ts" ] }, - "content_scripts": [ - { - "matches": [ - "*://*/*" - ], - "run_at": "document_end", - "js": [ - "content-scripts/initialize.ts" - ] - } - ], "applications": { "gecko": { "id": "{c3560e6b-00e5-4ab3-b89e-8a54ee5b2c9f}" diff --git a/source/options/components/page-main.ts b/source/options/components/page-main.ts index f756fd5..0a643a8 100644 --- a/source/options/components/page-main.ts +++ b/source/options/components/page-main.ts @@ -1,7 +1,7 @@ import {Component, html} from 'htm/preact'; -import browser from 'webextension-polyfill'; import {Settings} from '../../settings/settings.js'; +import {updateBadge} from '../../utilities/badge.js'; import {History} from '../../utilities/history.js'; import {ConfirmButton} from './confirm-button.js'; import {Link} from './link.js'; @@ -27,7 +27,7 @@ export class PageMain extends Component { removeItem = async (id: number) => { const {settings} = this.props; await settings.removeQueueItem(id); - await browser.runtime.sendMessage({action: 'queue update badge'}); + await updateBadge(settings); this.setState({queue: this.props.settings.queue}); }; @@ -77,10 +77,7 @@ export class PageMain extends Component {

Opening the next link from your queue:

    -
  • - Click on the extension icon to open it in the current tab (when - possible). -
  • +
  • Click on the extension icon to open it in the current tab.
  • Right-click the extension icon and click "Open next link in new tab". diff --git a/source/options/index.ts b/source/options/index.ts index bd802b3..7728650 100644 --- a/source/options/index.ts +++ b/source/options/index.ts @@ -2,16 +2,14 @@ import {html} from 'htm/preact'; import {Component, render} from 'preact'; import {Settings} from '../settings/settings.js'; -import {initializeMessaging, sendMessage} from '../utilities/messaging.js'; +import {updateBadge} from '../utilities/badge.js'; import {History} from '../utilities/history.js'; import {PageFooter, PageHeader, PageMain} from './components/components.js'; window.addEventListener('DOMContentLoaded', async () => { - initializeMessaging(); - await sendMessage('queue update badge'); - const history = await History.fromLocalStorage(); const settings = await Settings.fromSyncStorage(); + await updateBadge(settings); render( html`<${OptionsPage} history=${history} settings=${settings} />`, diff --git a/source/types.d.ts b/source/types.d.ts index 6bde824..60af7bd 100644 --- a/source/types.d.ts +++ b/source/types.d.ts @@ -24,12 +24,5 @@ declare global { text: string; url: string; }; - - type MessageAction = 'queue open url' | 'queue update badge'; - - type Message = { - action: MessageAction; - data: T; - }; } } diff --git a/source/utilities/messaging.ts b/source/utilities/messaging.ts deleted file mode 100644 index 5405e6a..0000000 --- a/source/utilities/messaging.ts +++ /dev/null @@ -1,17 +0,0 @@ -import browser from 'webextension-polyfill'; - -export function initializeMessaging() { - browser.runtime.onMessage.addListener((request: Queue.Message) => { - if (request.action === 'queue open url') { - const message = request as Queue.Message; - window.location.href = message.data.url; - } - }); -} - -export async function sendMessage( - action: Queue.MessageAction, - data?: T, -): Promise { - await browser.runtime.sendMessage({action, data}); -}