Add page for refreshing invidious permissions if it was revoked

Fixes #1354
This commit is contained in:
Ajay
2022-08-16 16:00:34 -04:00
parent eae8485713
commit faeb5dede0
6 changed files with 55 additions and 43 deletions

View File

@@ -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"

View File

@@ -19,6 +19,12 @@
<br/>
<div class="center">
__MSG_invidiousPermissionRefresh__
</div>
<br/>
<div class="center">
<div id="acceptPermissionButton" class="option-button inline">
__MSG_acceptPermission__

View File

@@ -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);
});

View File

@@ -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<void> {
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;
}
/**

View File

@@ -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"));
}
});
})
});
}

View File

@@ -210,6 +210,37 @@ export default class Utils {
});
}
applyInvidiousPermissions(enable: boolean, option = "supportInvidious"): Promise<boolean> {
return new Promise((resolve) => {
if (enable) {
this.setupExtraSitePermissions((granted) => {
if (!granted) {
Config.config[option] = false;
}
resolve(granted);
});
} else {
this.removeExtraSiteRegistration();
resolve(false);
}
});
}
containsInvidiousPermission(): Promise<boolean> {
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.
*/