Skip to content

Commit

Permalink
Merge pull request #138 from ajayyy/experimental-ajay
Browse files Browse the repository at this point in the history
Improved away function
  • Loading branch information
ajayyy authored Aug 24, 2019
2 parents 896357a + fef31c3 commit 519a822
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 31 deletions.
14 changes: 8 additions & 6 deletions content.js
Original file line number Diff line number Diff line change
Expand Up @@ -666,12 +666,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;
Expand All @@ -692,7 +695,6 @@ function changeStartSponsorButton(showStartSponsor, uploadButtonVisible) {
//disable submit button
document.getElementById("submitButton").style.display = "none";
}
});
}

function toggleStartSponsorButton() {
Expand Down
2 changes: 1 addition & 1 deletion manifest.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "__MSG_fullName__",
"short_name": "__MSG_Name__",
"version": "1.1.5",
"version": "1.1.6",
"default_locale": "en",
"description": "__MSG_Description__",
"content_scripts": [
Expand Down
55 changes: 31 additions & 24 deletions utils.js
Original file line number Diff line number Diff line change
@@ -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;
}

0 comments on commit 519a822

Please sign in to comment.