From d5a0c35848a87b2f325480be570b1ff2de22f54c Mon Sep 17 00:00:00 2001 From: Dima Gerasimov Date: Wed, 28 Dec 2022 00:03:17 +0000 Subject: [PATCH] rewrite main capture function to use polyfill --- extension/src/js/background.js | 94 ++++++++++++++++--------------- extension/src/js/browser_types.js | 54 ++++++++++++++++-- 2 files changed, 96 insertions(+), 52 deletions(-) diff --git a/extension/src/js/background.js b/extension/src/js/background.js index ed3c726..3597fa8 100644 --- a/extension/src/js/background.js +++ b/extension/src/js/background.js @@ -63,60 +63,61 @@ async function makeCaptureRequest( } -// TODO FIXME ugh. need defensive error handling on the very top... -function capture(comment: ?string = null, tag_str: ?string = null) { - chrome.tabs.query({currentWindow: true, active: true }, tabs => { - const tab = tabs[0] - if (tab.url == null) { - showNotification('ERROR: trying to capture null') - return +// TODO ugh. need defensive error handling on the very top... +async function capture(comment: ?string = null, tag_str: ?string = null) { + const tabs = await browser.tabs.query({currentWindow: true, active: true}) + const tab = tabs[0] + if (tab.url == null) { + // todo await? + showNotification('ERROR: trying to capture null') + return + } + const url: string = tab.url + const title: ?string = tab.title + + + const payload = (selection: ?string) => { + return { + url: url, + title: title, + selection: selection, + comment: comment, + tag_str: tag_str, } - const url: string = tab.url - const title: ?string = tab.title + } + const opts = await getOptions() - const payload = (selection: ?string) => { - return { - url: url, - title: title, - selection: selection, - comment: comment, - tag_str: tag_str, - } - } - - // TODO await properly and handle errors - const has_scripting = 'scripting' in chrome - - getOptions().then(opts => { - if (has_scripting) { - // $FlowFixMe - chrome.scripting.executeScript({ - target: {tabId: tab.id}, - func: () => window.getSelection().toString() - }, results => { - const [res] = results // should only inject in one frame, so just one result - const selection = res.result - makeCaptureRequest(payload(selection), opts) - }) - } else { - chrome.tabs.executeScript( { - code: "window.getSelection().toString();" - }, selections => { - const selection = selections == null ? null : selections[0] - makeCaptureRequest(payload(selection), opts) - }) - } + const has_scripting = 'scripting' in chrome + if (has_scripting) { + // TODO switch to polyfill and add flow types + // scripting is already promise based so it should be oly change to types + // $FlowFixMe + chrome.scripting.executeScript({ + target: {tabId: tab.id}, + func: () => window.getSelection().toString() + }, results => { + const [res] = results // should only inject in one frame, so just one result + const selection = res.result + makeCaptureRequest(payload(selection), opts) }) - }) + } else { + const selections = await browser.tabs.executeScript({ + code: "window.getSelection().toString();" + }) + const selection = selections == null ? null : selections[0] + await makeCaptureRequest(payload(selection), opts) + } } chrome.commands.onCommand.addListener(command => { if (command === COMMAND_CAPTURE_SIMPLE) { - capture(null, null); + // todo await + capture(null, null) } -}); +}) + chrome.runtime.onMessage.addListener((message: any, sender: chrome$MessageSender, sendResponse) => { // eslint-disable-line no-unused-vars if (message.method === 'logging') { @@ -125,9 +126,10 @@ chrome.runtime.onMessage.addListener((message: any, sender: chrome$MessageSender if (message.method === METHOD_CAPTURE_WITH_EXTRAS) { const comment = message.comment; const tag_str = message.tag_str; - capture(comment, tag_str); + // todo await + capture(comment, tag_str) } -}); +}) // TODO handle cannot access chrome:// url?? diff --git a/extension/src/js/browser_types.js b/extension/src/js/browser_types.js index 7f95f39..6d67d3f 100644 --- a/extension/src/js/browser_types.js +++ b/extension/src/js/browser_types.js @@ -20,18 +20,60 @@ type browser$storage = { } -type browser$Permissions = { - origins?: Array, - permissions?: Array, -} +type result = boolean +type granted = boolean type browser$permissions = { - contains: (permissions: browser$Permissions) => Promise /*result*/, - request : (permissions: browser$Permissions) => Promise /*granted*/, + contains: (permissions: chrome$Permissions) => Promise, + request : (permissions: chrome$Permissions) => Promise, } + +type browser$tabs = { + query(queryInfo: { + active?: boolean, + audible?: boolean, + currentWindow?: boolean, + highlighted?: boolean, + index?: number, + lastFocusedWindow?: boolean, + muted?: boolean, + pinned?: boolean, + status?: chrome$TabStatus, + title?: string, + url?: string | Array, + windowId?: number, + windowType?: chrome$WindowType + }): Promise>, + executeScript: ( + (( + tabId?: number, + details: { + allFrames?: boolean, + code?: string, + file?: string, + frameId?: number, + matchAboutBlank?: boolean, + runAt?: chrome$RunAt + }, + ) => Promise) & + (( + details: { + allFrames?: boolean, + code?: string, + file?: string, + frameId?: number, + matchAboutBlank?: boolean, + runAt?: chrome$RunAt + }, + ) => Promise) + ), +} + + declare var browser: { storage: browser$storage, permissions: browser$permissions, + tabs: browser$tabs, }