Prompt to accept youtube.com permission if video info fails to load

Should fix #698, #687, #611 and #635
This commit is contained in:
Ajay Ramachandran
2021-03-24 20:13:33 -04:00
parent cc935624e9
commit 3ff5fdb3a1
9 changed files with 483 additions and 21 deletions

View File

@@ -37,6 +37,9 @@ chrome.runtime.onMessage.addListener(function (request, sender, callback) {
case "openHelp":
chrome.tabs.create({url: chrome.runtime.getURL('help/index_en.html')});
return;
case "openPage":
chrome.tabs.create({url: chrome.runtime.getURL(request.url)});
return;
case "sendRequest":
sendRequestToCustomServer(request.type, request.url, request.data).then(async (response) => {
callback({

View File

@@ -258,7 +258,7 @@ async function videoIDChange(id) {
try {
await utils.wait(() => !!videoInfo, 5000, 1);
} catch (err) {
alert(chrome.i18n.getMessage("adblockerIssue") + "\n\n" + chrome.i18n.getMessage("adblockerIssueUnlistedVideosInfo"));
await videoInfoFetchFailed("adblockerIssueUnlistedVideosInfo");
}
if (isUnlisted()) {
@@ -268,7 +268,11 @@ async function videoIDChange(id) {
}
// Update whitelist data when the video data is loaded
utils.wait(() => !!videoInfo, 5000, 10).then(whitelistCheck);
utils.wait(() => !!videoInfo, 5000, 10).then(whitelistCheck).catch(() => {
if (Config.config.forceChannelCheck) {
videoInfoFetchFailed("adblockerIssueWhitelist");
}
});
//setup the preview bar
if (previewBar === null) {
@@ -727,6 +731,21 @@ async function getVideoInfo(): Promise<void> {
}
}
async function videoInfoFetchFailed(errorMessage: string): Promise<void> {
console.log("failed\t" + errorMessage)
if (utils.isFirefox()) {
// Attempt to ask permission for youtube.com domain
alert(chrome.i18n.getMessage("youtubePermissionRequest"));
chrome.runtime.sendMessage({
message: "openPage",
url: "permissions/index.html#youtube.com"
});
} else {
alert(chrome.i18n.getMessage("adblockerIssue") + "\n\n" + chrome.i18n.getMessage(errorMessage));
}
}
function getYouTubeVideoID(url: string) {
// For YouTube TV support
if(url.startsWith("https://www.youtube.com/tv#/")) url = url.replace("#", "");

View File

@@ -288,7 +288,7 @@ function invidiousInit(checkbox: HTMLInputElement, option: string) {
if (utils.isFirefox()) permissions = [];
chrome.permissions.contains({
origins: utils.getInvidiousInstancesRegex(),
origins: utils.getPermissionRegex(),
permissions: permissions
}, function (result) {
if (result != checkbox.checked) {

33
src/permissions.ts Normal file
View File

@@ -0,0 +1,33 @@
import Config from "./config";
import Utils from "./utils";
const utils = new Utils();
// This is needed, if Config is not imported before Utils, things break.
// Probably due to cyclic dependencies
Config.config;
window.addEventListener('DOMContentLoaded', init);
async function init() {
utils.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) {
alert(chrome.i18n.getMessage("permissionRequestSuccess"));
chrome.tabs.getCurrent((tab) => {
chrome.tabs.remove(tab.id);
});
} else {
alert(chrome.i18n.getMessage("permissionRequestFailed"));
}
});
});
}

View File

@@ -3,10 +3,10 @@ import { CategorySelection, SponsorTime, FetchResponse, BackgroundScriptContaine
import * as CompileConfig from "../config.json";
class Utils {
export default class Utils {
// Contains functions needed from the background script
backgroundScriptContainer: BackgroundScriptContainer | null = null;
backgroundScriptContainer: BackgroundScriptContainer | null;
// Used to add content scripts and CSS required
js = [
@@ -19,7 +19,7 @@ class Utils {
"popup.css"
];
constructor(backgroundScriptContainer?: BackgroundScriptContainer) {
constructor(backgroundScriptContainer: BackgroundScriptContainer = null) {
this.backgroundScriptContainer = backgroundScriptContainer;
}
@@ -43,6 +43,12 @@ class Utils {
});
}
containsPermission(permissions: chrome.permissions.Permissions): Promise<boolean> {
return new Promise((resolve) => {
chrome.permissions.contains(permissions, resolve)
});
}
/**
* Asks for the optional permissions required for all extra sites.
* It also starts the content script registrations.
@@ -57,7 +63,7 @@ class Utils {
if (this.isFirefox()) permissions = [];
chrome.permissions.request({
origins: this.getInvidiousInstancesRegex(),
origins: this.getPermissionRegex(),
permissions: permissions
}, async (granted) => {
if (granted) {
@@ -78,7 +84,6 @@ class Utils {
* For now, it is just SB.config.invidiousInstances.
*/
setupExtraSiteContentScripts(): void {
if (this.isFirefox()) {
const firefoxJS = [];
for (const file of this.js) {
@@ -95,7 +100,7 @@ class Utils {
allFrames: true,
js: firefoxJS,
css: firefoxCSS,
matches: this.getInvidiousInstancesRegex()
matches: this.getPermissionRegex()
};
if (this.backgroundScriptContainer) {
@@ -106,7 +111,7 @@ class Utils {
} else {
chrome.declarativeContent.onPageChanged.removeRules(["invidious"], () => {
const conditions = [];
for (const regex of this.getInvidiousInstancesRegex()) {
for (const regex of this.getPermissionRegex()) {
conditions.push(new chrome.declarativeContent.PageStateMatcher({
pageUrl: { urlMatches: regex }
}));
@@ -149,7 +154,7 @@ class Utils {
}
chrome.permissions.remove({
origins: this.getInvidiousInstancesRegex()
origins: this.getPermissionRegex()
});
}
@@ -250,16 +255,20 @@ class Utils {
}
/**
* @returns {String[]} Invidious Instances in regex form
* @returns {String[]} Domains in regex form
*/
getInvidiousInstancesRegex(): string[] {
const invidiousInstancesRegex: string[] = [];
for (const url of Config.config.invidiousInstances) {
invidiousInstancesRegex.push("https://*." + url + "/*");
invidiousInstancesRegex.push("http://*." + url + "/*");
getPermissionRegex(domains: string[] = []): string[] {
const permissionRegex: string[] = [];
if (domains.length === 0) {
domains = [...Config.config.invidiousInstances];
}
return invidiousInstancesRegex;
for (const url of domains) {
permissionRegex.push("https://*." + url + "/*");
permissionRegex.push("http://*." + url + "/*");
}
return permissionRegex;
}
generateUserID(length = 36): string {
@@ -434,5 +443,3 @@ class Utils {
}
}
export default Utils;