rewrite main capture function to use polyfill

This commit is contained in:
Dima Gerasimov 2022-12-28 00:03:17 +00:00 committed by karlicoss
parent 0fb2e42fe9
commit d5a0c35848
2 changed files with 96 additions and 52 deletions

View file

@ -63,11 +63,12 @@ async function makeCaptureRequest(
} }
// TODO FIXME ugh. need defensive error handling on the very top... // TODO ugh. need defensive error handling on the very top...
function capture(comment: ?string = null, tag_str: ?string = null) { async function capture(comment: ?string = null, tag_str: ?string = null) {
chrome.tabs.query({currentWindow: true, active: true }, tabs => { const tabs = await browser.tabs.query({currentWindow: true, active: true})
const tab = tabs[0] const tab = tabs[0]
if (tab.url == null) { if (tab.url == null) {
// todo await?
showNotification('ERROR: trying to capture null') showNotification('ERROR: trying to capture null')
return return
} }
@ -85,11 +86,12 @@ function capture(comment: ?string = null, tag_str: ?string = null) {
} }
} }
// TODO await properly and handle errors const opts = await getOptions()
const has_scripting = 'scripting' in chrome
getOptions().then(opts => { const has_scripting = 'scripting' in chrome
if (has_scripting) { 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 // $FlowFixMe
chrome.scripting.executeScript({ chrome.scripting.executeScript({
target: {tabId: tab.id}, target: {tabId: tab.id},
@ -100,23 +102,22 @@ function capture(comment: ?string = null, tag_str: ?string = null) {
makeCaptureRequest(payload(selection), opts) makeCaptureRequest(payload(selection), opts)
}) })
} else { } else {
chrome.tabs.executeScript( { const selections = await browser.tabs.executeScript({
code: "window.getSelection().toString();" code: "window.getSelection().toString();"
}, selections => { })
const selection = selections == null ? null : selections[0] const selection = selections == null ? null : selections[0]
makeCaptureRequest(payload(selection), opts) await makeCaptureRequest(payload(selection), opts)
})
} }
})
})
} }
chrome.commands.onCommand.addListener(command => { chrome.commands.onCommand.addListener(command => {
if (command === COMMAND_CAPTURE_SIMPLE) { 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 chrome.runtime.onMessage.addListener((message: any, sender: chrome$MessageSender, sendResponse) => { // eslint-disable-line no-unused-vars
if (message.method === 'logging') { if (message.method === 'logging') {
@ -125,9 +126,10 @@ chrome.runtime.onMessage.addListener((message: any, sender: chrome$MessageSender
if (message.method === METHOD_CAPTURE_WITH_EXTRAS) { if (message.method === METHOD_CAPTURE_WITH_EXTRAS) {
const comment = message.comment; const comment = message.comment;
const tag_str = message.tag_str; const tag_str = message.tag_str;
capture(comment, tag_str); // todo await
capture(comment, tag_str)
} }
}); })
// TODO handle cannot access chrome:// url?? // TODO handle cannot access chrome:// url??

View file

@ -20,18 +20,60 @@ type browser$storage = {
} }
type browser$Permissions = { type result = boolean
origins?: Array<string>, type granted = boolean
permissions?: Array<string>,
}
type browser$permissions = { type browser$permissions = {
contains: (permissions: browser$Permissions) => Promise<boolean> /*result*/, contains: (permissions: chrome$Permissions) => Promise<result>,
request : (permissions: browser$Permissions) => Promise<boolean> /*granted*/, request : (permissions: chrome$Permissions) => Promise<granted>,
} }
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<string>,
windowId?: number,
windowType?: chrome$WindowType
}): Promise<Array<chrome$Tab>>,
executeScript: (
((
tabId?: number,
details: {
allFrames?: boolean,
code?: string,
file?: string,
frameId?: number,
matchAboutBlank?: boolean,
runAt?: chrome$RunAt
},
) => Promise<void>) &
((
details: {
allFrames?: boolean,
code?: string,
file?: string,
frameId?: number,
matchAboutBlank?: boolean,
runAt?: chrome$RunAt
},
) => Promise<void>)
),
}
declare var browser: { declare var browser: {
storage: browser$storage, storage: browser$storage,
permissions: browser$permissions, permissions: browser$permissions,
tabs: browser$tabs,
} }