diff --git a/src/js/background.js b/src/js/background.js index 1add9c3..41310ac 100644 --- a/src/js/background.js +++ b/src/js/background.js @@ -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)); } }); diff --git a/src/js/common.js b/src/js/common.js index 3d375de..bf0251e 100644 --- a/src/js/common.js +++ b/src/js/common.js @@ -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); + // }); +} diff --git a/src/js/popup.js b/src/js/popup.js index 74f9505..87e58e2 100644 --- a/src/js/popup.js +++ b/src/js/popup.js @@ -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,14 +61,17 @@ function restoreState(state: ?State) { function submitComment () { // TODO focus const state = getState(); - chrome.runtime.sendMessage({ - 'method': METHOD_CAPTURE_WITH_EXTRAS, - ...state, - }, () => { - console.log("[popup] captured!"); + + ensurePermissions(() => { + chrome.runtime.sendMessage({ + 'method': METHOD_CAPTURE_WITH_EXTRAS, + ...state, + }, () => { + console.log("[popup] captured!"); + }); + window.submitted = true; + window.close(); }); - window.submitted = true; - window.close(); } // $FlowFixMe diff --git a/webpack.config.js b/webpack.config.js index 37cd30b..c44a8db 100644 --- a/webpack.config.js +++ b/webpack.config.js @@ -56,6 +56,10 @@ const manifestExtra = { description: pkg.description, permissions: permissions, commands: commandsExtra, + optional_permissions: [ + "http://*/capture", + "https://*/capture", + ], }; if (dev) {