Added support for the on page popup.

This commit is contained in:
Ajay Ramachandran
2020-02-01 17:36:02 -05:00
parent 932cf8ecf1
commit de66e21d14
3 changed files with 30 additions and 43 deletions

View File

@@ -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();

View File

@@ -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;

View File

@@ -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);