mirror of
https://github.com/ajayyy/SponsorBlock.git
synced 2025-12-07 03:57:09 +03:00
Prompt to accept youtube.com permission if video info fails to load
Should fix #698, #687, #611 and #635
(cherry picked from commit 3ff5fdb3a1)
This commit is contained in:
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