diff options
author | Ajay Ramachandran <[email protected]> | 2020-05-24 22:42:55 -0400 |
---|---|---|
committer | Ajay Ramachandran <[email protected]> | 2020-05-24 22:42:55 -0400 |
commit | 39155fdf999989947efebdcb8e283c4ef2d35fa0 (patch) | |
tree | 679b3d3eb6edce5a2b5b420413c5cdce3002fb93 /src/background.ts | |
parent | f165b3b602c822cfbd4fcd797d9d4e8653932ddd (diff) | |
download | SponsorBlock-39155fdf999989947efebdcb8e283c4ef2d35fa0.tar.gz SponsorBlock-39155fdf999989947efebdcb8e283c4ef2d35fa0.zip |
Moved requests to the background script.
This should avoid ad blockers messing with requests.
Helps with https://github.com/ajayyy/SponsorBlock/issues/354
Diffstat (limited to 'src/background.ts')
-rw-r--r-- | src/background.ts | 88 |
1 files changed, 67 insertions, 21 deletions
diff --git a/src/background.ts b/src/background.ts index 74382357..0c11ab06 100644 --- a/src/background.ts +++ b/src/background.ts @@ -1,4 +1,4 @@ -import * as Types from "./types"; +import * as CompileConfig from "../config.json"; import Config from "./config"; // Make the config public for debugging purposes @@ -30,7 +30,17 @@ chrome.runtime.onMessage.addListener(function (request, sender, callback) { switch(request.message) { case "openConfig": chrome.runtime.openOptionsPage(); - return + return; + case "sendRequest": + sendRequestToCustomServer(request.type, request.url, request.data).then(async (response) => { + callback({ + responseText: await response.text(), + status: response.status, + ok: response.ok + }); + }); + + return true; case "addSponsorTime": addSponsorTime(request.time, request.videoID, callback); @@ -47,7 +57,7 @@ chrome.runtime.onMessage.addListener(function (request, sender, callback) { //this allows the callback to be called later return true; case "submitVote": - submitVote(request.type, request.UUID, request.category, callback); + submitVote(request.type, request.UUID, request.category).then(callback); //this allows the callback to be called later return true; @@ -147,7 +157,7 @@ function addSponsorTime(time, videoID, callback) { }); } -function submitVote(type, UUID, category, callback) { +async function submitVote(type: number, UUID: string, category: string) { let userID = Config.config.userID; if (userID == undefined || userID === "undefined") { @@ -159,24 +169,60 @@ function submitVote(type, UUID, category, callback) { let typeSection = (type !== undefined) ? "&type=" + type : "&category=" + category; //publish this vote - utils.sendRequestToServer("POST", "/api/voteOnSponsorTime?UUID=" + UUID + "&userID=" + userID + typeSection, function(xmlhttp, error) { - if (xmlhttp.readyState == 4 && xmlhttp.status == 200) { - callback({ - successType: 1 - }); - } else if (xmlhttp.readyState == 4 && xmlhttp.status == 405) { - //duplicate vote - callback({ - successType: 0, - statusCode: xmlhttp.status - }); - } else if (error) { - //error while connect - callback({ - successType: -1, - statusCode: xmlhttp.status - }); + let response = await asyncRequestToServer("POST", "/api/voteOnSponsorTime?UUID=" + UUID + "&userID=" + userID + typeSection); + + if (response.ok) { + return { + successType: 1 + }; + } else if (response.status == 405) { + //duplicate vote + return { + successType: 0, + statusCode: response.status + }; + } else { + //error while connect + return { + successType: -1, + statusCode: response.status + }; + } +} + +async function asyncRequestToServer(type: string, address: string, data = {}) { + let serverAddress = Config.config.testingServer ? CompileConfig.testingServerAddress : Config.config.serverAddress; + + return await (sendRequestToCustomServer(type, serverAddress + address, data)); +} + +/** + * Sends a request to the specified url + * + * @param type The request type "GET", "POST", etc. + * @param address The address to add to the SponsorBlock server address + * @param callback + */ +async function sendRequestToCustomServer(type: string, url: string, data = {}) { + // If GET, convert JSON to parameters + if (type.toLowerCase() === "get") { + for (const key in data) { + let seperator = url.includes("?") ? "&" : "?"; + let value = (typeof(data[key]) === "string") ? data[key]: JSON.stringify(data[key]); + url += seperator + key + "=" + value; } + data = null; + } + + const response = await fetch(url, { + method: type, + headers: { + 'Content-Type': 'application/json' + }, + redirect: 'follow', + body: data ? JSON.stringify(data) : null }); + + return response; }
\ No newline at end of file |