initial optional permissions support for non-localhost

This commit is contained in:
Dima Gerasimov 2019-12-09 23:00:41 +00:00 committed by Dmitrii Gerasimov
parent c5440b61cb
commit 4130b4eca0
4 changed files with 52 additions and 10 deletions

View file

@ -1,7 +1,7 @@
/* @flow */
// TODO do I really need to annotate all files with @flow??
import {COMMAND_CAPTURE_SIMPLE, METHOD_CAPTURE_WITH_EXTRAS, showNotification} from './common';
import {COMMAND_CAPTURE_SIMPLE, METHOD_CAPTURE_WITH_EXTRAS, showNotification, ensurePermissions} from './common';
import {get_options} from './options';
type Params = {
@ -74,6 +74,8 @@ function makeCaptureRequest(
request.send(data);
}
// 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 => {
const tab = tabs[0];
@ -106,7 +108,7 @@ function capture(comment: ?string = null, tag_str: ?string = null) {
chrome.commands.onCommand.addListener(command => {
if (command === COMMAND_CAPTURE_SIMPLE) {
capture(null, null);
ensurePermissions(() => capture(null, null));
}
});

View file

@ -36,3 +36,36 @@ export function chromePermissionsRequest(params: any) {
export function chromePermissionsContains(params: any) {
return awrap(chrome.permissions.contains, params);
}
// TODO ugh. use promises already...
// TODO fuck. doesn't seem to work for Firefox at all...
// just getting Error: "An unexpected error occurred" background.bundle.js
// seems like this bug https://bugzilla.mozilla.org/show_bug.cgi?id=1382953
export function ensurePermissions(cb: () => void) {
const needs_check = false; // TODO FIXME
if (!needs_check) {
cb();
return;
}
const back = chrome.extension.getBackgroundPage().console;
function onResponse(response) {
const err = chrome.runtime.lastError;
console.error("ERROR!", err);
if (response) {
back.info("Permission was granted");
cb();
} else {
showNotification(`ERROR: Permission was refused`);
}
}
const perms = {
// TODO FIXME only if it's not localhost?
origins: ["https://*/capture"],
};
// TODO improve message somehow???
chrome.permissions.request(perms, onResponse);
// browser.permissions.request(perms).then(onResponse, error => {
// back.error("ERROR!!!!", error);
// });
}

View file

@ -1,6 +1,6 @@
/* @flow */
import {METHOD_CAPTURE_WITH_EXTRAS} from './common';
import {METHOD_CAPTURE_WITH_EXTRAS, ensurePermissions} from './common';
import {get_options} from './options';
// TODO template it in html too?
@ -61,6 +61,8 @@ function restoreState(state: ?State) {
function submitComment () {
// TODO focus
const state = getState();
ensurePermissions(() => {
chrome.runtime.sendMessage({
'method': METHOD_CAPTURE_WITH_EXTRAS,
...state,
@ -69,6 +71,7 @@ function submitComment () {
});
window.submitted = true;
window.close();
});
}
// $FlowFixMe

View file

@ -56,6 +56,10 @@ const manifestExtra = {
description: pkg.description,
permissions: permissions,
commands: commandsExtra,
optional_permissions: [
"http://*/capture",
"https://*/capture",
],
};
if (dev) {