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,60 +63,61 @@ 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) {
showNotification('ERROR: trying to capture null') // todo await?
return showNotification('ERROR: trying to capture null')
return
}
const url: string = tab.url
const title: ?string = tab.title
const payload = (selection: ?string) => {
return {
url: url,
title: title,
selection: selection,
comment: comment,
tag_str: tag_str,
} }
const url: string = tab.url }
const title: ?string = tab.title
const opts = await getOptions()
const payload = (selection: ?string) => { const has_scripting = 'scripting' in chrome
return { if (has_scripting) {
url: url, // TODO switch to polyfill and add flow types
title: title, // scripting is already promise based so it should be oly change to types
selection: selection, // $FlowFixMe
comment: comment, chrome.scripting.executeScript({
tag_str: tag_str, target: {tabId: tab.id},
} func: () => window.getSelection().toString()
} }, results => {
const [res] = results // should only inject in one frame, so just one result
// TODO await properly and handle errors const selection = res.result
const has_scripting = 'scripting' in chrome makeCaptureRequest(payload(selection), opts)
getOptions().then(opts => {
if (has_scripting) {
// $FlowFixMe
chrome.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)
})
} else {
chrome.tabs.executeScript( {
code: "window.getSelection().toString();"
}, selections => {
const selection = selections == null ? null : selections[0]
makeCaptureRequest(payload(selection), opts)
})
}
}) })
}) } else {
const selections = await browser.tabs.executeScript({
code: "window.getSelection().toString();"
})
const selection = selections == null ? null : selections[0]
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,
} }