2020-05-25 04:42:55 +02:00
|
|
|
import * as CompileConfig from "../config.json";
|
2020-02-15 05:16:01 +01:00
|
|
|
|
2020-02-04 08:10:59 +01:00
|
|
|
import Config from "./config";
|
2020-12-13 20:48:09 +01:00
|
|
|
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";
|
2020-12-14 23:37:35 +01:00
|
|
|
|
2020-02-15 05:16:01 +01:00
|
|
|
// Make the config public for debugging purposes
|
2020-12-14 23:37:35 +01:00
|
|
|
|
|
|
|
window.SB = Config;
|
2020-01-29 05:52:15 +01:00
|
|
|
|
2022-10-11 15:41:19 +02:00
|
|
|
import Utils from "./utils";
|
2020-12-12 22:57:41 +01:00
|
|
|
const utils = new Utils({
|
2020-02-02 00:47:36 +01:00
|
|
|
registerFirefoxContentScript,
|
|
|
|
unregisterFirefoxContentScript
|
|
|
|
});
|
2020-01-10 04:13:00 +01:00
|
|
|
|
2022-07-03 23:53:40 +02:00
|
|
|
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.
|
2020-12-12 22:57:41 +01:00
|
|
|
const contentScriptRegistrations = {};
|
2020-01-01 00:13:51 +01:00
|
|
|
|
2020-01-10 04:13:00 +01:00
|
|
|
// Register content script if needed
|
2023-02-07 03:22:34 +01:00
|
|
|
utils.wait(() => Config.config !== null).then(function() {
|
|
|
|
if (Config.config.supportInvidious) utils.setupExtraSiteContentScripts();
|
|
|
|
});
|
2020-01-10 04:13:00 +01:00
|
|
|
|
2023-02-14 07:20:46 +01:00
|
|
|
setupBackgroundRequestProxy();
|
2023-02-14 07:24:25 +01:00
|
|
|
setupTabUpdates(Config);
|
2019-07-09 05:43:06 +02:00
|
|
|
|
2022-07-03 23:53:40 +02:00
|
|
|
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":
|
2021-08-20 08:00:15 +02:00
|
|
|
chrome.tabs.create({url: chrome.runtime.getURL('help/index.html')});
|
2022-10-21 08:37:48 +02:00
|
|
|
return false;
|
2021-03-25 01:13:33 +01:00
|
|
|
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":
|
2020-05-25 04:42:55 +02:00
|
|
|
submitVote(request.type, request.UUID, request.category).then(callback);
|
2022-10-08 18:34:20 +02:00
|
|
|
|
2020-01-01 00:13:51 +01:00
|
|
|
//this allows the callback to be called later
|
|
|
|
return true;
|
2022-10-08 18:34:20 +02:00
|
|
|
case "registerContentScript":
|
2020-01-10 04:13:00 +01:00
|
|
|
registerFirefoxContentScript(request);
|
2020-01-01 00:13:51 +01:00
|
|
|
return false;
|
2022-10-08 18:34:20 +02:00
|
|
|
case "unregisterContentScript":
|
2020-02-02 00:47:36 +01:00
|
|
|
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
|
|
|
}
|
2022-07-03 23:53:40 +02:00
|
|
|
case "time":
|
2022-10-08 18:34:20 +02:00
|
|
|
case "infoUpdated":
|
2022-10-11 17:15:11 +02:00
|
|
|
case "videoChanged":
|
2022-07-03 23:53:40 +02:00
|
|
|
if (sender.tab) {
|
2022-07-04 06:49:32 +02:00
|
|
|
popupPort[sender.tab.id]?.postMessage(request);
|
2022-07-03 23:53:40 +02:00
|
|
|
}
|
|
|
|
return false;
|
2022-10-21 04:13:51 +02:00
|
|
|
default:
|
|
|
|
return false;
|
2019-08-12 18:41:26 +02:00
|
|
|
}
|
2019-07-09 21:55:33 +02:00
|
|
|
});
|
|
|
|
|
2023-05-08 23:02:49 +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,
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
2022-07-03 23:53:40 +02:00
|
|
|
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
|
2020-12-14 23:37:35 +01:00
|
|
|
chrome.runtime.onInstalled.addListener(function () {
|
2020-01-11 04:16:53 +01:00
|
|
|
// This let's the config sync to run fully before checking.
|
|
|
|
// This is required on Firefox
|
2022-08-16 22:00:34 +02:00
|
|
|
setTimeout(async () => {
|
2020-02-04 08:10:59 +01:00
|
|
|
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
|
2021-08-20 08:00:15 +02:00
|
|
|
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
|
2020-02-04 08:10:59 +01:00
|
|
|
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
|
|
|
}
|
2022-08-16 22:00:34 +02: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
|
|
|
});
|
2019-07-13 00:28:41 +02:00
|
|
|
|
2020-01-10 04:13:00 +01:00
|
|
|
/**
|
|
|
|
* Only works on Firefox.
|
|
|
|
* Firefox requires that it be applied after every extension restart.
|
2022-10-08 18:34:20 +02:00
|
|
|
*
|
|
|
|
* @param {JSON} options
|
2020-01-10 04:13:00 +01:00
|
|
|
*/
|
2020-12-13 20:48:09 +01:00
|
|
|
function registerFirefoxContentScript(options: Registration) {
|
2020-12-12 22:57:41 +01:00
|
|
|
const oldRegistration = contentScriptRegistrations[options.id];
|
2020-01-10 04:13:00 +01:00
|
|
|
if (oldRegistration) oldRegistration.unregister();
|
|
|
|
|
2023-03-18 05:28:26 +01:00
|
|
|
chrome.contentScripts.register({
|
2020-01-10 04:13:00 +01:00
|
|
|
allFrames: options.allFrames,
|
|
|
|
js: options.js,
|
|
|
|
css: options.css,
|
|
|
|
matches: options.matches
|
2020-01-11 23:48:11 +01:00
|
|
|
}).then((registration) => void (contentScriptRegistrations[options.id] = registration));
|
2020-01-10 04:13:00 +01:00
|
|
|
}
|
|
|
|
|
2020-02-02 00:47:36 +01:00
|
|
|
/**
|
|
|
|
* Only works on Firefox.
|
|
|
|
* Firefox requires that this is handled by the background script
|
2022-10-08 18:34:20 +02:00
|
|
|
*
|
2020-02-02 00:47:36 +01:00
|
|
|
*/
|
|
|
|
function unregisterFirefoxContentScript(id: string) {
|
2023-02-07 03:22:34 +01:00
|
|
|
if (contentScriptRegistrations[id]) {
|
|
|
|
contentScriptRegistrations[id].unregister();
|
|
|
|
delete contentScriptRegistrations[id];
|
|
|
|
}
|
2020-02-02 00:47:36 +01:00
|
|
|
}
|
|
|
|
|
2020-05-25 04:42:55 +02:00
|
|
|
async function submitVote(type: number, UUID: string, category: string) {
|
2020-02-04 08:10:59 +01:00
|
|
|
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();
|
2020-02-04 08:10:59 +01:00
|
|
|
Config.config.userID = userID;
|
2020-01-11 02:49:21 +01:00
|
|
|
}
|
2019-08-20 01:21:19 +02:00
|
|
|
|
2020-12-12 22:57:41 +01:00
|
|
|
const typeSection = (type !== undefined) ? "&type=" + type : "&category=" + category;
|
2020-05-10 22:44:53 +02:00
|
|
|
|
2020-01-11 02:49:21 +01:00
|
|
|
//publish this vote
|
2020-12-12 22:57:41 +01:00
|
|
|
const response = await asyncRequestToServer("POST", "/api/voteOnSponsorTime?UUID=" + UUID + "&userID=" + userID + typeSection);
|
2020-05-25 04:42:55 +02:00
|
|
|
|
|
|
|
if (response.ok) {
|
|
|
|
return {
|
2021-01-27 03:57:43 +01:00
|
|
|
successType: 1,
|
|
|
|
responseText: await response.text()
|
2020-05-25 04:42:55 +02:00
|
|
|
};
|
|
|
|
} else if (response.status == 405) {
|
|
|
|
//duplicate vote
|
|
|
|
return {
|
|
|
|
successType: 0,
|
2021-01-27 03:57:43 +01:00
|
|
|
statusCode: response.status,
|
|
|
|
responseText: await response.text()
|
2020-05-25 04:42:55 +02:00
|
|
|
};
|
|
|
|
} else {
|
|
|
|
//error while connect
|
|
|
|
return {
|
|
|
|
successType: -1,
|
2021-01-27 03:57:43 +01:00
|
|
|
statusCode: response.status,
|
|
|
|
responseText: await response.text()
|
2020-05-25 04:42:55 +02:00
|
|
|
};
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-02-14 07:20:46 +01:00
|
|
|
|
2020-05-25 04:42:55 +02:00
|
|
|
async function asyncRequestToServer(type: string, address: string, data = {}) {
|
2020-12-12 22:57:41 +01:00
|
|
|
const serverAddress = Config.config.testingServer ? CompileConfig.testingServerAddress : Config.config.serverAddress;
|
2020-05-25 04:42:55 +02:00
|
|
|
|
2023-02-14 07:20:46 +01:00
|
|
|
return await (sendRealRequestToCustomServer(type, serverAddress + address, data));
|
|
|
|
}
|