From fc85a03a42a6d3fb840459fc13389b3d36e48d3d Mon Sep 17 00:00:00 2001 From: Dima Gerasimov Date: Wed, 28 Dec 2022 21:02:07 +0000 Subject: [PATCH] switch scripting.executeScript to promises, add types --- extension/src/js/background.js | 14 ++++++-------- extension/src/js/browser_types.js | 14 ++++++++++++++ 2 files changed, 20 insertions(+), 8 deletions(-) diff --git a/extension/src/js/background.js b/extension/src/js/background.js index aa0276f..2be9b09 100644 --- a/extension/src/js/background.js +++ b/extension/src/js/background.js @@ -106,25 +106,23 @@ async function capture(comment: ?string = null, tag_str: ?string = null) { const opts = await getOptions() const has_scripting = 'scripting' in chrome + let selection 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({ + const results = await browser.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) }) + const [res] = results // should only inject in one frame, so just one result + selection = res.result } else { const selections = await browser.tabs.executeScript({ code: "window.getSelection().toString();" }) - const selection = selections == null ? null : selections[0] - await makeCaptureRequest(payload(selection), opts) + selection = selections == null ? null : selections[0] } + await makeCaptureRequest(payload(selection), opts) } diff --git a/extension/src/js/browser_types.js b/extension/src/js/browser_types.js index 6d67d3f..f343edb 100644 --- a/extension/src/js/browser_types.js +++ b/extension/src/js/browser_types.js @@ -72,8 +72,22 @@ type browser$tabs = { } +type ExecuteResult = { + result: any, +} + + +type browser$scripting = { + executeScript: ({ + target: {tabId?: number}, + func: () => void, + }) => Promise>, +} + + declare var browser: { storage: browser$storage, permissions: browser$permissions, tabs: browser$tabs, + scripting: browser$scripting, }