From 45e0f87f9f7cac6d86480b9b776077ada98ee431 Mon Sep 17 00:00:00 2001 From: Ajay Ramachandran Date: Sun, 9 Feb 2020 11:14:40 -0500 Subject: [PATCH 1/8] Reformatted manifest. --- manifest/manifest.json | 6 ++---- 1 file changed, 2 insertions(+), 4 deletions(-) diff --git a/manifest/manifest.json b/manifest/manifest.json index ef72db83..0cd5ea80 100644 --- a/manifest/manifest.json +++ b/manifest/manifest.json @@ -4,8 +4,7 @@ "version": "1.2.8", "default_locale": "en", "description": "__MSG_Description__", - "content_scripts": [ - { + "content_scripts": [{ "run_at": "document_start", "matches": [ "https://*.youtube.com/*", @@ -21,8 +20,7 @@ "./libs/Source+Sans+Pro.css", "popup.css" ] - } - ], + }], "web_accessible_resources": [ "icons/LogoSponsorBlocker256px.png", "icons/IconSponsorBlocker256px.png", From 852912a42be5709e3205d18b40e9958a2cf5204c Mon Sep 17 00:00:00 2001 From: Ajay Ramachandran Date: Mon, 10 Feb 2020 17:12:52 -0500 Subject: [PATCH 2/8] Fixed inlines sponsor popup. Fixes https://github.com/ajayyy/SponsorBlock/issues/272 --- src/popup.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/popup.ts b/src/popup.ts index 478f1fd3..8bc83140 100644 --- a/src/popup.ts +++ b/src/popup.ts @@ -38,7 +38,7 @@ 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(messageListener); utils.localizeHtmlPage(); From d2779aba860ac46e2c3a910c7566994911401e55 Mon Sep 17 00:00:00 2001 From: Ajay Ramachandran Date: Mon, 10 Feb 2020 18:03:09 -0500 Subject: [PATCH 3/8] Fix editing not adding numbers. Resolves https://github.com/ajayyy/SponsorBlock/issues/273. --- src/popup.ts | 31 ++++++++++++++++--------------- 1 file changed, 16 insertions(+), 15 deletions(-) diff --git a/src/popup.ts b/src/popup.ts index 8bc83140..e47edf31 100644 --- a/src/popup.ts +++ b/src/popup.ts @@ -656,9 +656,9 @@ async function runThePopup(messageListener?: MessageListener) { tabs[0].id, {message: "getCurrentTime"}, function (response) { - let minutes = document.getElementById(idStartName + "Minutes" + index); + let minutes = document.getElementById(idStartName + "Minutes" + index); let seconds = document.getElementById(idStartName + "Seconds" + index); - + minutes.value = String(getTimeInMinutes(response.currentTime)); seconds.value = getTimeInFormattedSeconds(response.currentTime); }); @@ -667,11 +667,11 @@ async function runThePopup(messageListener?: MessageListener) { //id start name is whether it is the startTime or endTime //gives back the time in seconds - function getSponsorTimeEditTimes(idStartName, index) { + function getSponsorTimeEditTimes(idStartName, index): number { let minutes = document.getElementById(idStartName + "Minutes" + index); let seconds = document.getElementById(idStartName + "Seconds" + index); - return parseInt(minutes.value) * 60 + seconds.value; + return parseInt(minutes.value) * 60 + parseInt(seconds.value); } function saveSponsorTimeEdit(index, closeEditMode = true) { @@ -679,16 +679,17 @@ async function runThePopup(messageListener?: MessageListener) { sponsorTimes[index][1] = getSponsorTimeEditTimes("endTime", index); //save this - Config.config.sponsorTimes.set(currentVideoID, sponsorTimes); - messageHandler.query({ - active: true, - currentWindow: true - }, tabs => { - messageHandler.sendMessage( - tabs[0].id, - {message: "sponsorDataChanged"} - ); - }); + Config.config.sponsorTimes.set(currentVideoID, sponsorTimes); + + messageHandler.query({ + active: true, + currentWindow: true + }, tabs => { + messageHandler.sendMessage( + tabs[0].id, + {message: "sponsorDataChanged"} + ); + }); if (closeEditMode) { displaySponsorTimes(); @@ -968,7 +969,7 @@ async function runThePopup(messageListener?: MessageListener) { secondsDisplay = "0" + secondsDisplay; } - let formatted = minutes+ ":" + secondsDisplay; + let formatted = minutes + ":" + secondsDisplay; return formatted; } From f05d80523b5838e7fa0613832b11af7ab3a47c34 Mon Sep 17 00:00:00 2001 From: Ajay Ramachandran Date: Mon, 10 Feb 2020 21:39:39 -0500 Subject: [PATCH 4/8] Made it unpause when previewing a sponsor. Resolves https://github.com/ajayyy/SponsorBlock/issues/222 --- src/content.ts | 54 ++++++++++++++++++++++++++++---------------------- 1 file changed, 30 insertions(+), 24 deletions(-) diff --git a/src/content.ts b/src/content.ts index 05255f4b..51bd2b58 100644 --- a/src/content.ts +++ b/src/content.ts @@ -27,7 +27,7 @@ var hiddenSponsorTimes = []; var sponsorSkipped = []; //the video -var v; +var video: HTMLVideoElement; var onInvidious; @@ -85,7 +85,7 @@ var skipNoticeContentContainer = () => ({ unskipSponsorTime, sponsorTimes, UUIDs, - v, + v: video, reskipSponsorTime, hiddenSponsorTimes, updatePreviewBar @@ -132,16 +132,22 @@ function messageListener(request: any, sender: any, sendResponse: (response: any break; case "getVideoDuration": sendResponse({ - duration: v.duration + duration: video.duration }); break; case "skipToTime": - v.currentTime = request.time; + video.currentTime = request.time; + + // Unpause the video if needed + if (video.paused){ + video.play(); + } + return case "getCurrentTime": sendResponse({ - currentTime: v.currentTime + currentTime: video.currentTime }); break; @@ -346,9 +352,9 @@ async function videoIDChange(id) { function sponsorsLookup(id: string, channelIDPromise?) { - v = document.querySelector('video') // Youtube video player + video = document.querySelector('video') // Youtube video player //there is no video here - if (v == null) { + if (video == null) { setTimeout(() => sponsorsLookup(id, channelIDPromise), 100); return; } @@ -357,7 +363,7 @@ function sponsorsLookup(id: string, channelIDPromise?) { durationListenerSetUp = true; //wait until it is loaded - v.addEventListener('durationchange', updatePreviewBar); + video.addEventListener('durationchange', updatePreviewBar); } if (channelIDPromise !== undefined) { @@ -415,7 +421,7 @@ function sponsorsLookup(id: string, channelIDPromise?) { //update the preview bar //leave the type blank for now until categories are added - if (lastPreviewBarUpdate == id || (lastPreviewBarUpdate == null && !isNaN(v.duration))) { + if (lastPreviewBarUpdate == id || (lastPreviewBarUpdate == null && !isNaN(video.duration))) { //set it now //otherwise the listener can handle it updatePreviewBar(); @@ -453,7 +459,7 @@ function sponsorsLookup(id: string, channelIDPromise?) { //add the event to run on the videos "ontimeupdate" if (!Config.config.disableSkipping) { - v.ontimeupdate = function () { + video.ontimeupdate = function () { sponsorCheck(); }; } @@ -568,7 +574,7 @@ function updatePreviewBar() { types.push("previewSponsor"); } - utils.wait(() => previewBar !== null).then((result) => previewBar.set(allSponsorTimes, types, v.duration)); + utils.wait(() => previewBar !== null).then((result) => previewBar.set(allSponsorTimes, types, video.duration)); //update last video id lastPreviewBarUpdate = sponsorVideoID; @@ -588,7 +594,7 @@ function whitelistCheck() { function sponsorCheck() { if (Config.config.disableSkipping) { // Make sure this isn't called again - v.ontimeupdate = null; + video.ontimeupdate = null; return; } else if (channelWhitelisted) { return; @@ -619,20 +625,20 @@ function sponsorCheck() { //don't keep track until they are loaded in if (sponsorTimes !== null || sponsorTimesSubmitting.length > 0) { - lastTime = v.currentTime; + lastTime = video.currentTime; } } function checkSponsorTime(sponsorTimes, index, openNotice): boolean { //this means part of the video was just skipped - if (Math.abs(v.currentTime - lastTime) > 1 && lastTime != -1) { + if (Math.abs(video.currentTime - lastTime) > 1 && lastTime != -1) { //make lastTime as if the video was playing normally - lastTime = v.currentTime - 0.0001; + lastTime = video.currentTime - 0.0001; } - if (checkIfTimeToSkip(v.currentTime, sponsorTimes[index][0], sponsorTimes[index][1]) && !hiddenSponsorTimes.includes(index)) { + if (checkIfTimeToSkip(video.currentTime, sponsorTimes[index][0], sponsorTimes[index][1]) && !hiddenSponsorTimes.includes(index)) { //skip it - skipToTime(v, index, sponsorTimes, openNotice); + skipToTime(video, index, sponsorTimes, openNotice); //something was skipped return true; @@ -690,14 +696,14 @@ function skipToTime(v, index, sponsorTimes, openNotice) { function unskipSponsorTime(UUID) { if (sponsorTimes != null) { //add a tiny bit of time to make sure it is not skipped again - v.currentTime = sponsorTimes[UUIDs.indexOf(UUID)][0] + 0.001; + video.currentTime = sponsorTimes[UUIDs.indexOf(UUID)][0] + 0.001; } } function reskipSponsorTime(UUID) { if (sponsorTimes != null) { //add a tiny bit of time to make sure it is not skipped again - v.currentTime = sponsorTimes[UUIDs.indexOf(UUID)][1]; + video.currentTime = sponsorTimes[UUIDs.indexOf(UUID)][1]; } } @@ -786,7 +792,7 @@ function startSponsorClicked() { //send back current time with message chrome.runtime.sendMessage({ message: "addSponsorTime", - time: v.currentTime, + time: video.currentTime, videoID: sponsorVideoID }, function(response) { //see if the sponsorTimesSubmitting needs to be updated @@ -1012,11 +1018,11 @@ function dontShowNoticeAgain() { } function sponsorMessageStarted(callback) { - v = document.querySelector('video'); + video = document.querySelector('video'); //send back current time callback({ - time: v.currentTime + time: video.currentTime }) //update button @@ -1039,8 +1045,8 @@ function submitSponsorTimes() { if (sponsorTimes != undefined && sponsorTimes.length > 0) { //check if a sponsor exceeds the duration of the video for (let i = 0; i < sponsorTimes.length; i++) { - if (sponsorTimes[i][1] > v.duration) { - sponsorTimes[i][1] = v.duration; + if (sponsorTimes[i][1] > video.duration) { + sponsorTimes[i][1] = video.duration; } } //update sponsorTimes From ba9e42e6f0371368cd72d64d3c0c34429eaa8e93 Mon Sep 17 00:00:00 2001 From: Ajay Ramachandran Date: Mon, 10 Feb 2020 21:49:17 -0500 Subject: [PATCH 5/8] Forced auto skip if previewing sponsor. Resolves https://github.com/ajayyy/SponsorBlock/issues/222 --- src/content.ts | 13 ++++++++++++- 1 file changed, 12 insertions(+), 1 deletion(-) diff --git a/src/content.ts b/src/content.ts index 51bd2b58..709c05a1 100644 --- a/src/content.ts +++ b/src/content.ts @@ -49,6 +49,10 @@ var channelWhitelisted = false; // create preview bar var previewBar = null; +// When not null, a sponsor is currently being previewed and auto skip should be enabled. +// This is set to a timeout function when that happens that will reset it after 3 seconds. +var previewResetter: NodeJS.Timeout = null; + //the player controls on the YouTube player var controls = null; @@ -144,6 +148,13 @@ function messageListener(request: any, sender: any, sendResponse: (response: any video.play(); } + // Start preview resetter + if (previewResetter !== null){ + clearTimeout(previewResetter); + } + + previewResetter = setTimeout(() => previewResetter = null, 4000); + return case "getCurrentTime": sendResponse({ @@ -658,7 +669,7 @@ function checkIfTimeToSkip(currentVideoTime, startTime, endTime) { //skip fromt he start time to the end time for a certain index sponsor time function skipToTime(v, index, sponsorTimes, openNotice) { - if (!Config.config.disableAutoSkip) { + if (!Config.config.disableAutoSkip || previewResetter !== null) { v.currentTime = sponsorTimes[index][1]; } From 2450457fe5c85d7a4d01e5e1d0f928d0087adc26 Mon Sep 17 00:00:00 2001 From: Ajay Ramachandran Date: Mon, 10 Feb 2020 21:54:11 -0500 Subject: [PATCH 6/8] Fixed close button on dark theme. Resolves https://github.com/ajayyy/SponsorBlock/issues/196 --- src/content.ts | 2 ++ 1 file changed, 2 insertions(+) diff --git a/src/content.ts b/src/content.ts index 709c05a1..f07bef16 100644 --- a/src/content.ts +++ b/src/content.ts @@ -892,6 +892,8 @@ function openInfoMenu() { closeButton.classList.add("smallLink"); closeButton.setAttribute("align", "center"); closeButton.addEventListener("click", closeInfoMenu); + // Theme based color + closeButton.style.color = "var(--yt-spec-text-primary)"; //add the close button popup.prepend(closeButton); From 657aff21678db14d7c4db6a48ed70103d04d215e Mon Sep 17 00:00:00 2001 From: Ajay Ramachandran Date: Mon, 10 Feb 2020 22:27:54 -0500 Subject: [PATCH 7/8] Removed use of Invidious API. Resolves https://github.com/ajayyy/SponsorBlock/issues/189. --- README.md | 2 +- src/content.ts | 13 ++++++++++--- 2 files changed, 11 insertions(+), 4 deletions(-) diff --git a/README.md b/README.md index 89053539..3bb7dfd6 100644 --- a/README.md +++ b/README.md @@ -68,7 +68,7 @@ The result is in `dist`. # Credit -The awesome [Invidious API](https://github.com/omarroth/invidious/wiki/API) is used to grab the time the video was published. +The awesome [Invidious API](https://github.com/omarroth/invidious/wiki/API) used to be used. Original code from [YTSponsorSkip](https://github.com/OfficialNoob/YTSponsorSkip), but not much of the code is left. diff --git a/src/content.ts b/src/content.ts index f07bef16..6e51aab3 100644 --- a/src/content.ts +++ b/src/content.ts @@ -444,12 +444,19 @@ function sponsorsLookup(id: string, channelIDPromise?) { //check if this video was uploaded recently //use the invidious api to get the time published - sendRequestToCustomServer('GET', "https://invidio.us/api/v1/videos/" + id + '?fields=published', function(xmlhttp, error) { + sendRequestToCustomServer('GET', "https://www.youtube.com/get_video_info?video_id=" + id, function(xmlhttp, error) { if (xmlhttp.readyState == 4 && xmlhttp.status == 200) { - let unixTimePublished = JSON.parse(xmlhttp.responseText).published; + let decodedData = decodeURIComponent(xmlhttp.responseText).match(/(?<=player_response=)[^&]*/)[0]; + + if (decodedData === undefined) { + console.error("[SB] Failed at getting video upload date info from YouTube."); + return; + } + + let dateUploaded = JSON.parse(decodedData).microformat.playerMicroformatRenderer.uploadDate; //if less than 3 days old - if ((Date.now() / 1000) - unixTimePublished < 259200) { + if (Date.now() - new Date(dateUploaded).getTime() < 259200000) { //TODO lower when server becomes better setTimeout(() => sponsorsLookup(id, channelIDPromise), 180000); } From 2f2c1ad49b1ee20bf3e2ba999509594fca66d4e6 Mon Sep 17 00:00:00 2001 From: Ajay Ramachandran Date: Mon, 10 Feb 2020 22:32:15 -0500 Subject: [PATCH 8/8] Increased version number. --- manifest/manifest.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/manifest/manifest.json b/manifest/manifest.json index 0cd5ea80..d32d3ef3 100644 --- a/manifest/manifest.json +++ b/manifest/manifest.json @@ -1,7 +1,7 @@ { "name": "__MSG_fullName__", "short_name": "__MSG_Name__", - "version": "1.2.8", + "version": "1.2.9", "default_locale": "en", "description": "__MSG_Description__", "content_scripts": [{