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

View file

@ -20,18 +20,60 @@ type browser$storage = {
}
type browser$Permissions = {
origins?: Array<string>,
permissions?: Array<string>,
}
type result = boolean
type granted = boolean
type browser$permissions = {
contains: (permissions: browser$Permissions) => Promise<boolean> /*result*/,
request : (permissions: browser$Permissions) => Promise<boolean> /*granted*/,
contains: (permissions: chrome$Permissions) => Promise<result>,
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: {
storage: browser$storage,
permissions: browser$permissions,
tabs: browser$tabs,
}