mirror of
https://github.com/ajayyy/SponsorBlock.git
synced 2025-12-24 16:38:47 +03:00
Prompt to accept youtube.com permission if video info fails to load
Should fix #698, #687, #611 and #635
This commit is contained in:
@@ -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({
|
||||
|
||||
@@ -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("#", "");
|
||||
|
||||
@@ -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
33
src/permissions.ts
Normal 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"));
|
||||
}
|
||||
});
|
||||
});
|
||||
}
|
||||
41
src/utils.ts
41
src/utils.ts
@@ -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;
|
||||
|
||||
Reference in New Issue
Block a user