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, }