extension: some 'cosmetic' preparations for manifest v3

tested on chromium/firefox stable
This commit is contained in:
Dima Gerasimov 2022-12-24 05:29:44 +00:00 committed by karlicoss
parent 5bdd5116e7
commit 11cd3b3f65
3 changed files with 100 additions and 57 deletions

View file

@ -65,31 +65,49 @@ async function makeCaptureRequest(
// TODO FIXME ugh. need defensive error handling on the very top... // TODO FIXME ugh. need defensive error handling on the very top...
function capture(comment: ?string = null, tag_str: ?string = null) { function capture(comment: ?string = null, tag_str: ?string = null) {
chrome.tabs.query({currentWindow: true, active: true }, tabs => { chrome.tabs.query({currentWindow: true, active: true }, tabs => {
const tab = tabs[0]; const tab = tabs[0]
if (tab.url == null) { if (tab.url == null) {
showNotification('ERROR: trying to capture null'); showNotification('ERROR: trying to capture null')
return; return
} }
const url: string = tab.url; const url: string = tab.url
const title: ?string = tab.title; const title: ?string = tab.title
const payload = (selection: ?string) => {
return {
url: url,
title: title,
selection: selection,
comment: comment,
tag_str: tag_str,
}
}
// TODO await properly and handle errors
const has_scripting = 'scripting' in chrome
get_options(opts => { get_options(opts => {
// console.log('action!'); if (has_scripting) {
// ugh.. https://stackoverflow.com/a/19165930/706389 // $FlowFixMe
chrome.tabs.executeScript( { chrome.scripting.executeScript({
code: "window.getSelection().toString();" target: {tabId: tab.id},
}, selections => { func: () => window.getSelection().toString()
const selection = selections == null ? null : selections[0]; }, results => {
makeCaptureRequest({ const [res] = results // should only inject in one frame, so just one result
url: url, const selection = res.result
title: title, makeCaptureRequest(payload(selection), opts)
selection: selection, })
comment: comment, } else {
tag_str: tag_str, chrome.tabs.executeScript( {
}, opts); code: "window.getSelection().toString();"
}); }, selections => {
}); const selection = selections == null ? null : selections[0]
}); makeCaptureRequest(payload(selection), opts)
})
}
})
})
} }

View file

@ -8,19 +8,6 @@
"options_ui": { "options_ui": {
"page": "options.html" "page": "options.html"
}, },
"browser_action": {
"default_icon": "img/unicorn.png",
"default_popup": "popup.html",
"default_title": "Capture page, with extra information"
},
"commands": {
"capture-simple": {
"description": "Quick capture: url, title and selection"
},
"_execute_browser_action": {
"description": "Capture page, with extra information"
}
},
"icons": { "icons": {
"128": "img/unicorn.png" "128": "img/unicorn.png"
}, },

View file

@ -25,34 +25,74 @@ const target = env.TARGET;
const name = "grasp" + (dev ? ' [dev]' : ''); const name = "grasp" + (dev ? ' [dev]' : '');
// Firefox wouldn't let you rebind its default shortcuts most of which use Shift
// On the other hand, Chrome wouldn't let you use Alt
const modifier = target === T.CHROME ? 'Shift' : 'Alt';
// ugh. declarative formats are shit. // ugh. declarative formats are shit.
const commandsExtra = {
// Firefox wouldn't let you rebind its default shortcuts most of which use Shift
// On the other hand, Chrome wouldn't let you use Alt
const modifier = target === T.CHROME ? 'Shift' : 'Alt'
const action_name = 'browser_action'
const commands = {
"capture-simple": { "capture-simple": {
"description": "Quick capture: url, title and selection",
"suggested_key": { "suggested_key": {
"default": `Ctrl+${modifier}+C`, "default": `Ctrl+${modifier}+C`,
"mac": `Command+${modifier}+C` "mac": `Command+${modifier}+C`,
} },
}, }
"_execute_browser_action": { }
commands[`_execute_${action_name}`] = {
"description": "Capture page, with extra information",
"suggested_key": { "suggested_key": {
"default": `Ctrl+${modifier}+Y`, "default": `Ctrl+${modifier}+Y`,
"mac": `Command+${modifier}+Y` "mac": `Command+${modifier}+Y`,
} },
}, }
};
const action = {
"default_icon": "img/unicorn.png",
"default_popup": "popup.html",
"default_title": "Capture page, with extra information",
}
if (target == T.FIREFOX) {
action['browser_style'] = true
}
// prepare for manifest v3
const host_permissions = [
"http://localhost/capture",
"https://localhost/capture",
]
const optional_permissions = [
"http://*/capture",
"https://*/capture",
]
// TODO make permissions literate // TODO make permissions literate
const permissions = [ const permissions = [
"storage", "storage",
"notifications", "notifications",
"activeTab",
"http://localhost/capture", // need to query active tab and get its url/title
"https://localhost/capture" "activeTab",
];
...host_permissions,
]
if (target === T.FIREFOX) {
// chrome v2 doesn't support scripting api
// code has a fallback just for that
// need to get selected text
permissions.push("scripting")
}
const manifestExtra = { const manifestExtra = {
@ -60,12 +100,11 @@ const manifestExtra = {
version: pkg.version, version: pkg.version,
description: pkg.description, description: pkg.description,
permissions: permissions, permissions: permissions,
commands: commandsExtra, commands: commands,
optional_permissions: [ optional_permissions: optional_permissions,
"http://*/capture", }
"https://*/capture", manifestExtra[action_name] = action
],
};
if (dev) { if (dev) {
manifestExtra.content_security_policy = "script-src 'self' 'unsafe-eval'; object-src 'self'"; manifestExtra.content_security_policy = "script-src 'self' 'unsafe-eval'; object-src 'self'";
@ -76,7 +115,6 @@ if (target === T.CHROME) {
} }
if (target === T.FIREFOX) { if (target === T.FIREFOX) {
manifestExtra.options_ui = {browser_style: true}; manifestExtra.options_ui = {browser_style: true};
manifestExtra.browser_action = {browser_style: true};
} }
const buildPath = path.join(__dirname, 'dist', target); const buildPath = path.join(__dirname, 'dist', target);