From faeb5dede016f25710bc79b6b3fcdcd82f37e45b Mon Sep 17 00:00:00 2001 From: Ajay Date: Tue, 16 Aug 2022 16:00:34 -0400 Subject: [PATCH] Add page for refreshing invidious permissions if it was revoked Fixes #1354 --- public/_locales/en/messages.json | 7 ++----- public/permissions/index.html | 6 ++++++ src/background.ts | 8 +++++++- src/options.ts | 26 +++----------------------- src/permissions.ts | 20 ++++++-------------- src/utils.ts | 31 +++++++++++++++++++++++++++++++ 6 files changed, 55 insertions(+), 43 deletions(-) diff --git a/public/_locales/en/messages.json b/public/_locales/en/messages.json index 2038801d..24afb87c 100644 --- a/public/_locales/en/messages.json +++ b/public/_locales/en/messages.json @@ -794,11 +794,8 @@ "description": "This error appears in an alert when they try to whitelist a channel and the extension is unable to determine what channel they are looking at.", "message": "Channel ID is not loaded yet. If you are using an embedded video, try using the YouTube homepage instead. This could also be caused by changes in the YouTube layout, if you think so, make a comment here:" }, - "videoInfoFetchFailed": { - "message": "It seems that something is blocking SponsorBlock's ability to get video data. Please see https://github.com/ajayyy/SponsorBlock/issues/741 for more info." - }, - "youtubePermissionRequest": { - "message": "It seems that SponsorBlock is unable to reach the YouTube API. To fix this, accept the permission prompt that will appear next, wait a few seconds, and then reload the page." + "invidiousPermissionRefresh": { + "message": "The browser has revoked the permission needed to function on Invidious and other 3rd-party sites. Please click the button below to reactivate this permission." }, "acceptPermission": { "message": "Accept permission" diff --git a/public/permissions/index.html b/public/permissions/index.html index 4475f039..bf854248 100644 --- a/public/permissions/index.html +++ b/public/permissions/index.html @@ -19,6 +19,12 @@
+
+ __MSG_invidiousPermissionRefresh__ +
+ +
+
__MSG_acceptPermission__ diff --git a/src/background.ts b/src/background.ts index b3880dff..6c92a10d 100644 --- a/src/background.ts +++ b/src/background.ts @@ -107,7 +107,7 @@ chrome.runtime.onMessage.addListener(function (request, _, callback) { chrome.runtime.onInstalled.addListener(function () { // This let's the config sync to run fully before checking. // This is required on Firefox - setTimeout(function() { + setTimeout(async () => { const userID = Config.config.userID; // If there is no userID, then it is the first install. @@ -123,6 +123,12 @@ chrome.runtime.onInstalled.addListener(function () { // Don't show update notification Config.config.categoryPillUpdate = true; } + + if (Config.config.supportInvidious) { + if (!(await utils.containsInvidiousPermission())) { + chrome.tabs.create({url: chrome.extension.getURL("/permissions/index.html")}); + } + } }, 1500); }); diff --git a/src/options.ts b/src/options.ts index 8d456948..9d456936 100644 --- a/src/options.ts +++ b/src/options.ts @@ -452,13 +452,7 @@ function invidiousInstanceAddInit(element: HTMLElement, option: string) { * @param option */ function invidiousInit(checkbox: HTMLInputElement, option: string) { - let permissions = ["declarativeContent"]; - if (utils.isFirefox()) permissions = []; - - chrome.permissions.contains({ - origins: utils.getPermissionRegex(), - permissions: permissions - }, function (result) { + utils.containsInvidiousPermission().then((result) => { if (result != checkbox.checked) { Config.config[option] = result; @@ -474,22 +468,8 @@ function invidiousInit(checkbox: HTMLInputElement, option: string) { * @param option */ async function invidiousOnClick(checkbox: HTMLInputElement, option: string): Promise { - return new Promise((resolve) => { - if (checkbox.checked) { - utils.setupExtraSitePermissions(function (granted) { - if (!granted) { - Config.config[option] = false; - checkbox.checked = false; - } else { - checkbox.checked = true; - } - - resolve(); - }); - } else { - utils.removeExtraSiteRegistration(); - } - }); + const enabled = await utils.applyInvidiousPermissions(checkbox.checked, option); + checkbox.checked = enabled; } /** diff --git a/src/permissions.ts b/src/permissions.ts index 05da496e..1e2c1119 100644 --- a/src/permissions.ts +++ b/src/permissions.ts @@ -12,25 +12,17 @@ window.addEventListener('DOMContentLoaded', init); async function init() { localizeHtmlPage(); - const domains = document.location.hash.replace("#", "").split(","); - const acceptButton = document.getElementById("acceptPermissionButton"); acceptButton.addEventListener("click", () => { - chrome.permissions.request({ - origins: utils.getPermissionRegex(domains), - permissions: [] - }, (granted) => { - if (granted) { + utils.applyInvidiousPermissions(Config.config.supportInvidious).then((enabled) => { + Config.config.supportInvidious = enabled; + + if (enabled) { alert(chrome.i18n.getMessage("permissionRequestSuccess")); - - Config.config.ytInfoPermissionGranted = true; - - chrome.tabs.getCurrent((tab) => { - chrome.tabs.remove(tab.id); - }); + window.close(); } else { alert(chrome.i18n.getMessage("permissionRequestFailed")); } - }); + }) }); } \ No newline at end of file diff --git a/src/utils.ts b/src/utils.ts index fbfbbda7..ae390d78 100644 --- a/src/utils.ts +++ b/src/utils.ts @@ -210,6 +210,37 @@ export default class Utils { }); } + applyInvidiousPermissions(enable: boolean, option = "supportInvidious"): Promise { + return new Promise((resolve) => { + if (enable) { + this.setupExtraSitePermissions((granted) => { + if (!granted) { + Config.config[option] = false; + } + + resolve(granted); + }); + } else { + this.removeExtraSiteRegistration(); + resolve(false); + } + }); + } + + containsInvidiousPermission(): Promise { + return new Promise((resolve) => { + let permissions = ["declarativeContent"]; + if (this.isFirefox()) permissions = []; + + chrome.permissions.contains({ + origins: this.getPermissionRegex(), + permissions: permissions + }, function (result) { + resolve(result); + }); + }) + } + /** * Merges any overlapping timestamp ranges into single segments and returns them as a new array. */