Merge pull request #283 from ajayyy/experimental

New skipping mechanism fixes
This commit is contained in:
Ajay Ramachandran 2020-02-20 11:25:32 -05:00 committed by GitHub
commit cd11618a5d
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
3 changed files with 30 additions and 24 deletions

View file

@ -1,7 +1,7 @@
{ {
"name": "__MSG_fullName__", "name": "__MSG_fullName__",
"short_name": "__MSG_Name__", "short_name": "__MSG_Name__",
"version": "1.2.14", "version": "1.2.15",
"default_locale": "en", "default_locale": "en",
"description": "__MSG_Description__", "description": "__MSG_Description__",
"content_scripts": [{ "content_scripts": [{

View file

@ -24,16 +24,19 @@
}, },
"scripts": { "scripts": {
"web-run": "npm run web-run:chrome", "web-run": "npm run web-run:chrome",
"web-run:firefox": "cd dist && web-ext run --start-url https://chrome.google.com/webstore/detail/ublock-origin/cjpalhdlnbpafiamejdnhcphjbkeiagm", "web-run:firefox": "cd dist && web-ext run --start-url https://addons.mozilla.org/firefox/addon/ublock-origin/",
"web-run:chrome": "cd dist && web-ext run --start-url https://chrome.google.com/webstore/detail/ublock-origin/cjpalhdlnbpafiamejdnhcphjbkeiagm -t chromium", "web-run:chrome": "cd dist && web-ext run --start-url https://chrome.google.com/webstore/detail/ublock-origin/cjpalhdlnbpafiamejdnhcphjbkeiagm -t chromium",
"build": "npm run build:chrome", "build": "npm run build:chrome",
"build:chrome": "webpack --env.browser=chrome --config webpack/webpack.prod.js", "build:chrome": "webpack --env.browser=chrome --config webpack/webpack.prod.js",
"build:firefox": "webpack --env.browser=firefox --config webpack/webpack.prod.js", "build:firefox": "webpack --env.browser=firefox --config webpack/webpack.prod.js",
"build:dev": "npm run build:dev:chrome",
"build:dev:chrome": "webpack --env.browser=chrome --config webpack/webpack.dev.js",
"build:dev:firefox": "webpack --env.browser=firefox --config webpack/webpack.dev.js",
"build:watch": "npm run build:watch:chrome", "build:watch": "npm run build:watch:chrome",
"build:watch:chrome": "webpack --env.browser=chrome --config webpack/webpack.dev.js --watch", "build:watch:chrome": "webpack --env.browser=chrome --config webpack/webpack.dev.js --watch",
"build:watch:firefox": "webpack --env.browser=firefox --config webpack/webpack.dev.js --watch", "build:watch:firefox": "webpack --env.browser=firefox --config webpack/webpack.dev.js --watch",
"dev": "npm run build && concurrently \"npm run web-run\" \"npm run build:watch\"", "dev": "npm run build:dev && concurrently \"npm run web-run\" \"npm run build:watch\"",
"dev:firefox": "npm run build:firefox && concurrently \"npm run web-run:firefox\" \"npm run build:watch:firefox\"", "dev:firefox": "npm run build:dev:firefox && concurrently \"npm run web-run:firefox\" \"npm run build:watch:firefox\"",
"clean": "rimraf dist", "clean": "rimraf dist",
"test": "npx jest" "test": "npx jest"
}, },

View file

@ -43,8 +43,6 @@ var lastPreviewBarUpdate;
//whether the duration listener listening for the duration changes of the video has been setup yet //whether the duration listener listening for the duration changes of the video has been setup yet
var durationListenerSetUp = false; var durationListenerSetUp = false;
// Timestamp of the last duration change
var lastDurationChange = 0;
//the channel this video is about //the channel this video is about
var channelURL; var channelURL;
@ -66,10 +64,7 @@ var previewResetter: NodeJS.Timeout = null;
var controls = null; var controls = null;
// Direct Links after the config is loaded // Direct Links after the config is loaded
utils.wait(() => Config.config !== null).then(() => videoIDChange(getYouTubeVideoID(document.URL))); utils.wait(() => Config.config !== null, 1000, 1).then(() => videoIDChange(getYouTubeVideoID(document.URL)));
//the last time looked at (used to see if this time is in the interval)
var lastTime = -1;
//the amount of times the sponsor lookup has retried //the amount of times the sponsor lookup has retried
//this only happens if there is an error //this only happens if there is an error
@ -240,9 +235,6 @@ document.onkeydown = function(e: KeyboardEvent){
} }
function resetValues() { function resetValues() {
//reset last sponsor times
lastTime = -1;
//reset sponsor times //reset sponsor times
sponsorTimes = null; sponsorTimes = null;
UUIDs = []; UUIDs = [];
@ -430,14 +422,14 @@ function createPreviewBar(): void {
* This happens when the resolution changes or at random time to clear memory. * This happens when the resolution changes or at random time to clear memory.
*/ */
function durationChangeListener() { function durationChangeListener() {
lastDurationChange = Date.now();
updatePreviewBar(); updatePreviewBar();
} }
function cancelSponsorSchedule(): void { function cancelSponsorSchedule(): void {
if (currentSkipSchedule !== null) { if (currentSkipSchedule !== null) {
clearTimeout(currentSkipSchedule); clearTimeout(currentSkipSchedule);
currentSkipSchedule = null;
} }
} }
@ -448,7 +440,7 @@ function cancelSponsorSchedule(): void {
function startSponsorSchedule(currentTime?: number): void { function startSponsorSchedule(currentTime?: number): void {
cancelSponsorSchedule(); cancelSponsorSchedule();
if (sponsorTimes === null || Config.config.disableSkipping || channelWhitelisted){ if (Config.config.disableSkipping || channelWhitelisted){
return; return;
} }
@ -456,18 +448,24 @@ function startSponsorSchedule(currentTime?: number): void {
let skipInfo = getNextSkipIndex(currentTime); let skipInfo = getNextSkipIndex(currentTime);
if (skipInfo.index === -1) return;
let skipTime = skipInfo.array[skipInfo.index]; let skipTime = skipInfo.array[skipInfo.index];
let timeUntilSponsor = skipTime[0] - currentTime; let timeUntilSponsor = skipTime[0] - currentTime;
currentSkipSchedule = setTimeout(() => { let skippingFunction = () => {
if (video.currentTime >= skipTime[0] && video.currentTime < skipTime[1]) { if (video.currentTime >= skipTime[0] && video.currentTime < skipTime[1]) {
skipToTime(video, skipInfo.index, skipInfo.array, skipInfo.openNotice); skipToTime(video, skipInfo.index, skipInfo.array, skipInfo.openNotice);
startSponsorSchedule();
} else {
startSponsorSchedule();
} }
}, timeUntilSponsor * 1000 * (1 / video.playbackRate));
startSponsorSchedule();
};
if (timeUntilSponsor <= 0) {
skippingFunction();
} else {
currentSkipSchedule = setTimeout(skippingFunction, timeUntilSponsor * 1000 * (1 / video.playbackRate));
}
} }
function sponsorsLookup(id: string, channelIDPromise?) { function sponsorsLookup(id: string, channelIDPromise?) {
@ -493,6 +491,8 @@ function sponsorsLookup(id: string, channelIDPromise?) {
video.addEventListener('ratechange', () => startSponsorSchedule()); video.addEventListener('ratechange', () => startSponsorSchedule());
video.addEventListener('seeking', cancelSponsorSchedule); video.addEventListener('seeking', cancelSponsorSchedule);
video.addEventListener('pause', cancelSponsorSchedule); video.addEventListener('pause', cancelSponsorSchedule);
startSponsorSchedule();
} }
if (channelIDPromise !== undefined) { if (channelIDPromise !== undefined) {
@ -767,7 +767,8 @@ function getNextSkipIndex(currentTime: number): {array: number[][], index: numbe
let minPreviewSponsorTimeIndex = previewSponsorStartTimes.indexOf(Math.min(...previewSponsorStartTimesAfterCurrentTime)); let minPreviewSponsorTimeIndex = previewSponsorStartTimes.indexOf(Math.min(...previewSponsorStartTimesAfterCurrentTime));
if (minPreviewSponsorTimeIndex == -1 || sponsorStartTimes[minSponsorTimeIndex] < previewSponsorStartTimes[minPreviewSponsorTimeIndex]) { if ((minPreviewSponsorTimeIndex === -1 && minSponsorTimeIndex !== -1) ||
sponsorStartTimes[minSponsorTimeIndex] < previewSponsorStartTimes[minPreviewSponsorTimeIndex]) {
return { return {
array: sponsorTimes, array: sponsorTimes,
index: minSponsorTimeIndex, index: minSponsorTimeIndex,
@ -791,6 +792,8 @@ function getNextSkipIndex(currentTime: number): {array: number[][], index: numbe
* @param hideHiddenSponsors * @param hideHiddenSponsors
*/ */
function getStartTimes(sponsorTimes: number[][], minimum?: number, hideHiddenSponsors: boolean = false): number[] { function getStartTimes(sponsorTimes: number[][], minimum?: number, hideHiddenSponsors: boolean = false): number[] {
if (sponsorTimes === null) return [];
let startTimes: number[] = []; let startTimes: number[] = [];
for (let i = 0; i < sponsorTimes.length; i++) { for (let i = 0; i < sponsorTimes.length; i++) {
@ -802,7 +805,7 @@ function getStartTimes(sponsorTimes: number[][], minimum?: number, hideHiddenSpo
return startTimes; return startTimes;
} }
//skip from fhe start time to the end time for a certain index sponsor time //skip from the start time to the end time for a certain index sponsor time
function skipToTime(v, index, sponsorTimes, openNotice) { function skipToTime(v, index, sponsorTimes, openNotice) {
if (!Config.config.disableAutoSkip || previewResetter !== null) { if (!Config.config.disableAutoSkip || previewResetter !== null) {
v.currentTime = sponsorTimes[index][1]; v.currentTime = sponsorTimes[index][1];