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

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: {
storage: browser$storage,
permissions: browser$permissions,
tabs: browser$tabs,
scripting: browser$scripting,
}