From c1efe02614257bbac64a174ffac655f7467599cf Mon Sep 17 00:00:00 2001 From: Ajay Ramachandran Date: Fri, 23 Aug 2019 21:06:00 -0400 Subject: [PATCH 1/2] Improved promise calling in button updating function. --- content.js | 14 ++++++++------ 1 file changed, 8 insertions(+), 6 deletions(-) diff --git a/content.js b/content.js index 0951ab2977..1d1c6c3b05 100644 --- a/content.js +++ b/content.js @@ -655,12 +655,15 @@ function isSubmitButtonLoaded() { return document.getElementById("submitButton") !== null; } -function changeStartSponsorButton(showStartSponsor, uploadButtonVisible) { - if(!sponsorVideoID) return false; - wait(isSubmitButtonLoaded).then(result => { +async function changeStartSponsorButton(showStartSponsor, uploadButtonVisible) { + if(!sponsorVideoID) return false; + + //make sure submit button is loaded + await wait(isSubmitButtonLoaded); + //if it isn't visible, there is no data - let shouldHide = (uploadButtonVisible && !hideDeleteButtonPlayerControls) ? "unset":"none" - document.getElementById("deleteButton").style.display = shouldHide; + let shouldHide = (uploadButtonVisible && !hideDeleteButtonPlayerControls) ? "unset" : "none" + document.getElementById("deleteButton").style.display = shouldHide; if (showStartSponsor) { showingStartSponsor = true; @@ -681,7 +684,6 @@ function changeStartSponsorButton(showStartSponsor, uploadButtonVisible) { //disable submit button document.getElementById("submitButton").style.display = "none"; } - }); } function toggleStartSponsorButton() { From 2d12f957bca25248a3461ed1d2862b2eda2ecee7 Mon Sep 17 00:00:00 2001 From: Ajay Ramachandran Date: Fri, 23 Aug 2019 21:10:28 -0400 Subject: [PATCH 2/2] Improved await function. --- utils.js | 55 +++++++++++++++++++++++++++++++------------------------ 1 file changed, 31 insertions(+), 24 deletions(-) diff --git a/utils.js b/utils.js index 5fa60564b8..a3d1ac68d1 100644 --- a/utils.js +++ b/utils.js @@ -1,41 +1,48 @@ // Function that can be used to wait for a condition before returning async function wait(condition, timeout = 5000, check = 100) { - return await new Promise((resolve, reject) => { - setTimeout(() => {reject("TIMEOUT")}, timeout); - const interval = setInterval(() => { - let result = condition(); - if (result !== false) { - resolve(result); - clearInterval(interval); - }; - }, check); - }); + return await new Promise((resolve, reject) => { + setTimeout(() => reject("TIMEOUT"), timeout); + + let intervalCheck = () => { + let result = condition(); + if (result !== false) { + resolve(result); + clearInterval(interval); + }; + }; + + let interval = setInterval(intervalCheck, check); + + //run the check once first, this speeds it up a lot + intervalCheck(); + }); } function getYouTubeVideoID(url) { //Attempt to parse url let urlObject = null; try { - urlObject = new URL(url); + urlObject = new URL(url); } catch (e) { - console.error("[SB] Unable to parse URL: " + url); - return false; + console.error("[SB] Unable to parse URL: " + url); + return false; } - + //Check if valid hostname if(!["www.youtube.com","www.youtube-nocookie.com"].includes(urlObject.host)) return false; - + //Get ID from searchParam if ((urlObject.pathname == "/watch" || urlObject.pathname == "/watch/") && urlObject.searchParams.has("v")) { - id = urlObject.searchParams.get("v"); - return id.length == 11 ? id : false; + id = urlObject.searchParams.get("v"); + return id.length == 11 ? id : false; } else if (urlObject.pathname.startsWith("/embed/")) { - try { - return urlObject.pathname.substr(7, 11); - } catch (e) { - console.error("[SB] Video ID not valid for " + url); - return false; - } + try { + return urlObject.pathname.substr(7, 11); + } catch (e) { + console.error("[SB] Video ID not valid for " + url); + return false; + } } - return false; + + return false; } \ No newline at end of file