mirror of
https://github.com/ajayyy/SponsorBlock.git
synced 2025-12-13 06:57:09 +03:00
Compare commits
54 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
88350e3421 | ||
|
|
36d79313de | ||
|
|
a9993d5d80 | ||
|
|
4a6ddf6774 | ||
|
|
b6172c6d9b | ||
|
|
b21a59f4e5 | ||
|
|
78dd44c502 | ||
|
|
f4a129b346 | ||
|
|
6d60a90533 | ||
|
|
0d33794636 | ||
|
|
e62d46f2dd | ||
|
|
7f36c7eec3 | ||
|
|
08d28798c6 | ||
|
|
fe33277d8f | ||
|
|
feec5b4e22 | ||
|
|
62b5e90d87 | ||
|
|
2c980a269d | ||
|
|
1da60e38a1 | ||
|
|
ed67cc52fe | ||
|
|
b614dce91a | ||
|
|
c013c7ef0f | ||
|
|
c78e2cd214 | ||
|
|
da5a3841bd | ||
|
|
e73d79071c | ||
|
|
0467dd5d21 | ||
|
|
5f879bceab | ||
|
|
457bd15e17 | ||
|
|
cc1b8ee499 | ||
|
|
4a9ed1348e | ||
|
|
5595420be6 | ||
|
|
191e9ceb6f | ||
|
|
1e26faa57c | ||
|
|
030256c9e1 | ||
|
|
77eff12d9b | ||
|
|
2967fce013 | ||
|
|
db1def623a | ||
|
|
140e324bf1 | ||
|
|
f0bf051259 | ||
|
|
2ec47d52cd | ||
|
|
50002cfbbd | ||
|
|
6ccd4b8b37 | ||
|
|
c0894afff9 | ||
|
|
09f244150c | ||
|
|
efec8b320c | ||
|
|
e6ea9f77e9 | ||
|
|
0813aa4ba3 | ||
|
|
8d82a6a3e6 | ||
|
|
88a8fda566 | ||
|
|
995fe072bd | ||
|
|
8cdbebd6de | ||
|
|
94af8ab301 | ||
|
|
be3a4a4e91 | ||
|
|
1c17464c94 | ||
|
|
8896c5707a |
12
.github/workflows/release.yml
vendored
12
.github/workflows/release.yml
vendored
@@ -1,6 +1,8 @@
|
||||
name: Upload Release Build
|
||||
|
||||
on: release
|
||||
on:
|
||||
release:
|
||||
types: [published]
|
||||
|
||||
jobs:
|
||||
|
||||
@@ -65,12 +67,12 @@ jobs:
|
||||
uses: Shopify/upload-to-release@master
|
||||
with:
|
||||
args: builds/ChromeExtension.zip
|
||||
env:
|
||||
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
|
||||
name: ChromeExtension.zip
|
||||
repo-token: ${{ secrets.GITHUB_TOKEN }}
|
||||
- name: Upload to release
|
||||
uses: Shopify/upload-to-release@master
|
||||
with:
|
||||
args: builds/FirefoxExtension.zip
|
||||
env:
|
||||
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
|
||||
name: FirefoxExtension.zip
|
||||
repo-token: ${{ secrets.GITHUB_TOKEN }}
|
||||
|
||||
|
||||
24
README.md
24
README.md
@@ -40,32 +40,30 @@ The backend server code is available here: https://github.com/ajayyy/SponsorBloc
|
||||
|
||||
It is a simple Sqlite database that will hold all the timing data.
|
||||
|
||||
To make sure that this project doesn't die, I have made the database publicly downloadable at https://api.sponsor.ajay.app/database.db. So, you can download a backup or get archive.org to take a backup for you if you want.
|
||||
To make sure that this project doesn't die, I have made the database publicly downloadable at https://sponsor.ajay.app/database.db. You can download a backup or get archive.org to take a backup for you if you want.
|
||||
|
||||
Hopefully this project can be combined with projects like [this](https://github.com/Sponsoff/sponsorship_remover) and use this data to create a neural network to predict when sponsored segments happen. That project is sadly abandoned now, so I have decided to attempt to revive this idea.
|
||||
The dataset and API are now being used in some [ports](https://github.com/ajayyy/SponsorBlock/wiki/Unofficial-Ports) as well as a [neural network](https://github.com/andrewzlee/NeuralBlock).
|
||||
|
||||
A [previous project](https://github.com/Sponsoff/sponsorship_remover) attempted to create a neural network to predict when sponsored segments happen. That project is sadly abandoned now, so I have decided to attempt to revive this idea starting from a crowd-sourced system instead.
|
||||
|
||||
# API
|
||||
|
||||
You can read the API docs [here](https://github.com/ajayyy/SponsorBlockServer#api-docs).
|
||||
|
||||
# Build Yourself
|
||||
# Building
|
||||
|
||||
You can load this project as an unpacked extension. Make sure to rename the `config.json.example` file to `config.json` before installing.
|
||||
There are also other build scripts available. Install `npm`, then run `npm install` in the repository to install dependencies.
|
||||
|
||||
There are also other build scripts available. Install `npm`, then run `npm install` in the repository.
|
||||
Run `npm run build` to generate a Chrome extension.
|
||||
|
||||
Use `npm run build:firefox` to generate a Firefox extension.
|
||||
|
||||
The result is in `dist`. This can be loaded as an unpacked extension
|
||||
|
||||
## Developing with a clean profile
|
||||
|
||||
Run `npm run dev` to run the extension using a clean browser profile with hot reloading. Use `npm run dev:firefox` for Firefox. This uses [`web-ext run`](https://extensionworkshop.com/documentation/develop/web-ext-command-reference/#commands).
|
||||
|
||||
## Packing
|
||||
|
||||
Run `npm run build` to generate a packed Chrome extension.
|
||||
|
||||
Use `npm run build:firefox` to generate a Firefox extension.
|
||||
|
||||
The result is in `dist`.
|
||||
|
||||
# Credit
|
||||
|
||||
The awesome [Invidious API](https://github.com/omarroth/invidious/wiki/API) previously was used.
|
||||
|
||||
@@ -1,7 +1,7 @@
|
||||
{
|
||||
"name": "__MSG_fullName__",
|
||||
"short_name": "__MSG_Name__",
|
||||
"version": "1.2.20",
|
||||
"version": "1.2.24",
|
||||
"default_locale": "en",
|
||||
"description": "__MSG_Description__",
|
||||
"content_scripts": [{
|
||||
|
||||
@@ -425,5 +425,41 @@
|
||||
},
|
||||
"mobileUpdateInfo": {
|
||||
"message": "m.youtube.com is now supported"
|
||||
},
|
||||
"exportOptions": {
|
||||
"message": "Import/Export All Options"
|
||||
},
|
||||
"whatExportOptions": {
|
||||
"message": "This is your entire configuration in JSON. This includes your userID, so be sure to share this wisely."
|
||||
},
|
||||
"setOptions": {
|
||||
"message": "Set Options"
|
||||
},
|
||||
"exportOptionsWarning": {
|
||||
"message": "Warning: Changing the options is permanent and can break your install. Are you sure you would like to do this? Make sure to backup your old one just in case."
|
||||
},
|
||||
"incorrectlyFormattedOptions": {
|
||||
"message": "This JSON is not formatted correctly. Your options have not been changed."
|
||||
},
|
||||
"copyDebugInformation": {
|
||||
"message": "Copy Debug Information To Clipboard"
|
||||
},
|
||||
"copyDebugInformationFailed": {
|
||||
"message": "Failed to write to clipboard"
|
||||
},
|
||||
"copyDebugInformationOptions": {
|
||||
"message": "Copies information to the clipboard to be provided to a developer when raising a bug / when a developer requests it. Sensitive information such as your user ID, whitelisted channels, and custom server address have been removed. However it does contain information such as your useragent, browser, operating system, and extension version number. "
|
||||
},
|
||||
"copyDebugInformationComplete": {
|
||||
"message": "The debug information has been copied to the clip board. Feel free to remove any information you would rather not share. Save this in a text file or paste into the bug report."
|
||||
},
|
||||
"theKey": {
|
||||
"message": "The key"
|
||||
},
|
||||
"keyAlreadyUsedByYouTube": {
|
||||
"message": "is already used by youtube. Please select another key."
|
||||
},
|
||||
"keyAlreadyUsed": {
|
||||
"message": "is bound to another action. Please select another key."
|
||||
}
|
||||
}
|
||||
|
||||
@@ -307,6 +307,46 @@
|
||||
|
||||
<br/>
|
||||
<br/>
|
||||
|
||||
<div option-type="private-text-change" sync-option="*" confirm-message="exportOptionsWarning">
|
||||
<div class="option-button trigger-button">
|
||||
__MSG_exportOptions__
|
||||
</div>
|
||||
|
||||
<br/>
|
||||
|
||||
<div class="small-description">__MSG_whatExportOptions__</div>
|
||||
|
||||
<div class="option-hidden-section hidden">
|
||||
<br/>
|
||||
|
||||
<input class="option-text-box" type="text">
|
||||
|
||||
<br/>
|
||||
<br/>
|
||||
|
||||
<div class="option-button text-change-set">
|
||||
__MSG_setOptions__
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
<br/>
|
||||
<br/>
|
||||
|
||||
<div option-type="button-press" sync-option="copyDebugInformation" confirm-message="copyDebugInformation">
|
||||
<div class="option-button trigger-button">
|
||||
__MSG_copyDebugInformation__
|
||||
</div>
|
||||
|
||||
<br/>
|
||||
|
||||
<div class="small-description">__MSG_copyDebugInformationOptions__</div>
|
||||
</div>
|
||||
|
||||
<br/>
|
||||
<br/>
|
||||
|
||||
|
||||
<div option-type="text-change" sync-option="serverAddress">
|
||||
<label class="text-label-container">
|
||||
|
||||
@@ -1,5 +1,8 @@
|
||||
import * as Types from "./types";
|
||||
|
||||
import Config from "./config";
|
||||
// Make the config public for debugging purposes
|
||||
(<any> window).SB = Config;
|
||||
|
||||
import Utils from "./utils";
|
||||
var utils = new Utils({
|
||||
@@ -85,10 +88,6 @@ chrome.runtime.onInstalled.addListener(function (object) {
|
||||
const newUserID = utils.generateUserID();
|
||||
//save this UUID
|
||||
Config.config.userID = newUserID;
|
||||
|
||||
//TODO: Remove when mobile support is old
|
||||
// Don't show this to new users
|
||||
// Config.config.mobileUpdateShowCount = 1;
|
||||
}
|
||||
}, 1500);
|
||||
});
|
||||
|
||||
@@ -33,6 +33,10 @@ interface SBObject {
|
||||
defaults: SBConfig;
|
||||
localConfig: SBConfig;
|
||||
config: SBConfig;
|
||||
|
||||
// Functions
|
||||
encodeStoredItem<T>(data: T): T | Array<any>;
|
||||
convertJSON(): void;
|
||||
}
|
||||
|
||||
// Allows a SBMap to be conveted into json form
|
||||
@@ -84,13 +88,8 @@ class SBMap<T, U> extends Map {
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
toJSON() {
|
||||
return Array.from(this.entries());
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
var Config: SBObject = {
|
||||
/**
|
||||
* Callback function when an option is updated
|
||||
@@ -124,21 +123,25 @@ var Config: SBObject = {
|
||||
mobileUpdateShowCount: 0
|
||||
},
|
||||
localConfig: null,
|
||||
config: null
|
||||
config: null,
|
||||
|
||||
// Functions
|
||||
encodeStoredItem,
|
||||
convertJSON
|
||||
};
|
||||
|
||||
// Function setup
|
||||
|
||||
/**
|
||||
* A SBMap cannot be stored in the chrome storage.
|
||||
* This data will be encoded into an array instead as specified by the toJSON function.
|
||||
* This data will be encoded into an array instead
|
||||
*
|
||||
* @param data
|
||||
*/
|
||||
function encodeStoredItem(data) {
|
||||
function encodeStoredItem<T>(data: T): T | Array<any> {
|
||||
// if data is SBMap convert to json for storing
|
||||
if(!(data instanceof SBMap)) return data;
|
||||
return JSON.stringify(data);
|
||||
return Array.from(data.entries());
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -147,19 +150,31 @@ function encodeStoredItem(data) {
|
||||
*
|
||||
* @param {*} data
|
||||
*/
|
||||
function decodeStoredItem(id: string, data) {
|
||||
if(typeof data !== "string") return data;
|
||||
|
||||
try {
|
||||
let str = JSON.parse(data);
|
||||
|
||||
if(!Array.isArray(str)) return data;
|
||||
return new SBMap(id, str);
|
||||
} catch(e) {
|
||||
function decodeStoredItem<T>(id: string, data: T): T | SBMap<string, any> {
|
||||
if (!Config.defaults[id]) return data;
|
||||
|
||||
// If all else fails, return the data
|
||||
return data;
|
||||
if (Config.defaults[id] instanceof SBMap) {
|
||||
try {
|
||||
let jsonData: any = data;
|
||||
|
||||
// Check if data is stored in the old format for SBMap (a JSON string)
|
||||
if (typeof data === "string") {
|
||||
try {
|
||||
jsonData = JSON.parse(data);
|
||||
} catch(e) {
|
||||
// Continue normally (out of this if statement)
|
||||
}
|
||||
}
|
||||
|
||||
if (!Array.isArray(jsonData)) return data;
|
||||
return new SBMap(id, jsonData);
|
||||
} catch(e) {
|
||||
console.error("Failed to parse SBMap: " + id);
|
||||
}
|
||||
}
|
||||
|
||||
// If all else fails, return the data
|
||||
return data;
|
||||
}
|
||||
|
||||
function configProxy(): any {
|
||||
@@ -232,7 +247,7 @@ function resetConfig() {
|
||||
Config.config = Config.defaults;
|
||||
};
|
||||
|
||||
function convertJSON() {
|
||||
function convertJSON(): void {
|
||||
Object.keys(Config.localConfig).forEach(key => {
|
||||
Config.localConfig[key] = decodeStoredItem(key, Config.localConfig[key]);
|
||||
});
|
||||
|
||||
131
src/content.ts
131
src/content.ts
@@ -21,7 +21,7 @@ var UUIDs = [];
|
||||
var sponsorVideoID = null;
|
||||
|
||||
// Skips are scheduled to ensure precision.
|
||||
// Skips are rescheduled every seeked event.
|
||||
// Skips are rescheduled every seeking event.
|
||||
// Skips are canceled every seeking event
|
||||
var currentSkipSchedule: NodeJS.Timeout = null;
|
||||
var seekListenerSetUp = false
|
||||
@@ -35,6 +35,9 @@ var sponsorSkipped = [];
|
||||
//the video
|
||||
var video: HTMLVideoElement;
|
||||
|
||||
/** The last time this video was seeking to */
|
||||
var lastVideoTime: number = null;
|
||||
|
||||
var onInvidious;
|
||||
var onMobileYouTube;
|
||||
|
||||
@@ -45,7 +48,12 @@ var lastPreviewBarUpdate;
|
||||
var durationListenerSetUp = false;
|
||||
|
||||
// Is the video currently being switched
|
||||
var switchingVideos = false;
|
||||
var switchingVideos = null;
|
||||
|
||||
// Used by the play and playing listeners to make sure two aren't
|
||||
// called at the same time
|
||||
var lastCheckTime = 0;
|
||||
var lastCheckVideoTime = -1;
|
||||
|
||||
//the channel this video is about
|
||||
var channelURL;
|
||||
@@ -238,6 +246,9 @@ document.onkeydown = function(e: KeyboardEvent){
|
||||
}
|
||||
|
||||
function resetValues() {
|
||||
lastCheckTime = 0;
|
||||
lastCheckVideoTime = -1;
|
||||
|
||||
//reset sponsor times
|
||||
sponsorTimes = null;
|
||||
UUIDs = [];
|
||||
@@ -250,6 +261,13 @@ function resetValues() {
|
||||
|
||||
//reset sponsor data found check
|
||||
sponsorDataFound = false;
|
||||
|
||||
if (switchingVideos === null) {
|
||||
// When first loading a video, it is not switching videos
|
||||
switchingVideos = false;
|
||||
} else {
|
||||
switchingVideos = true;
|
||||
}
|
||||
}
|
||||
|
||||
async function videoIDChange(id) {
|
||||
@@ -264,8 +282,6 @@ async function videoIDChange(id) {
|
||||
//id is not valid
|
||||
if (!id) return;
|
||||
|
||||
switchingVideos = true;
|
||||
|
||||
// Wait for options to be ready
|
||||
await utils.wait(() => Config.config !== null, 5000, 1);
|
||||
|
||||
@@ -444,12 +460,13 @@ function cancelSponsorSchedule(): void {
|
||||
*/
|
||||
function startSponsorSchedule(currentTime?: number): void {
|
||||
cancelSponsorSchedule();
|
||||
if (video.paused) return;
|
||||
|
||||
if (Config.config.disableSkipping || channelWhitelisted){
|
||||
return;
|
||||
}
|
||||
|
||||
if (currentTime === undefined) currentTime = video.currentTime;
|
||||
if (currentTime === undefined || currentTime === null) currentTime = video.currentTime;
|
||||
|
||||
let skipInfo = getNextSkipIndex(currentTime);
|
||||
|
||||
@@ -459,11 +476,31 @@ function startSponsorSchedule(currentTime?: number): void {
|
||||
let timeUntilSponsor = skipTime[0] - currentTime;
|
||||
|
||||
let skippingFunction = () => {
|
||||
let forcedSkipTime: number = null;
|
||||
|
||||
if (video.currentTime >= skipTime[0] && video.currentTime < skipTime[1]) {
|
||||
skipToTime(video, skipInfo.index, skipInfo.array, skipInfo.openNotice);
|
||||
// Double check that the videoID is correct
|
||||
// TODO: Remove this bug catching if statement when the bug is found
|
||||
let currentVideoID = getYouTubeVideoID(document.URL);
|
||||
if (currentVideoID == sponsorVideoID) {
|
||||
skipToTime(video, skipInfo.index, skipInfo.array, skipInfo.openNotice);
|
||||
|
||||
if (Config.config.disableAutoSkip) {
|
||||
forcedSkipTime = skipTime[0] + 0.001;
|
||||
} else {
|
||||
forcedSkipTime = skipTime[1];
|
||||
}
|
||||
} else {
|
||||
// Something has really gone wrong
|
||||
console.error("[SponsorBlock] The videoID recorded when trying to skip is different than what it should be.");
|
||||
console.error("[SponsorBlock] VideoID recorded: " + sponsorVideoID + ". Actual VideoID: " + currentVideoID);
|
||||
|
||||
// Video ID change occured
|
||||
videoIDChange(currentVideoID);
|
||||
}
|
||||
}
|
||||
|
||||
startSponsorSchedule(skipTime[0] + 0.001);
|
||||
startSponsorSchedule(forcedSkipTime);
|
||||
};
|
||||
|
||||
if (timeUntilSponsor <= 0) {
|
||||
@@ -493,14 +530,45 @@ function sponsorsLookup(id: string, channelIDPromise?) {
|
||||
|
||||
video.addEventListener('play', () => {
|
||||
switchingVideos = false;
|
||||
startSponsorSchedule();
|
||||
|
||||
// Make sure it doesn't get double called with the playing event
|
||||
if (lastCheckVideoTime !== video.currentTime && Date.now() - lastCheckTime > 2000) {
|
||||
lastCheckTime = Date.now();
|
||||
lastCheckVideoTime = video.currentTime;
|
||||
|
||||
startSponsorSchedule();
|
||||
}
|
||||
});
|
||||
video.addEventListener('seeked', () => {
|
||||
if (!video.paused) startSponsorSchedule();
|
||||
video.addEventListener('playing', () => {
|
||||
// Make sure it doesn't get double called with the play event
|
||||
if (lastCheckVideoTime !== video.currentTime && Date.now() - lastCheckTime > 2000) {
|
||||
lastCheckTime = Date.now();
|
||||
lastCheckVideoTime = video.currentTime;
|
||||
|
||||
startSponsorSchedule();
|
||||
}
|
||||
});
|
||||
video.addEventListener('seeking', () => {
|
||||
// Reset lastCheckVideoTime
|
||||
lastCheckVideoTime = -1
|
||||
lastCheckTime = 0;
|
||||
|
||||
lastVideoTime = video.currentTime;
|
||||
|
||||
if (!video.paused){
|
||||
startSponsorSchedule();
|
||||
}
|
||||
});
|
||||
video.addEventListener('ratechange', () => startSponsorSchedule());
|
||||
video.addEventListener('seeking', cancelSponsorSchedule);
|
||||
video.addEventListener('pause', cancelSponsorSchedule);
|
||||
video.addEventListener('pause', () => {
|
||||
// Reset lastCheckVideoTime
|
||||
lastCheckVideoTime = -1;
|
||||
lastCheckTime = 0;
|
||||
|
||||
lastVideoTime = video.currentTime;
|
||||
|
||||
cancelSponsorSchedule();
|
||||
});
|
||||
|
||||
startSponsorSchedule();
|
||||
}
|
||||
@@ -555,26 +623,26 @@ function sponsorsLookup(id: string, channelIDPromise?) {
|
||||
UUIDs = smallUUIDs;
|
||||
}
|
||||
|
||||
// See if there are any zero second sponsors
|
||||
let zeroSecondSponsor = false;
|
||||
for (const time of sponsorTimes) {
|
||||
if (time[0] <= 0) {
|
||||
zeroSecondSponsor = true;
|
||||
break;
|
||||
}
|
||||
}
|
||||
if (!zeroSecondSponsor) {
|
||||
for (const time of sponsorTimesSubmitting) {
|
||||
if (time[0] <= 0) {
|
||||
zeroSecondSponsor = true;
|
||||
if (!switchingVideos) {
|
||||
// See if there are any starting sponsors
|
||||
let startingSponsor: number = -1;
|
||||
for (const time of sponsorTimes) {
|
||||
if (time[0] <= video.currentTime && time[0] > startingSponsor && time[1] > video.currentTime) {
|
||||
startingSponsor = time[0];
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
if (!startingSponsor) {
|
||||
for (const time of sponsorTimesSubmitting) {
|
||||
if (time[0] <= video.currentTime && time[0] > startingSponsor && time[1] > video.currentTime) {
|
||||
startingSponsor = time[0];
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if (!video.paused && !switchingVideos) {
|
||||
if (zeroSecondSponsor) {
|
||||
startSponsorSchedule(0);
|
||||
if (startingSponsor !== -1) {
|
||||
startSponsorSchedule(startingSponsor);
|
||||
} else {
|
||||
startSponsorSchedule();
|
||||
}
|
||||
@@ -834,13 +902,6 @@ function skipToTime(v, index, sponsorTimes, openNotice) {
|
||||
|
||||
let skipNotice = new SkipNotice(this, currentUUID, Config.config.disableAutoSkip, skipNoticeContentContainer);
|
||||
|
||||
//TODO: Remove this when Mobile support is old
|
||||
if (Config.config.mobileUpdateShowCount < 1) {
|
||||
skipNotice.addNoticeInfoMessage(chrome.i18n.getMessage("mobileUpdateInfo"));
|
||||
|
||||
Config.config.mobileUpdateShowCount += 1;
|
||||
}
|
||||
|
||||
//auto-upvote this sponsor
|
||||
if (Config.config.trackViewCount && !Config.config.disableAutoSkip && Config.config.autoUpvote) {
|
||||
vote(1, currentUUID, null);
|
||||
|
||||
@@ -1,5 +1,7 @@
|
||||
'use strict';
|
||||
|
||||
import Config from "../config";
|
||||
|
||||
/**
|
||||
* The notice that tells the user that a sponsor was just skipped
|
||||
*/
|
||||
@@ -305,7 +307,7 @@ class SkipNotice {
|
||||
if (this.manualSkip) {
|
||||
this.changeNoticeTitle(chrome.i18n.getMessage("noticeTitle"));
|
||||
|
||||
this.contentContainer().vote(1, this.UUID, this);
|
||||
if (Config.config.autoUpvote) this.contentContainer().vote(1, this.UUID);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
177
src/options.ts
177
src/options.ts
@@ -1,4 +1,8 @@
|
||||
import Config from "./config";
|
||||
import * as CompileConfig from "../config.json";
|
||||
|
||||
// Make the config public for debugging purposes
|
||||
(<any> window).SB = Config;
|
||||
|
||||
import Utils from "./utils";
|
||||
var utils = new Utils();
|
||||
@@ -72,7 +76,7 @@ async function init() {
|
||||
|
||||
textChangeInput.value = Config.config[textChangeOption];
|
||||
|
||||
textChangeSetButton.addEventListener("click", () => {
|
||||
textChangeSetButton.addEventListener("click", async () => {
|
||||
// See if anything extra must be done
|
||||
switch (textChangeOption) {
|
||||
case "serverAddress":
|
||||
@@ -84,6 +88,18 @@ async function init() {
|
||||
return;
|
||||
}
|
||||
|
||||
// Permission needed on Firefox
|
||||
if (utils.isFirefox()) {
|
||||
let permissionSuccess = await new Promise((resolve, reject) => {
|
||||
chrome.permissions.request({
|
||||
origins: [textChangeInput.value + "/"],
|
||||
permissions: []
|
||||
}, resolve);
|
||||
});
|
||||
|
||||
if (!permissionSuccess) return;
|
||||
}
|
||||
|
||||
break;
|
||||
}
|
||||
|
||||
@@ -112,6 +128,16 @@ async function init() {
|
||||
invidiousInstanceAddInit(<HTMLElement> optionsElements[i], privateTextChangeOption);
|
||||
}
|
||||
|
||||
break;
|
||||
case "button-press":
|
||||
let actionButton = optionsElements[i].querySelector(".trigger-button");
|
||||
|
||||
switch(optionsElements[i].getAttribute("sync-option")) {
|
||||
case "copyDebugInformation":
|
||||
actionButton.addEventListener("click", copyDebugOutputToClipboard);
|
||||
break;
|
||||
}
|
||||
|
||||
break;
|
||||
case "keybind-change":
|
||||
let keybindButton = optionsElements[i].querySelector(".trigger-button");
|
||||
@@ -259,6 +285,8 @@ function invidiousOnClick(checkbox: HTMLInputElement, option: string) {
|
||||
if (!granted) {
|
||||
Config.config[option] = false;
|
||||
checkbox.checked = false;
|
||||
} else {
|
||||
checkbox.checked = true;
|
||||
}
|
||||
});
|
||||
} else {
|
||||
@@ -291,7 +319,7 @@ function activateKeybindChange(element: HTMLElement) {
|
||||
|
||||
element.querySelector(".option-hidden-section").classList.remove("hidden");
|
||||
|
||||
document.addEventListener("keydown", (e) => keybindKeyPressed(element, e), {once: true});
|
||||
document.addEventListener("keydown", (e) => keybindKeyPressed(element, e), {once: true});
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -303,25 +331,60 @@ function activateKeybindChange(element: HTMLElement) {
|
||||
function keybindKeyPressed(element: HTMLElement, e: KeyboardEvent) {
|
||||
var key = e.key;
|
||||
|
||||
let button = element.querySelector(".trigger-button");
|
||||
if (["Shift", "Control", "Meta", "Alt", "ArrowUp", "ArrowDown", "ArrowLeft", "ArrowRight", "Tab"].indexOf(key) !== -1) {
|
||||
|
||||
// Wait for more
|
||||
document.addEventListener("keydown", (e) => keybindKeyPressed(element, e), {once: true});
|
||||
} else {
|
||||
let button: HTMLElement = element.querySelector(".trigger-button");
|
||||
let option = element.getAttribute("sync-option");
|
||||
|
||||
// Don't allow keys which are already listened for by youtube
|
||||
let restrictedKeys = "1234567890,.jklftcibmJKLFTCIBMNP/<> -+";
|
||||
if (restrictedKeys.indexOf(key) !== -1 ) {
|
||||
closeKeybindOption(element, button);
|
||||
|
||||
alert(chrome.i18n.getMessage("theKey") + " " + key + " " + chrome.i18n.getMessage("keyAlreadyUsedByYouTube"));
|
||||
return;
|
||||
}
|
||||
|
||||
// Make sure keybind isn't used by the other listener
|
||||
// TODO: If other keybindings are going to be added, we need a better way to find the other keys used.
|
||||
let otherKeybind = (option === "startSponsorKeybind") ? Config.config['submitKeybind'] : Config.config['startSponsorKeybind'];
|
||||
if (key === otherKeybind) {
|
||||
closeKeybindOption(element, button);
|
||||
|
||||
alert(chrome.i18n.getMessage("theKey") + " " + key + " " + chrome.i18n.getMessage("keyAlreadyUsed"));
|
||||
return;
|
||||
}
|
||||
|
||||
// cancel setting a keybind
|
||||
if (key === "Escape") {
|
||||
closeKeybindOption(element, button);
|
||||
|
||||
return;
|
||||
}
|
||||
|
||||
Config.config[option] = key;
|
||||
|
||||
let status = <HTMLElement> element.querySelector(".option-hidden-section > .keybind-status");
|
||||
status.innerText = chrome.i18n.getMessage("keybindDescriptionComplete");
|
||||
|
||||
let statusKey = <HTMLElement> element.querySelector(".option-hidden-section > .keybind-status-key");
|
||||
statusKey.innerText = key;
|
||||
|
||||
// cancel setting a keybind
|
||||
if (key === "Escape") {
|
||||
element.querySelector(".option-hidden-section").classList.add("hidden");
|
||||
button.classList.remove("disabled");
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
||||
let option = element.getAttribute("sync-option");
|
||||
|
||||
Config.config[option] = key;
|
||||
|
||||
let status = <HTMLElement> element.querySelector(".option-hidden-section > .keybind-status");
|
||||
status.innerText = chrome.i18n.getMessage("keybindDescriptionComplete");
|
||||
|
||||
let statusKey = <HTMLElement> element.querySelector(".option-hidden-section > .keybind-status-key");
|
||||
statusKey.innerText = key;
|
||||
|
||||
/**
|
||||
* Closes the menu for editing the keybind
|
||||
*
|
||||
* @param element
|
||||
* @param button
|
||||
*/
|
||||
function closeKeybindOption(element: HTMLElement, button: HTMLElement) {
|
||||
element.querySelector(".option-hidden-section").classList.add("hidden");
|
||||
button.classList.remove("disabled");
|
||||
}
|
||||
|
||||
@@ -345,15 +408,57 @@ function activatePrivateTextChange(element: HTMLElement) {
|
||||
element.querySelector(".option-hidden-section").classList.remove("hidden");
|
||||
return;
|
||||
}
|
||||
|
||||
textBox.value = Config.config[option];
|
||||
|
||||
let result = Config.config[option];
|
||||
|
||||
// See if anything extra must be done
|
||||
switch (option) {
|
||||
case "*":
|
||||
let jsonData = JSON.parse(JSON.stringify(Config.localConfig));
|
||||
|
||||
// Fix sponsorTimes data as it is destroyed from the JSON stringify
|
||||
jsonData.sponsorTimes = Config.encodeStoredItem(Config.localConfig.sponsorTimes);
|
||||
|
||||
result = JSON.stringify(jsonData);
|
||||
break;
|
||||
}
|
||||
|
||||
textBox.value = result;
|
||||
|
||||
let setButton = element.querySelector(".text-change-set");
|
||||
setButton.addEventListener("click", () => {
|
||||
let confirmMessage = element.getAttribute("confirm-message");
|
||||
|
||||
if (confirmMessage === null || confirm(chrome.i18n.getMessage(confirmMessage))) {
|
||||
Config.config[option] = textBox.value;
|
||||
|
||||
// See if anything extra must be done
|
||||
switch (option) {
|
||||
case "*":
|
||||
try {
|
||||
let newConfig = JSON.parse(textBox.value);
|
||||
for (const key in newConfig) {
|
||||
Config.config[key] = newConfig[key];
|
||||
}
|
||||
Config.convertJSON();
|
||||
|
||||
// Reload options on page
|
||||
init();
|
||||
|
||||
if (newConfig.supportInvidious) {
|
||||
let checkbox = <HTMLInputElement> document.querySelector("#support-invidious > label > label > input");
|
||||
|
||||
checkbox.checked = true;
|
||||
invidiousOnClick(checkbox, "supportInvidious");
|
||||
}
|
||||
|
||||
} catch (e) {
|
||||
alert(chrome.i18n.getMessage("incorrectlyFormattedOptions"));
|
||||
}
|
||||
|
||||
break;
|
||||
default:
|
||||
Config.config[option] = textBox.value;
|
||||
}
|
||||
}
|
||||
});
|
||||
|
||||
@@ -381,4 +486,36 @@ function validateServerAddress(input: string): string {
|
||||
}
|
||||
|
||||
return input;
|
||||
}
|
||||
|
||||
function copyDebugOutputToClipboard() {
|
||||
// Build output debug information object
|
||||
let output = {
|
||||
debug: {
|
||||
userAgent: navigator.userAgent,
|
||||
platform: navigator.platform,
|
||||
language: navigator.language,
|
||||
extensionVersion: chrome.runtime.getManifest().version
|
||||
},
|
||||
config: JSON.parse(JSON.stringify(Config.localConfig)) // Deep clone config object
|
||||
};
|
||||
|
||||
// Fix sponsorTimes data as it is destroyed from the JSON stringify
|
||||
output.config.sponsorTimes = Config.encodeStoredItem(Config.localConfig.sponsorTimes);
|
||||
|
||||
// Sanitise sensitive user config values
|
||||
delete output.config.userID;
|
||||
output.config.serverAddress = (output.config.serverAddress === CompileConfig.serverAddress)
|
||||
? "Default server address" : "Custom server address";
|
||||
output.config.invidiousInstances = output.config.invidiousInstances.length;
|
||||
output.config.whitelistedChannels = output.config.whitelistedChannels.length;
|
||||
|
||||
// Copy object to clipboard
|
||||
navigator.clipboard.writeText(JSON.stringify(output, null, 4))
|
||||
.then(() => {
|
||||
alert(chrome.i18n.getMessage("copyDebugInformationComplete"));
|
||||
})
|
||||
.catch(err => {
|
||||
alert(chrome.i18n.getMessage("copyDebugInformationFailed"));
|
||||
});;
|
||||
}
|
||||
Reference in New Issue
Block a user