From de66e21d148e9a59b835b1c9c48aebbb5dbe522d Mon Sep 17 00:00:00 2001 From: Ajay Ramachandran Date: Sat, 1 Feb 2020 17:36:02 -0500 Subject: [PATCH] Added support for the on page popup. --- src/content.ts | 27 +++++---------------------- src/popup.ts | 37 +++++++++++++++++-------------------- src/utils.ts | 9 ++++++++- 3 files changed, 30 insertions(+), 43 deletions(-) diff --git a/src/content.ts b/src/content.ts index 2e3f7c73..88eba234 100644 --- a/src/content.ts +++ b/src/content.ts @@ -1,6 +1,8 @@ import Utils from "./utils"; import SB from "./SB"; +import runThePopup from "./popup.js"; + import PreviewBar from "./js-components/previewBar"; import SkipNotice from "./js-components/previewBar"; @@ -74,7 +76,7 @@ var popupInitialised = false; //get messages from the background script and the popup chrome.runtime.onMessage.addListener(messageListener); -function messageListener(request, sender, sendResponse) { +function messageListener(request: any, sender: any, sendResponse: (response: any) => void): void { //messages from popup script switch(request.message){ case "update": @@ -578,7 +580,7 @@ function skipToTime(v, index, sponsorTimes, openNotice) { //send telemetry that a this sponsor was skipped if (SB.config.trackViewCount && !sponsorSkipped[index]) { - sendRequestToServer("POST", "/api/viewedVideoSponsorTime?UUID=" + currentUUID); + Utils.sendRequestToServer("POST", "/api/viewedVideoSponsorTime?UUID=" + currentUUID); if (!SB.config.disableAutoSkip) { // Count this as a skip @@ -806,7 +808,7 @@ function openInfoMenu() { parentNode.insertBefore(popup, parentNode.firstChild); //run the popup init script - runThePopup(); + runThePopup(messageListener); } }); } @@ -1055,25 +1057,6 @@ function getFormattedTime(seconds) { return formatted; } -function sendRequestToServer(type: string, address: string, callback = null) { - let xmlhttp = new XMLHttpRequest(); - - xmlhttp.open(type, serverAddress + address, true); - - if (callback !== null) { - xmlhttp.onreadystatechange = function () { - callback(xmlhttp, false); - }; - - xmlhttp.onerror = function(ev) { - callback(xmlhttp, true); - }; - } - - //submit this request - xmlhttp.send(); -} - function sendRequestToCustomServer(type, fullAddress, callback) { let xmlhttp = new XMLHttpRequest(); diff --git a/src/popup.ts b/src/popup.ts index e5e0bfc2..a2c46c5f 100644 --- a/src/popup.ts +++ b/src/popup.ts @@ -1,23 +1,27 @@ import Utils from "./utils"; import SB from "./SB"; -class MessageHandler { - onContentScript: boolean; +interface MessageListener { + (request: any, sender: any, callback: (response: any) => void): void; +} - constructor (onContentScript: boolean = false) { - this.onContentScript = onContentScript; +class MessageHandler { + messageListener: MessageListener; + + constructor (messageListener?: MessageListener) { + this.messageListener = messageListener; } sendMessage(id: number, request, callback?) { - if (this.onContentScript) { - messageListener(request, null, callback); + if (this.messageListener) { + this.messageListener(request, null, callback); } else { - chrome.tabs.sendMessage(id, request. callback); + chrome.tabs.sendMessage(id, request, callback); } } query(config, callback) { - if (this.onContentScript) { + if (this.messageListener) { // Send back dummy info callback([{ url: document.URL, @@ -30,21 +34,12 @@ class MessageHandler { } } -var messageHandler = new MessageHandler(); - //make this a function to allow this to run on the content page -async function runThePopup() { +async function runThePopup(messageListener?: MessageListener) { + var messageHandler = new MessageHandler(); + Utils.localizeHtmlPage(); - //is it in the popup or content script - var inPopup = true; - if (chrome.tabs == undefined) { - //this is on the content script, use direct communication - messageHandler = new MessageHandler(true); - - inPopup = false; - } - await Utils.wait(() => SB.config !== undefined); var OptionsElements: any = {}; @@ -1128,3 +1123,5 @@ if (chrome.tabs != undefined) { //this means it is actually opened in the popup runThePopup(); } + +export default runThePopup; \ No newline at end of file diff --git a/src/utils.ts b/src/utils.ts index 838e1996..ad8bcfac 100644 --- a/src/utils.ts +++ b/src/utils.ts @@ -269,7 +269,14 @@ class Utils { return errorMessage; } - static sendRequestToServer(type, address, callback) { + /** + * 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 + */ + static sendRequestToServer(type: string, address: string, callback?: (xmlhttp: XMLHttpRequest, err: boolean) => any) { let xmlhttp = new XMLHttpRequest(); xmlhttp.open(type, CompileConfig.serverAddress + address, true);