use polyfill for permissions

This commit is contained in:
Dima Gerasimov 2022-12-27 23:41:25 +00:00 committed by karlicoss
parent f906728bf8
commit 0fb2e42fe9
2 changed files with 21 additions and 18 deletions

View file

@ -20,6 +20,18 @@ type browser$storage = {
} }
type browser$Permissions = {
origins?: Array<string>,
permissions?: Array<string>,
}
type browser$permissions = {
contains: (permissions: browser$Permissions) => Promise<boolean> /*result*/,
request : (permissions: browser$Permissions) => Promise<boolean> /*granted*/,
}
declare var browser: { declare var browser: {
storage: browser$storage, storage: browser$storage,
permissions: browser$permissions,
} }

View file

@ -1,16 +1,4 @@
/* @flow */ /* @flow */
import {showNotification, awrap} from './common';
import {getOptions} from './options';
export function chromePermissionsRequest(params: any): Promise<any> {
return awrap(chrome.permissions.request, params);
}
export function chromePermissionsContains(params: any): Promise<any> {
return awrap(chrome.permissions.contains, params);
}
function urlForPermissionsCheck(url: string): string { function urlForPermissionsCheck(url: string): string {
var u = new URL(url); var u = new URL(url);
@ -23,23 +11,24 @@ function urlForPermissionsCheck(url: string): string {
return u.toString(); return u.toString();
} }
export function hasPermissions(endpoint: string): Promise<any> { export async function hasPermissions(endpoint: string): Promise<boolean> {
const perms = { const perms = {
origins: [ origins: [
urlForPermissionsCheck(endpoint), urlForPermissionsCheck(endpoint),
], ],
}; }
return chromePermissionsContains(perms); return browser.permissions.contains(perms)
} }
export function ensurePermissions(endpoint: string): Promise<any> {
export function ensurePermissions(endpoint: string): Promise<boolean> {
const perms = { const perms = {
origins: [ origins: [
urlForPermissionsCheck(endpoint), urlForPermissionsCheck(endpoint),
], ],
}; }
// shouldn't prompt if we already have the permission // shouldn't prompt if we already have the permission
return chromePermissionsRequest(perms); return browser.permissions.request(perms)
} }
// just keeping the old one for the reference... // just keeping the old one for the reference...
@ -47,6 +36,7 @@ export function ensurePermissions(endpoint: string): Promise<any> {
// TODO fuck. doesn't seem to work for Firefox at all... // TODO fuck. doesn't seem to work for Firefox at all...
// just getting Error: "An unexpected error occurred" background.bundle.js // just getting Error: "An unexpected error occurred" background.bundle.js
// seems like this bug https://bugzilla.mozilla.org/show_bug.cgi?id=1382953 // seems like this bug https://bugzilla.mozilla.org/show_bug.cgi?id=1382953
/*
function ensurePermissions_old(cb: () => void) { // eslint-disable-line no-unused-vars function ensurePermissions_old(cb: () => void) { // eslint-disable-line no-unused-vars
const back = chrome.extension.getBackgroundPage().console; const back = chrome.extension.getBackgroundPage().console;
@ -82,3 +72,4 @@ function ensurePermissions_old(cb: () => void) { // eslint-disable-line no-unuse
}); });
// TODO improve message somehow??? // TODO improve message somehow???
} }
*/