Skip to content

Instantly share code, notes, and snippets.

@akshar-dave
Last active July 3, 2022 13:50
Show Gist options
  • Save akshar-dave/a150946125d407e94349fde02f44abf3 to your computer and use it in GitHub Desktop.
Save akshar-dave/a150946125d407e94349fde02f44abf3 to your computer and use it in GitHub Desktop.
Set alternate titles for YouTube.
if(window.location.href === "https://www.youtube.com"){
window.location = "https://www.youtube.com/feed/subscriptions"
}
const getCurrentVideoID = () => {
let currentUrl = window.location.href;
let currentVideoID = currentUrl.split("https://www.youtube.com/watch?v=", -1)[1];
return currentVideoID
}
let timeoutDuration = 2000;
const ANON_KEY = "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJpc3MiOiJzdXBhYmFzZSIsInJlZiI6Imh0b2RzZG1wd3p5am50ZmJoanJkIiwicm9sZSI6ImFub24iLCJpYXQiOjE2NTYxMzI2ODgsImV4cCI6MTk3MTcwODY4OH0.g-_WwHDRl-bry70vqajpQI4GujtuDxt-i-e5GgYWHw4";
const headers = {
apikey: ANON_KEY,
authorization: `Bearer: ${ANON_KEY}`,
"content-type": "application/json",
"Prefer": "return=representation",
}
window.onload = () => {
setTimeout(init, timeoutDuration);
}
const init = async () => {
let titleELem = document.querySelector('#above-the-fold #title h1');
let newTitle;
titleELem.addEventListener("click", () => {
newTitle = prompt(`Set a new title for this video: ${titleELem.innerText}`);
if(!newTitle.trim()) return;
sendTitle(getCurrentVideoID(), newTitle);
titleELem.innerText = newTitle;
});
let currentVideoTitles = await getTitles();
let randomTitle = currentVideoTitles[Math.floor(Math.random() * currentVideoTitles.length)].title;
titleELem.innerText = randomTitle;
}
const sendTitle = (video_id, title) => {
const options = {
method: 'POST',
headers: headers,
body: JSON.stringify({video_id: video_id, title: title})
};
fetch(`https://htodsdmpwzyjntfbhjrd.supabase.co/rest/v1/titles`, options);
}
const getTitles = async () => {
let videoTitles = fetch(`https://htodsdmpwzyjntfbhjrd.supabase.co/rest/v1/titles?select=*&video_id=eq.${getCurrentVideoID()}`, {
headers: headers,
})
.then((res) => res.json());
return videoTitles;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment