diff options
author | rafern <[email protected]> | 2020-05-14 10:58:30 +0100 |
---|---|---|
committer | rafern <[email protected]> | 2020-05-14 10:58:30 +0100 |
commit | c046df7d189c58825748dffa693533a73bb9a1f0 (patch) | |
tree | 95ac3794a710bd41ee0f8290f7fa11b1f2372fdf /src/content.ts | |
parent | dd5ed6ce421fd98ba994814ceb786790c2dea706 (diff) | |
download | SponsorBlock-c046df7d189c58825748dffa693533a73bb9a1f0.tar.gz SponsorBlock-c046df7d189c58825748dffa693533a73bb9a1f0.zip |
Requested changes
Diffstat (limited to 'src/content.ts')
-rw-r--r-- | src/content.ts | 120 |
1 files changed, 57 insertions, 63 deletions
diff --git a/src/content.ts b/src/content.ts index 89fed5ec..8b19c015 100644 --- a/src/content.ts +++ b/src/content.ts @@ -24,7 +24,7 @@ var sponsorTimes: SponsorTime[] = null; //what video id are these sponsors for var sponsorVideoID: VideoID = null; -// JSON video info +// JSON video info var videoInfo: any = null; //the channel this video is about var channelID; @@ -41,9 +41,6 @@ var sponsorSkipped: boolean[] = []; //the video var video: HTMLVideoElement; -/** The last time this video was seeking to */ -var lastVideoTime: number = null; - var onInvidious; var onMobileYouTube; @@ -124,7 +121,7 @@ var skipNoticeContentContainer: ContentContainer = () => ({ //get messages from the background script and the popup chrome.runtime.onMessage.addListener(messageListener); - + function messageListener(request: any, sender: any, sendResponse: (response: any) => void): void { //messages from popup script switch(request.message){ @@ -176,7 +173,7 @@ function messageListener(request: any, sender: any, sendResponse: (response: any // Start preview resetter if (previewResetter !== null){ clearTimeout(previewResetter); - } + } previewResetter = setTimeout(() => previewResetter = null, 4000); @@ -217,8 +214,8 @@ function messageListener(request: any, sender: any, sendResponse: (response: any /** * Called when the config is updated - * - * @param {String} changes + * + * @param {String} changes */ function contentConfigUpdateListener(changes) { for (const key in changes) { @@ -284,6 +281,9 @@ function resetValues() { } else { switchingVideos = true; } + + // Reset advert playing flag + isAdPlaying = false; } async function videoIDChange(id) { @@ -327,10 +327,10 @@ async function videoIDChange(id) { // Mobile YouTube workaround const observer = new MutationObserver(handleMobileControlsMutations); - observer.observe(document.getElementById("player-control-container"), { - attributes: true, - childList: true, - subtree: true + observer.observe(document.getElementById("player-control-container"), { + attributes: true, + childList: true, + subtree: true }); } else { utils.wait(getControls).then(createPreviewBar); @@ -355,10 +355,10 @@ async function videoIDChange(id) { //set the previous id now, don't wait for chrome.storage.get previousVideoID = id; } - + //close popup closeInfoMenu(); - + sponsorsLookup(id); //make sure everything is properly added @@ -386,7 +386,7 @@ async function videoIDChange(id) { function handleMobileControlsMutations(): void { let mobileYouTubeSelector = ".progress-bar-background"; - + updateVisibilityOfPlayerControlsButton().then((createdButtons) => { if (createdButtons) { if (sponsorTimesSubmitting != null && sponsorTimesSubmitting.length > 0 && sponsorTimesSubmitting[sponsorTimesSubmitting.length - 1].segment.length >= 2) { @@ -398,7 +398,7 @@ function handleMobileControlsMutations(): void { } } }); - + if (previewBar !== null) { if (document.body.contains(previewBar.container)) { updatePreviewBarPositionMobile(document.getElementsByClassName(mobileYouTubeSelector)[0]); @@ -436,7 +436,7 @@ function createPreviewBar(): void { if (el && el.length && el[0]) { previewBar = new PreviewBar(el[0], onMobileYouTube); - + updatePreviewBar(); break; @@ -462,7 +462,7 @@ function cancelSponsorSchedule(): void { } /** - * + * * @param currentTime Optional if you don't want to use the actual current time */ function startSponsorSchedule(includeIntersectingSegments: boolean = false, currentTime?: number): void { @@ -473,8 +473,6 @@ function startSponsorSchedule(includeIntersectingSegments: boolean = false, curr // Reset lastCheckVideoTime lastCheckVideoTime = -1; lastCheckTime = 0; - - lastVideoTime = video.currentTime; return; } @@ -530,7 +528,7 @@ function startSponsorSchedule(includeIntersectingSegments: boolean = false, curr /** * This makes sure the videoID is still correct - * + * * TODO: Remove this bug catching if statement when the bug is found */ function incorrectVideoIDCheck(videoID?: string): boolean { @@ -570,6 +568,8 @@ function sponsorsLookup(id: string) { video.addEventListener('play', () => { switchingVideos = false; + updateAdFlag(); + // Make sure it doesn't get double called with the playing event if (lastCheckVideoTime !== video.currentTime && Date.now() - lastCheckTime > 2000) { lastCheckTime = Date.now(); @@ -577,8 +577,6 @@ function sponsorsLookup(id: string) { startSponsorSchedule(); } - - updateAdFlag(); }); video.addEventListener('playing', () => { // Make sure it doesn't get double called with the play event @@ -594,8 +592,6 @@ function sponsorsLookup(id: string) { lastCheckVideoTime = -1 lastCheckTime = 0; - lastVideoTime = video.currentTime; - if (!video.paused){ startSponsorSchedule(); } @@ -606,8 +602,6 @@ function sponsorsLookup(id: string) { lastCheckVideoTime = -1; lastCheckTime = 0; - lastVideoTime = video.currentTime; - cancelSponsorSchedule(); }); @@ -699,8 +693,8 @@ function sponsorsLookup(id: string) { } /** - * Only should be used when it is okay to skip a sponsor when in the middle of it - * + * Only should be used when it is okay to skip a sponsor when in the middle of it + * * Ex. When segments are first loaded */ function startSkipScheduleCheckingForStartSponsors() { @@ -753,9 +747,9 @@ function getYouTubeVideoID(url: string) { //Attempt to parse url let urlObject = null; - try { + try { urlObject = new URL(url); - } catch (e) { + } catch (e) { console.error("[SB] Unable to parse URL: " + url); return false; } @@ -785,7 +779,7 @@ function getYouTubeVideoID(url: string) { console.error("[SB] Video ID not valid for " + url); return false; } - } + } return false; } @@ -854,7 +848,7 @@ function whitelistCheck() { /** * Returns info about the next upcoming sponsor skip */ -function getNextSkipIndex(currentTime: number, includeIntersectingSegments: boolean): +function getNextSkipIndex(currentTime: number, includeIntersectingSegments: boolean): {array: SponsorTime[], index: number, endIndex: number, openNotice: boolean} { let sponsorStartTimes = getStartTimes(sponsorTimes, includeIntersectingSegments); @@ -869,7 +863,7 @@ function getNextSkipIndex(currentTime: number, includeIntersectingSegments: bool let minPreviewSponsorTimeIndex = previewSponsorStartTimes.indexOf(Math.min(...previewSponsorStartTimesAfterCurrentTime)); let previewEndTimeIndex = getLatestEndTimeIndex(sponsorTimesSubmitting, minPreviewSponsorTimeIndex); - if ((minPreviewSponsorTimeIndex === -1 && minSponsorTimeIndex !== -1) || + if ((minPreviewSponsorTimeIndex === -1 && minSponsorTimeIndex !== -1) || sponsorStartTimes[minSponsorTimeIndex] < previewSponsorStartTimes[minPreviewSponsorTimeIndex]) { return { array: sponsorTimes, @@ -889,16 +883,16 @@ function getNextSkipIndex(currentTime: number, includeIntersectingSegments: bool /** * This returns index if the skip option is not AutoSkip - * + * * Finds the last endTime that occurs in a segment that the given * segment skips into that is part of an AutoSkip category. - * - * Used to find where a segment should truely skip to if there are intersecting submissions due to + * + * Used to find where a segment should truely skip to if there are intersecting submissions due to * them having different categories. - * - * @param sponsorTimes + * + * @param sponsorTimes * @param index Index of the given sponsor - * @param hideHiddenSponsors + * @param hideHiddenSponsors */ function getLatestEndTimeIndex(sponsorTimes: SponsorTime[], index: number, hideHiddenSponsors: boolean = true): number { // Only combine segments for AutoSkip @@ -912,7 +906,7 @@ function getLatestEndTimeIndex(sponsorTimes: SponsorTime[], index: number, hideH let currentSegment = sponsorTimes[i].segment; let latestEndTime = sponsorTimes[latestEndTimeIndex].segment[1]; - if (currentSegment[0] <= latestEndTime && currentSegment[1] > latestEndTime + if (currentSegment[0] <= latestEndTime && currentSegment[1] > latestEndTime && (!hideHiddenSponsors || sponsorTimes[i].hidden === SponsorHideType.Visible) && utils.getCategorySelection(sponsorTimes[i].category).option === CategorySkipOption.AutoSkip) { // Overlapping segment @@ -931,26 +925,26 @@ function getLatestEndTimeIndex(sponsorTimes: SponsorTime[], index: number, hideH /** * Gets just the start times from a sponsor times array. * Optionally specify a minimum - * - * @param sponsorTimes + * + * @param sponsorTimes * @param minimum * @param hideHiddenSponsors - * @param includeIntersectingSegments If true, it will include segments that start before + * @param includeIntersectingSegments If true, it will include segments that start before * the current time, but end after */ -function getStartTimes(sponsorTimes: SponsorTime[], includeIntersectingSegments: boolean, minimum?: number, +function getStartTimes(sponsorTimes: SponsorTime[], includeIntersectingSegments: boolean, minimum?: number, onlySkippableSponsors: boolean = false, hideHiddenSponsors: boolean = false): number[] { if (sponsorTimes === null) return []; let startTimes: number[] = []; for (let i = 0; i < sponsorTimes.length; i++) { - if ((minimum === undefined || (sponsorTimes[i].segment[0] >= minimum || (includeIntersectingSegments && sponsorTimes[i].segment[1] > minimum))) + if ((minimum === undefined || (sponsorTimes[i].segment[0] >= minimum || (includeIntersectingSegments && sponsorTimes[i].segment[1] > minimum))) && (!onlySkippableSponsors || utils.getCategorySelection(sponsorTimes[i].category).option !== CategorySkipOption.ShowOverlay) && (!hideHiddenSponsors || sponsorTimes[i].hidden === SponsorHideType.Visible)) { startTimes.push(sponsorTimes[i].segment[0]); - } + } } return startTimes; @@ -958,8 +952,8 @@ function getStartTimes(sponsorTimes: SponsorTime[], includeIntersectingSegments: /** * Skip to exact time in a video and autoskips - * - * @param time + * + * @param time */ function previewTime(time: number) { video.currentTime = time; @@ -988,7 +982,7 @@ function skipToTime(v: HTMLVideoElement, index: number, sponsorTimes: SponsorTim lastSponsorTimeSkipped = sponsorTimes[index].segment[0]; let currentUUID: string = sponsorTimes[index].UUID; - lastSponsorTimeSkippedUUID = currentUUID; + lastSponsorTimeSkippedUUID = currentUUID; if (openNotice) { //send out the message saying that a sponsor message was skipped @@ -1033,9 +1027,9 @@ function reskipSponsorTime(UUID) { } /** - * Checks if currently inside a segment and will trigger + * Checks if currently inside a segment and will trigger * a skip schedule if true. - * + * * This is used for when a manual skip is finished or a reskip is complete */ function checkIfInsideSegment() { @@ -1111,7 +1105,7 @@ async function createButtons(): Promise<boolean> { let createdButton = false; // Add button if does not already exist in html - createdButton = createButton("startSponsor", "sponsorStart", startSponsorClicked, "PlayerStartIconSponsorBlocker256px.png") || createdButton; + createdButton = createButton("startSponsor", "sponsorStart", startSponsorClicked, "PlayerStartIconSponsorBlocker256px.png") || createdButton; createdButton = createButton("info", "openPopup", openInfoMenu, "PlayerInfoIconSponsorBlocker256px.png") || createdButton; createdButton = createButton("delete", "clearTimes", clearSponsorTimes, "PlayerDeleteIconSponsorBlocker256px.png") || createdButton; createdButton = createButton("submit", "SubmitTimes", submitSponsorTimes, "PlayerUploadIconSponsorBlocker256px.png") || createdButton; @@ -1139,7 +1133,7 @@ async function updateVisibilityOfPlayerControlsButton(): Promise<boolean> { } else { document.getElementById("infoButton").style.removeProperty("display"); } - + if (Config.config.hideDeleteButtonPlayerControls || onInvidious) { document.getElementById("deleteButton").style.display = "none"; } @@ -1226,7 +1220,7 @@ function updateSponsorTimesSubmitting(getFromConfig: boolean = true) { async function changeStartSponsorButton(showStartSponsor, uploadButtonVisible) { if(!sponsorVideoID) return false; - + //if it isn't visible, there is no data let shouldHide = (uploadButtonVisible && !(Config.config.hideDeleteButtonPlayerControls || onInvidious)) ? "unset" : "none" document.getElementById("deleteButton").style.display = shouldHide; @@ -1284,7 +1278,7 @@ function openInfoMenu() { //add the close button popup.prepend(closeButton); - + let parentNodes = document.querySelectorAll("#secondary"); let parentNode = null; for (let i = 0; i < parentNodes.length; i++) { @@ -1296,7 +1290,7 @@ function openInfoMenu() { //old youtube theme parentNode = document.getElementById("watch7-sidebar-contents"); } - + //make the logo source not 404 //query selector must be used since getElementByID doesn't work on a node and this isn't added to the document yet @@ -1377,10 +1371,10 @@ function vote(type: number, UUID: string, category?: string, skipNotice?: SkipNo // Count this as a skip Config.config.minutesSaved = Config.config.minutesSaved + factor * (sponsorTimes[sponsorIndex].segment[1] - sponsorTimes[sponsorIndex].segment[0]) / 60; - + Config.config.skipCount = Config.config.skipCount + factor; } - + chrome.runtime.sendMessage({ message: "submitVote", type: type, @@ -1475,9 +1469,9 @@ async function sendSubmitMessage(){ if (Config.config.minDuration > 0) { for (let i = 0; i < sponsorTimesSubmitting.length; i++) { if (sponsorTimesSubmitting[i].segment[1] - sponsorTimesSubmitting[i].segment[0] < Config.config.minDuration) { - let confirmShort = chrome.i18n.getMessage("shortCheck") + "\n\n" + + let confirmShort = chrome.i18n.getMessage("shortCheck") + "\n\n" + getSegmentsMessage(utils.getSegmentsFromSponsorTimes(sponsorTimesSubmitting)); - + if(!confirm(confirmShort)) return; } } @@ -1510,7 +1504,7 @@ async function sendSubmitMessage(){ //add submissions to current sponsors list if (sponsorTimes === null) sponsorTimes = []; - + sponsorTimes = sponsorTimes.concat(sponsorTimesSubmitting); // Increase contribution count @@ -1589,7 +1583,7 @@ function sendRequestToCustomServer(type, fullAddress, callback) { xmlhttp.onreadystatechange = function () { callback(xmlhttp, false); }; - + xmlhttp.onerror = function(ev) { callback(xmlhttp, true); }; |