From b01a1872c6c18e4aed7bd65dfd3739dd855e83d9 Mon Sep 17 00:00:00 2001 From: Jordan Stremming Date: Mon, 8 May 2023 13:39:24 -0500 Subject: [PATCH 1/4] code streamlined and commented --- content.js | 65 ++++++++++++++++++++++++------------------------------ 1 file changed, 29 insertions(+), 36 deletions(-) diff --git a/content.js b/content.js index e9d2c3b..5b7fa28 100644 --- a/content.js +++ b/content.js @@ -1,41 +1,34 @@ -// 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; -} +// 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}` : ""; + // + const isSecure = window.location.protocol === "https:" ? "secure" : ""; + document.cookie = `${name}=${encodeURIComponent(value)}; ${expires}; domain=${domain}; path=${path}; ${isSecure}`; +}; -// 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) { +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; + // open the link in the same tab + window.open(link.href, "_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); From f34ba75862530d93e02798b0d2a6acfc15a1acaf Mon Sep 17 00:00:00 2001 From: Jordan Stremming Date: Mon, 8 May 2023 13:39:53 -0500 Subject: [PATCH 2/4] use Chrome's defined variable instead of "magic string" for INSTALL --- background.js | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) 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/"}); } }); From 95f88ddb30e1ce6c25620bbfed270664ade9b341 Mon Sep 17 00:00:00 2001 From: Jordan Stremming Date: Mon, 8 May 2023 13:40:10 -0500 Subject: [PATCH 3/4] update to Manifest v3 --- manifest.json | 21 +++++++++++---------- 1 file changed, 11 insertions(+), 10 deletions(-) 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/*"] } From 9467fa6ffef7f0ce0d1a418c026431d594f8a4fb Mon Sep 17 00:00:00 2001 From: Jordan Stremming Date: Mon, 8 May 2023 13:53:16 -0500 Subject: [PATCH 4/4] add support to keep search queries --- content.js | 18 +++++++++++++++--- 1 file changed, 15 insertions(+), 3 deletions(-) diff --git a/content.js b/content.js index 5b7fa28..b5da9b5 100644 --- a/content.js +++ b/content.js @@ -2,7 +2,7 @@ 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}`; }; @@ -10,10 +10,13 @@ const setCookie = (name, value, days, domain = ".youtube.com", path = "/") => { const openYoutubeVideoInTab = (e) => { // ignore right-click or if Alt key is pressed if (e.button > 1 || e.altKey) return; + // 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 || @@ -21,8 +24,17 @@ const openYoutubeVideoInTab = (e) => { link.href.replace(/#.*/, "") === location.href.replace(/#.*/, "") ) return; - // open the link in the same tab - window.open(link.href, "_self"); + + // 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(); };