mirror of
https://github.com/ajayyy/SponsorBlock.git
synced 2025-12-08 04:27:15 +03:00
Added support for the on page popup.
This commit is contained in:
@@ -1,6 +1,8 @@
|
|||||||
import Utils from "./utils";
|
import Utils from "./utils";
|
||||||
import SB from "./SB";
|
import SB from "./SB";
|
||||||
|
|
||||||
|
import runThePopup from "./popup.js";
|
||||||
|
|
||||||
import PreviewBar from "./js-components/previewBar";
|
import PreviewBar from "./js-components/previewBar";
|
||||||
import SkipNotice 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
|
//get messages from the background script and the popup
|
||||||
chrome.runtime.onMessage.addListener(messageListener);
|
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
|
//messages from popup script
|
||||||
switch(request.message){
|
switch(request.message){
|
||||||
case "update":
|
case "update":
|
||||||
@@ -578,7 +580,7 @@ function skipToTime(v, index, sponsorTimes, openNotice) {
|
|||||||
|
|
||||||
//send telemetry that a this sponsor was skipped
|
//send telemetry that a this sponsor was skipped
|
||||||
if (SB.config.trackViewCount && !sponsorSkipped[index]) {
|
if (SB.config.trackViewCount && !sponsorSkipped[index]) {
|
||||||
sendRequestToServer("POST", "/api/viewedVideoSponsorTime?UUID=" + currentUUID);
|
Utils.sendRequestToServer("POST", "/api/viewedVideoSponsorTime?UUID=" + currentUUID);
|
||||||
|
|
||||||
if (!SB.config.disableAutoSkip) {
|
if (!SB.config.disableAutoSkip) {
|
||||||
// Count this as a skip
|
// Count this as a skip
|
||||||
@@ -806,7 +808,7 @@ function openInfoMenu() {
|
|||||||
parentNode.insertBefore(popup, parentNode.firstChild);
|
parentNode.insertBefore(popup, parentNode.firstChild);
|
||||||
|
|
||||||
//run the popup init script
|
//run the popup init script
|
||||||
runThePopup();
|
runThePopup(messageListener);
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
@@ -1055,25 +1057,6 @@ function getFormattedTime(seconds) {
|
|||||||
return formatted;
|
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) {
|
function sendRequestToCustomServer(type, fullAddress, callback) {
|
||||||
let xmlhttp = new XMLHttpRequest();
|
let xmlhttp = new XMLHttpRequest();
|
||||||
|
|
||||||
|
|||||||
35
src/popup.ts
35
src/popup.ts
@@ -1,23 +1,27 @@
|
|||||||
import Utils from "./utils";
|
import Utils from "./utils";
|
||||||
import SB from "./SB";
|
import SB from "./SB";
|
||||||
|
|
||||||
class MessageHandler {
|
interface MessageListener {
|
||||||
onContentScript: boolean;
|
(request: any, sender: any, callback: (response: any) => void): void;
|
||||||
|
}
|
||||||
|
|
||||||
constructor (onContentScript: boolean = false) {
|
class MessageHandler {
|
||||||
this.onContentScript = onContentScript;
|
messageListener: MessageListener;
|
||||||
|
|
||||||
|
constructor (messageListener?: MessageListener) {
|
||||||
|
this.messageListener = messageListener;
|
||||||
}
|
}
|
||||||
|
|
||||||
sendMessage(id: number, request, callback?) {
|
sendMessage(id: number, request, callback?) {
|
||||||
if (this.onContentScript) {
|
if (this.messageListener) {
|
||||||
messageListener(request, null, callback);
|
this.messageListener(request, null, callback);
|
||||||
} else {
|
} else {
|
||||||
chrome.tabs.sendMessage(id, request. callback);
|
chrome.tabs.sendMessage(id, request, callback);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
query(config, callback) {
|
query(config, callback) {
|
||||||
if (this.onContentScript) {
|
if (this.messageListener) {
|
||||||
// Send back dummy info
|
// Send back dummy info
|
||||||
callback([{
|
callback([{
|
||||||
url: document.URL,
|
url: document.URL,
|
||||||
@@ -30,21 +34,12 @@ class MessageHandler {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
//make this a function to allow this to run on the content page
|
||||||
|
async function runThePopup(messageListener?: MessageListener) {
|
||||||
var messageHandler = new MessageHandler();
|
var messageHandler = new MessageHandler();
|
||||||
|
|
||||||
//make this a function to allow this to run on the content page
|
|
||||||
async function runThePopup() {
|
|
||||||
Utils.localizeHtmlPage();
|
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);
|
await Utils.wait(() => SB.config !== undefined);
|
||||||
|
|
||||||
var OptionsElements: any = {};
|
var OptionsElements: any = {};
|
||||||
@@ -1128,3 +1123,5 @@ if (chrome.tabs != undefined) {
|
|||||||
//this means it is actually opened in the popup
|
//this means it is actually opened in the popup
|
||||||
runThePopup();
|
runThePopup();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
export default runThePopup;
|
||||||
@@ -269,7 +269,14 @@ class Utils {
|
|||||||
return errorMessage;
|
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();
|
let xmlhttp = new XMLHttpRequest();
|
||||||
|
|
||||||
xmlhttp.open(type, CompileConfig.serverAddress + address, true);
|
xmlhttp.open(type, CompileConfig.serverAddress + address, true);
|
||||||
|
|||||||
Reference in New Issue
Block a user