mirror of
https://github.com/ajayyy/SponsorBlock.git
synced 2025-12-10 13:37:04 +03:00
Added migration code for whitelisted channelIDs vs URLs
This commit is contained in:
@@ -1,10 +1,13 @@
|
|||||||
import * as CompileConfig from "../config.json";
|
import * as CompileConfig from "../config.json";
|
||||||
import { CategorySelection, CategorySkipOption } from "./types";
|
import { CategorySelection, CategorySkipOption } from "./types";
|
||||||
|
|
||||||
|
import Utils from "./utils";
|
||||||
|
const utils = new Utils();
|
||||||
|
|
||||||
interface SBConfig {
|
interface SBConfig {
|
||||||
userID: string,
|
userID: string,
|
||||||
sponsorTimes: SBMap<string, any>,
|
sponsorTimes: SBMap<string, any>,
|
||||||
whitelistedChannels: Array<any>,
|
whitelistedChannels: string[],
|
||||||
startSponsorKeybind: string,
|
startSponsorKeybind: string,
|
||||||
submitKeybind: string,
|
submitKeybind: string,
|
||||||
minutesSaved: number,
|
minutesSaved: number,
|
||||||
@@ -236,7 +239,7 @@ function fetchConfig() {
|
|||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
function migrateOldFormats() {
|
async function migrateOldFormats() {
|
||||||
if (Config.config["disableAutoSkip"]) {
|
if (Config.config["disableAutoSkip"]) {
|
||||||
for (const selection of Config.config.categorySelections) {
|
for (const selection of Config.config.categorySelections) {
|
||||||
if (selection.name === "sponsor") {
|
if (selection.name === "sponsor") {
|
||||||
@@ -246,6 +249,36 @@ function migrateOldFormats() {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Channel URLS
|
||||||
|
if (Config.config.whitelistedChannels.length > 0 &&
|
||||||
|
(Config.config.whitelistedChannels[0].includes("/") || Config.config.whitelistedChannels[0] == null)) {
|
||||||
|
let newChannelList: string[] = [];
|
||||||
|
for (const item of Config.config.whitelistedChannels) {
|
||||||
|
if (item != null) {
|
||||||
|
if (item.includes("/channel/")) {
|
||||||
|
newChannelList.push(item.split("/")[2]);
|
||||||
|
} else if (item.includes("/user/") && utils.isContentScript()) {
|
||||||
|
// Replace channel URL with channelID
|
||||||
|
let response = await utils.asyncRequestToCustomServer("GET", "https://sponsor.ajay.app/invidious/api/v1/channels/" + item.split("/")[2] + "?fields=authorId");
|
||||||
|
|
||||||
|
if (response.ok) {
|
||||||
|
newChannelList.push((await response.json()).authorId);
|
||||||
|
} else {
|
||||||
|
// Add it at the beginning so it gets converted later
|
||||||
|
newChannelList.unshift(item);
|
||||||
|
}
|
||||||
|
} else if (item.includes("/user/")) {
|
||||||
|
// Add it at the beginning so it gets converted later (The API can only be called in the content script due to CORS issues)
|
||||||
|
newChannelList.unshift(item);
|
||||||
|
} else {
|
||||||
|
newChannelList.push(item);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
Config.config.whitelistedChannels = newChannelList;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
async function setupConfig() {
|
async function setupConfig() {
|
||||||
|
|||||||
30
src/utils.ts
30
src/utils.ts
@@ -270,27 +270,26 @@ class Utils {
|
|||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Sends a request to the SponsorBlock server with address added as a query
|
* Sends a request to a custom server
|
||||||
*
|
*
|
||||||
* @param type The request type. "GET", "POST", etc.
|
* @param type The request type. "GET", "POST", etc.
|
||||||
* @param address The address to add to the SponsorBlock server address
|
* @param address The address to add to the SponsorBlock server address
|
||||||
* @param callback
|
* @param callback
|
||||||
*/
|
*/
|
||||||
async asyncRequestToServer(type: string, address: string, data = {}) {
|
async asyncRequestToCustomServer(type: string, url: string, data = {}) {
|
||||||
let serverAddress = Config.config.testingServer ? CompileConfig.testingServerAddress : Config.config.serverAddress;
|
|
||||||
|
|
||||||
// If GET, convert JSON to parameters
|
// If GET, convert JSON to parameters
|
||||||
if (type.toLowerCase() === "get") {
|
if (type.toLowerCase() === "get") {
|
||||||
for (const key in data) {
|
for (const key in data) {
|
||||||
let seperator = address.includes("?") ? "&" : "?";
|
let seperator = url.includes("?") ? "&" : "?";
|
||||||
let value = (typeof(data[key]) === "string") ? data[key]: JSON.stringify(data[key]);
|
let value = (typeof(data[key]) === "string") ? data[key]: JSON.stringify(data[key]);
|
||||||
address += seperator + key + "=" + value;
|
url += seperator + key + "=" + value;
|
||||||
}
|
}
|
||||||
|
|
||||||
data = null;
|
data = null;
|
||||||
}
|
}
|
||||||
|
|
||||||
const response = await fetch(serverAddress + address, {
|
const response = await fetch(url, {
|
||||||
method: type,
|
method: type,
|
||||||
headers: {
|
headers: {
|
||||||
'Content-Type': 'application/json'
|
'Content-Type': 'application/json'
|
||||||
@@ -302,6 +301,19 @@ class Utils {
|
|||||||
return response;
|
return response;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Sends a request to the SponsorBlock server with address added as a query
|
||||||
|
*
|
||||||
|
* @param type The request type. "GET", "POST", etc.
|
||||||
|
* @param address The address to add to the SponsorBlock server address
|
||||||
|
* @param callback
|
||||||
|
*/
|
||||||
|
async asyncRequestToServer(type: string, address: string, data = {}) {
|
||||||
|
let serverAddress = Config.config.testingServer ? CompileConfig.testingServerAddress : Config.config.serverAddress;
|
||||||
|
|
||||||
|
return await (this.asyncRequestToCustomServer(type, serverAddress + address, data));
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Sends a request to the SponsorBlock server with address added as a query
|
* Sends a request to the SponsorBlock server with address added as a query
|
||||||
*
|
*
|
||||||
@@ -361,10 +373,14 @@ class Utils {
|
|||||||
return minutes * 60 + seconds;
|
return minutes * 60 + seconds;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
isContentScript(): boolean {
|
||||||
|
return window.location.protocol === "http:" || window.location.protocol === "https:";
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Is this Firefox (web-extensions)
|
* Is this Firefox (web-extensions)
|
||||||
*/
|
*/
|
||||||
isFirefox() {
|
isFirefox(): boolean {
|
||||||
return typeof(browser) !== "undefined";
|
return typeof(browser) !== "undefined";
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user