switch scripting.executeScript to promises, add types

This commit is contained in:
Dima Gerasimov 2022-12-28 21:02:07 +00:00 committed by karlicoss
parent 32fb5ce108
commit fc85a03a42
2 changed files with 20 additions and 8 deletions

View file

@ -106,25 +106,23 @@ async function capture(comment: ?string = null, tag_str: ?string = null) {
const opts = await getOptions() const opts = await getOptions()
const has_scripting = 'scripting' in chrome const has_scripting = 'scripting' in chrome
let selection
if (has_scripting) { if (has_scripting) {
// TODO switch to polyfill and add flow types // TODO switch to polyfill and add flow types
// scripting is already promise based so it should be oly change to types // scripting is already promise based so it should be oly change to types
// $FlowFixMe const results = await browser.scripting.executeScript({
chrome.scripting.executeScript({
target: {tabId: tab.id}, target: {tabId: tab.id},
func: () => window.getSelection().toString() 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 { } else {
const selections = await browser.tabs.executeScript({ const selections = await browser.tabs.executeScript({
code: "window.getSelection().toString();" code: "window.getSelection().toString();"
}) })
const selection = selections == null ? null : selections[0] selection = selections == null ? null : selections[0]
await makeCaptureRequest(payload(selection), opts)
} }
await makeCaptureRequest(payload(selection), opts)
} }

View file

@ -72,8 +72,22 @@ type browser$tabs = {
} }
type ExecuteResult = {
result: any,
}
type browser$scripting = {
executeScript: ({
target: {tabId?: number},
func: () => void,
}) => Promise<Array<ExecuteResult>>,
}
declare var browser: { declare var browser: {
storage: browser$storage, storage: browser$storage,
permissions: browser$permissions, permissions: browser$permissions,
tabs: browser$tabs, tabs: browser$tabs,
scripting: browser$scripting,
} }