diff --git a/background.js b/background.js index aa85cdc..6453437 100644 --- a/background.js +++ b/background.js @@ -1,6 +1,6 @@ -//opens youtube on install -chrome.runtime.onInstalled.addListener(function(details) { - if(details.reason == "install") { +// open YouTube on install +chrome.runtime.onInstalled.addListener(details => { + if(details.reason === chrome.runtime.OnInstalledReason.INSTALL) { chrome.tabs.create({url: "https://www.youtube.com/"}); } }); diff --git a/content.js b/content.js index e9d2c3b..b5da9b5 100644 --- a/content.js +++ b/content.js @@ -1,41 +1,46 @@ -// creates set cookie function for easy replacement -function setCookie(name, value, days, domain, path) { - let expires = ""; - let secureFlag = ""; - if (days) { - let date = new Date(); - date.setTime(date.getTime() + (days * 24 * 60 * 60 * 1000)); - expires = "; expires=" + date.toUTCString(); - } - let cookieString = name + "=" + value + expires; - if (domain) { - cookieString += "; domain=" + domain; - } - if (path) { - cookieString += "; path=" + path; - } else { - cookieString += "; path=/"; - } - if (window.location.protocol === "https:") { - secureFlag = "; secure"; - } - document.cookie = cookieString + secureFlag; -} - -// Set the PREF cookie reponsible for your theme -setCookie("PREF", "f6=8", 365, ".youtube.com", "/"); - -//the function is self explanatory but the new update doesnt force open it in a new tab anymore -function openYoutubeVideoInTab(e) { +// helper function to set the document's cookie +const setCookie = (name, value, days, domain = ".youtube.com", path = "/") => { + // if days exists, convert days to seconds for expiration + const expires = days ? `max-age=${days * 86400}` : ""; + // include "secure" if https is used + const isSecure = window.location.protocol === "https:" ? "secure" : ""; + document.cookie = `${name}=${encodeURIComponent(value)}; ${expires}; domain=${domain}; path=${path}; ${isSecure}`; +}; + +const openYoutubeVideoInTab = (e) => { + // ignore right-click or if Alt key is pressed if (e.button > 1 || e.altKey) return; - if (e.target.classList.contains('ytp-scrubber-pull-indicator')) return; - - var link = e.target.closest('[href^="/watch"]'); - if (!link || (link.getAttribute('href') || '').match(/^(javascript|#|$)/) || link.href.replace(/#.*/, '') == location.href.replace(/#.*/, '')) return; - - window.open(link.href, '_self'); - //cancels the event that youtube uses for dynamic websites + + // ignore if the target is the player's scrubber + if (e.target.classList.contains("ytp-scrubber-pull-indicator")) return; + + // find the closest link element that starts with "/watch" + const link = e.target.closest('[href^="/watch"]'); + + // ignore if the link is not valid or a JavaScript/anchor link + if ( + !link || + (link.getAttribute("href") || "").match(/^(javascript|#|$)/) || + link.href.replace(/#.*/, "") === location.href.replace(/#.*/, "") + ) + return; + + // extract search parameters from the current URL + const searchParams = new URLSearchParams(window.location.search); + + // append search parameters to the link href + const href = new URL(link.href); + if (searchParams.size > 1) href.searchParams.append(...searchParams); + + // open the link with preserved search parameters in the same tab + window.open(href.toString(), "_self"); + + // cancel events YouTube uses for dynamic websites e.preventDefault(); -} +}; + +// set the PREF value to disable new UI +setCookie("PREF", "f6=8", 365); +// add listener to open the video with new UI window.addEventListener('mouseup', openYoutubeVideoInTab, true); diff --git a/manifest.json b/manifest.json index 5e49862..aff4f4a 100644 --- a/manifest.json +++ b/manifest.json @@ -1,19 +1,20 @@ { - "manifest_version": 2, - "name": "YouTube Cookie Setter", - "version": "1.0", - "description": "Sets a YouTube cookie.", + "manifest_version": 3, + "name": "Revert YouTube UI", + "version": "1.1", + "description": "Reverts new desktop YouTube UI implemented in April 2023.", "content_scripts": [ { - "matches": ["https://www.youtube.com/*"], - "js": ["content.js"] + "js": ["content.js"], + "matches": ["https://*.youtube.com/*"] } ], - "browser_action": { + "action": { "default_popup": "popup.html" }, "background": { - "scripts": ["background.js"], - "persistent": false - } + "service_worker": "background.js", + "permissions": ["scripting", "activeTab"] + }, + "host_permissions": ["https://*.youtube.com/*"] }