From 0fb2e42fe927e2b18203bb70e3e341043558ae42 Mon Sep 17 00:00:00 2001 From: Dima Gerasimov Date: Tue, 27 Dec 2022 23:41:25 +0000 Subject: [PATCH] use polyfill for permissions --- extension/src/js/browser_types.js | 12 ++++++++++++ extension/src/js/permissions.js | 27 +++++++++------------------ 2 files changed, 21 insertions(+), 18 deletions(-) diff --git a/extension/src/js/browser_types.js b/extension/src/js/browser_types.js index 472e1c5..7f95f39 100644 --- a/extension/src/js/browser_types.js +++ b/extension/src/js/browser_types.js @@ -20,6 +20,18 @@ type browser$storage = { } +type browser$Permissions = { + origins?: Array, + permissions?: Array, +} + + +type browser$permissions = { + contains: (permissions: browser$Permissions) => Promise /*result*/, + request : (permissions: browser$Permissions) => Promise /*granted*/, +} + declare var browser: { storage: browser$storage, + permissions: browser$permissions, } diff --git a/extension/src/js/permissions.js b/extension/src/js/permissions.js index a3396e1..cbdf0c6 100644 --- a/extension/src/js/permissions.js +++ b/extension/src/js/permissions.js @@ -1,16 +1,4 @@ /* @flow */ -import {showNotification, awrap} from './common'; -import {getOptions} from './options'; - - -export function chromePermissionsRequest(params: any): Promise { - return awrap(chrome.permissions.request, params); -} - -export function chromePermissionsContains(params: any): Promise { - return awrap(chrome.permissions.contains, params); -} - function urlForPermissionsCheck(url: string): string { var u = new URL(url); @@ -23,23 +11,24 @@ function urlForPermissionsCheck(url: string): string { return u.toString(); } -export function hasPermissions(endpoint: string): Promise { +export async function hasPermissions(endpoint: string): Promise { const perms = { origins: [ urlForPermissionsCheck(endpoint), ], - }; - return chromePermissionsContains(perms); + } + return browser.permissions.contains(perms) } -export function ensurePermissions(endpoint: string): Promise { + +export function ensurePermissions(endpoint: string): Promise { const perms = { origins: [ urlForPermissionsCheck(endpoint), ], - }; + } // 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... @@ -47,6 +36,7 @@ export function ensurePermissions(endpoint: string): Promise { // 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 +/* function ensurePermissions_old(cb: () => void) { // eslint-disable-line no-unused-vars const back = chrome.extension.getBackgroundPage().console; @@ -82,3 +72,4 @@ function ensurePermissions_old(cb: () => void) { // eslint-disable-line no-unuse }); // TODO improve message somehow??? } +*/