SponsorBlock/src/background.ts

207 lines
6.6 KiB
TypeScript
Raw Normal View History

import * as CompileConfig from "../config.json";
import Config from "./config";
import { Registration } from "./types";
2023-03-18 05:28:26 +01:00
import "content-scripts-register-polyfill";
2023-02-14 07:24:25 +01:00
import { sendRealRequestToCustomServer, setupBackgroundRequestProxy } from "@ajayyy/maze-utils/lib/background-request-proxy";
import { setupTabUpdates } from "@ajayyy/maze-utils/lib/tab-updates";
2023-02-18 08:54:42 +01:00
import { generateUserID } from "@ajayyy/maze-utils/lib/setup";
// Make the config public for debugging purposes
window.SB = Config;
2020-01-29 05:52:15 +01:00
2022-10-11 15:41:19 +02:00
import Utils from "./utils";
const utils = new Utils({
registerFirefoxContentScript,
unregisterFirefoxContentScript
});
const popupPort: Record<string, chrome.runtime.Port> = {};
2020-01-01 00:13:51 +01:00
// Used only on Firefox, which does not support non persistent background pages.
const contentScriptRegistrations = {};
2020-01-01 00:13:51 +01:00
// Register content script if needed
utils.wait(() => Config.config !== null).then(function() {
if (Config.config.supportInvidious) utils.setupExtraSiteContentScripts();
});
2023-02-14 07:20:46 +01:00
setupBackgroundRequestProxy();
2023-02-14 07:24:25 +01:00
setupTabUpdates(Config);
chrome.runtime.onMessage.addListener(function (request, sender, callback) {
2022-05-24 23:13:02 +02:00
switch(request.message) {
2020-01-13 20:54:24 +01:00
case "openConfig":
2021-05-11 07:09:01 +02:00
chrome.tabs.create({url: chrome.runtime.getURL('options/options.html' + (request.hash ? '#' + request.hash : ''))});
2022-10-21 08:37:48 +02:00
return false;
2020-12-13 01:07:15 +01:00
case "openHelp":
chrome.tabs.create({url: chrome.runtime.getURL('help/index.html')});
2022-10-21 08:37:48 +02:00
return false;
case "openPage":
chrome.tabs.create({url: chrome.runtime.getURL(request.url)});
2022-10-21 08:37:48 +02:00
return false;
2020-01-01 00:13:51 +01:00
case "submitVote":
submitVote(request.type, request.UUID, request.category).then(callback);
2020-01-01 00:13:51 +01:00
//this allows the callback to be called later
return true;
case "registerContentScript":
registerFirefoxContentScript(request);
2020-01-01 00:13:51 +01:00
return false;
case "unregisterContentScript":
unregisterFirefoxContentScript(request.id)
2020-01-01 00:13:51 +01:00
return false;
2022-06-06 23:09:34 +02:00
case "tabs": {
2022-06-03 00:47:03 +02:00
chrome.tabs.query({
active: true,
currentWindow: true
}, tabs => {
chrome.tabs.sendMessage(
tabs[0].id,
request.data,
2022-06-06 23:09:34 +02:00
(response) => {
callback(response);
}
2022-06-03 00:47:03 +02:00
);
2022-06-06 23:09:34 +02:00
});
2022-06-03 00:47:03 +02:00
return true;
2022-06-06 23:09:34 +02:00
}
case "time":
case "infoUpdated":
case "videoChanged":
if (sender.tab) {
2022-07-04 06:49:32 +02:00
popupPort[sender.tab.id]?.postMessage(request);
}
return false;
default:
return false;
}
2019-07-09 21:55:33 +02:00
});
chrome.runtime.onMessageExternal.addListener((request, sender, callback) => {
if (CompileConfig.extensionCommunicationAllowList.includes(sender.id)) {
if (request.message === "requestConfig") {
callback({
userID: Config.config.userID,
allowExpirements: Config.config.allowExpirements,
showDonationLink: Config.config.showDonationLink,
showUpsells: Config.config.showUpsells,
darkMode: Config.config.darkMode,
})
}
}
});
chrome.runtime.onConnect.addListener((port) => {
if (port.name === "popup") {
chrome.tabs.query({
active: true,
currentWindow: true
}, tabs => {
popupPort[tabs[0].id] = port;
});
}
});
2019-07-31 06:12:02 +02:00
//add help page on install
chrome.runtime.onInstalled.addListener(function () {
// This let's the config sync to run fully before checking.
// This is required on Firefox
setTimeout(async () => {
const userID = Config.config.userID;
2020-01-01 15:44:39 +01:00
// If there is no userID, then it is the first install.
if (!userID){
//open up the install page
chrome.tabs.create({url: chrome.extension.getURL("/help/index.html")});
2020-01-01 15:44:39 +01:00
//generate a userID
2023-02-18 08:54:42 +01:00
const newUserID = generateUserID();
2020-01-01 15:44:39 +01:00
//save this UUID
Config.config.userID = newUserID;
2022-01-07 02:08:12 +01:00
// Don't show update notification
Config.config.categoryPillUpdate = true;
2020-01-01 15:44:39 +01:00
}
if (Config.config.supportInvidious) {
if (!(await utils.containsInvidiousPermission())) {
chrome.tabs.create({url: chrome.extension.getURL("/permissions/index.html")});
}
}
2019-11-23 18:44:38 +01:00
}, 1500);
2019-07-31 06:12:02 +02:00
});
/**
* Only works on Firefox.
* Firefox requires that it be applied after every extension restart.
*
* @param {JSON} options
*/
function registerFirefoxContentScript(options: Registration) {
const oldRegistration = contentScriptRegistrations[options.id];
if (oldRegistration) oldRegistration.unregister();
2023-03-18 05:28:26 +01:00
chrome.contentScripts.register({
allFrames: options.allFrames,
js: options.js,
css: options.css,
matches: options.matches
}).then((registration) => void (contentScriptRegistrations[options.id] = registration));
}
/**
* Only works on Firefox.
* Firefox requires that this is handled by the background script
*
*/
function unregisterFirefoxContentScript(id: string) {
if (contentScriptRegistrations[id]) {
contentScriptRegistrations[id].unregister();
delete contentScriptRegistrations[id];
}
}
async function submitVote(type: number, UUID: string, category: string) {
let userID = Config.config.userID;
2020-01-11 02:49:21 +01:00
if (userID == undefined || userID === "undefined") {
//generate one
2023-02-18 08:54:42 +01:00
userID = generateUserID();
Config.config.userID = userID;
2020-01-11 02:49:21 +01:00
}
2019-08-20 01:21:19 +02:00
const typeSection = (type !== undefined) ? "&type=" + type : "&category=" + category;
2020-01-11 02:49:21 +01:00
//publish this vote
const response = await asyncRequestToServer("POST", "/api/voteOnSponsorTime?UUID=" + UUID + "&userID=" + userID + typeSection);
if (response.ok) {
return {
successType: 1,
responseText: await response.text()
};
} else if (response.status == 405) {
//duplicate vote
return {
successType: 0,
statusCode: response.status,
responseText: await response.text()
};
} else {
//error while connect
return {
successType: -1,
statusCode: response.status,
responseText: await response.text()
};
}
}
2023-02-14 07:20:46 +01:00
async function asyncRequestToServer(type: string, address: string, data = {}) {
const serverAddress = Config.config.testingServer ? CompileConfig.testingServerAddress : Config.config.serverAddress;
2023-02-14 07:20:46 +01:00
return await (sendRealRequestToCustomServer(type, serverAddress + address, data));
}