Merge branch 'master' of github.com:ajayyy/SponsorBlock

This commit is contained in:
mmble
2020-09-23 20:22:27 +02:00
37 changed files with 3773 additions and 252 deletions

View File

@@ -218,6 +218,14 @@ class SponsorTimeEditComponent extends React.Component<SponsorTimeEditProps, Spo
</span>
): ""}
{(!isNaN(segment[1])) ? (
<span id={"sponsorTimeInspectButton" + this.idSuffix}
className="sponsorTimeEditButton"
onClick={this.inspectTime.bind(this)}>
{chrome.i18n.getMessage("inspect")}
</span>
): ""}
{(!isNaN(segment[1])) ? (
<span id={"sponsorTimeEditButton" + this.idSuffix}
className="sponsorTimeEditButton"
@@ -346,6 +354,20 @@ class SponsorTimeEditComponent extends React.Component<SponsorTimeEditProps, Spo
this.props.contentContainer().previewTime(skipTime - 2);
}
inspectTime(): void {
let sponsorTimes = this.props.contentContainer().sponsorTimesSubmitting;
let index = this.props.index;
let skipTime = sponsorTimes[index].segment[0];
if (this.state.editing) {
// Save edits before inspecting
this.saveEditTimes();
}
this.props.contentContainer().previewTime(skipTime + 0.000001, false);
}
deleteTime(): void {
let sponsorTimes = this.props.contentContainer().sponsorTimesSubmitting;
let index = this.props.index;

View File

@@ -6,7 +6,6 @@ const utils = new Utils();
interface SBConfig {
userID: string,
// sponsorTimes: SBMap<string, SponsorTime[]>,
segmentTimes: SBMap<string, SponsorTime[]>,
defaultCategory: string,
whitelistedChannels: string[],
@@ -35,6 +34,8 @@ interface SBConfig {
audioNotificationOnSkip,
checkForUnlistedVideos: boolean,
testingServer: boolean,
hashPrefix: boolean,
refetchWhenNotFound: boolean,
// What categories should be skipped
categorySelections: CategorySelection[],
@@ -159,13 +160,15 @@ var Config: SBObject = {
hideUploadButtonPlayerControls: false,
hideDiscordLaunches: 0,
hideDiscordLink: false,
invidiousInstances: ["invidio.us", "invidious.snopyta.org"],
invidiousInstances: ["invidious.snopyta.org"],
supportInvidious: false,
serverAddress: CompileConfig.serverAddress,
minDuration: 0,
audioNotificationOnSkip: false,
checkForUnlistedVideos: false,
testingServer: false,
hashPrefix: false,
refetchWhenNotFound: true,
categorySelections: [{
name: "sponsor",

View File

@@ -618,12 +618,36 @@ function sponsorsLookup(id: string) {
categories.push(categorySelection.name);
}
utils.asyncRequestToServer('GET', "/api/skipSegments", {
videoID: id,
categories
}).then(async (response: FetchResponse) => {
// Check for hashPrefix setting
let getRequest;
if (Config.config.hashPrefix) {
getRequest = utils.asyncRequestToServer('GET', "/api/skipSegments/" + utils.getHash(id, 1).substr(0,4), {
categories
});
} else {
getRequest = utils.asyncRequestToServer('GET', "/api/skipSegments", {
videoID: id,
categories
});
}
getRequest.then(async (response: FetchResponse) => {
if (response?.ok) {
let recievedSegments: SponsorTime[] = JSON.parse(response.responseText);
let result = JSON.parse(response.responseText);
if (Config.config.hashPrefix) {
result = result.filter((video) => video.videoID === id);
if (result.length > 0) {
result = result[0].segments;
if (result.length === 0) { // return if no segments found
retryFetch(id);
return;
}
} else { // return if no video found
retryFetch(id);
return;
}
}
let recievedSegments: SponsorTime[] = result;
if (!recievedSegments.length) {
console.error("[SponsorBlock] Server returned malformed response: " + JSON.stringify(recievedSegments));
return;
@@ -667,32 +691,38 @@ function sponsorsLookup(id: string) {
sponsorLookupRetries = 0;
} else if (response?.status === 404) {
sponsorDataFound = false;
//check if this video was uploaded recently
utils.wait(() => !!videoInfo).then(() => {
let dateUploaded = videoInfo?.microformat?.playerMicroformatRenderer?.uploadDate;
//if less than 3 days old
if (Date.now() - new Date(dateUploaded).getTime() < 259200000) {
//TODO lower when server becomes better
setTimeout(() => sponsorsLookup(id), 180000);
}
});
sponsorLookupRetries = 0;
retryFetch(id);
} else if (sponsorLookupRetries < 90 && !recheckStarted) {
recheckStarted = true;
//TODO lower when server becomes better (back to 1 second)
//some error occurred, try again in a second
setTimeout(() => sponsorsLookup(id), 10000);
setTimeout(() => sponsorsLookup(id), 10000 + Math.random() * 30000);
sponsorLookupRetries++;
}
});
}
function retryFetch(id: string): void {
if (!Config.config.refetchWhenNotFound) return;
sponsorDataFound = false;
//check if this video was uploaded recently
utils.wait(() => !!videoInfo).then(() => {
let dateUploaded = videoInfo?.microformat?.playerMicroformatRenderer?.uploadDate;
//if less than 3 days old
if (Date.now() - new Date(dateUploaded).getTime() < 259200000) {
//TODO lower when server becomes better
setTimeout(() => sponsorsLookup(id), 120000);
}
});
sponsorLookupRetries = 0;
}
/**
* Only should be used when it is okay to skip a sponsor when in the middle of it
*
@@ -962,11 +992,11 @@ function getStartTimes(sponsorTimes: SponsorTime[], includeIntersectingSegments:
*
* @param time
*/
function previewTime(time: number) {
function previewTime(time: number, unpause = true) {
video.currentTime = time;
// Unpause the video if needed
if (video.paused){
if (unpause && video.paused){
video.play();
}
}

View File

@@ -16,7 +16,7 @@ interface ContentContainer {
sponsorSubmissionNotice: SubmissionNotice,
resetSponsorSubmissionNotice: () => void,
changeStartSponsorButton: (showStartSponsor: any, uploadButtonVisible: any) => Promise<boolean>,
previewTime: (time: number) => void,
previewTime: (time: number, unpause?: boolean) => void,
videoInfo: any,
getRealCurrentTime: () => number
}

View File

@@ -1,5 +1,6 @@
import Config from "./config";
import { CategorySelection, SponsorTime, FetchResponse } from "./types";
import { sha256 } from 'js-sha256';
import * as CompileConfig from "../config.json";
@@ -378,6 +379,20 @@ class Utils {
isFirefox(): boolean {
return typeof(browser) !== "undefined";
}
getHash(value: string, times=5000): string {
if (times <= 0) return "";
for (let i = 0; i < times; i++) {
let hash = sha256.create();
hash.update(value);
hash.hex();
value = hash.toString();
}
return value;
}
}
export default Utils;