Last active
December 4, 2022 18:17
-
-
Save UCyborg/754fe319866a7f126ec1d074ed922b37 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// ==UserScript== | |
// @name YouTube layout config | |
// @namespace https://github.com/UCyborg | |
// @description Provides configuration interface for switching between desktop and mobile version of YouTube. | |
// @version 1.0 | |
// @author UCyborg | |
// @downloadURL https://gist.github.com/UCyborg/754fe319866a7f126ec1d074ed922b37/raw/YouTube_layout_config.user.js | |
// @include /https:\/\/(www|m)\.youtube\.com\/(?!embed\/.*$)/ | |
// @run-at document-start | |
// @require https://openuserjs.org/src/libs/sizzle/GM_config.js | |
// @grant GM_getValue | |
// @grant GM_setValue | |
// @grant GM_registerMenuCommand | |
// ==/UserScript== | |
'use strict'; | |
function updateYTPrefCookie(includeApp, mobileApp) { | |
function recordDefaultYTLayout() { | |
const url = new URL(window.location); | |
if (url.host.split('.')[0] == 'www') { | |
GM_setValue('YTDefaultLayout', 'Desktop'); | |
} else { | |
GM_setValue('YTDefaultLayout', 'Mobile'); | |
} | |
} | |
function getYTPrefCookie() { | |
const name = 'PREF='; | |
const ca = document.cookie.split(';'); | |
for (let i = 0; i < ca.length; i++) { | |
let c = ca[i]; | |
while (c.charAt(0) == ' ') { | |
c = c.substring(1); | |
} | |
if (c.indexOf(name) == 0) { | |
return c.substring(name.length, c.length); | |
} | |
} | |
return ''; | |
} | |
function setYTPrefCookie(cookie) { | |
if (cookie != '0') { | |
const date = new Date(); | |
date.setFullYear(cookie == '' ? 1970 : date.getFullYear() + 2); | |
document.cookie = 'PREF=' + (cookie == '' ? ';' : cookie) + '; expires=' + date.toGMTString() + '; domain=youtube.com; path=/'; | |
} | |
} | |
const newCookiePrefs = []; | |
if (includeApp) { | |
newCookiePrefs.push(('app=' + (mobileApp ? 'm' : 'desktop'))); | |
} | |
const curCookie = getYTPrefCookie(); | |
const curCookiePrefs = curCookie.split('&'); | |
let appEncountered = false; | |
for (let i = 0; i < curCookiePrefs.length; i++) { | |
if (curCookiePrefs[i].split('=')[0] == 'app') { | |
appEncountered = true; | |
continue; | |
} | |
newCookiePrefs.push(curCookiePrefs[i]); | |
} | |
if (!appEncountered) { | |
recordDefaultYTLayout(); | |
} | |
const finalCookie = newCookiePrefs.join('&'); | |
setYTPrefCookie(finalCookie == curCookie ? '0' : finalCookie); | |
} | |
(function() { | |
function openYTConfig() { | |
GM_config.open(); | |
YTLayoutConfig.style = 'position: fixed; width: 260px; height: 150px; border: 1px solid #000; z-index: 9999;'; | |
} | |
function reloadYTSite(wantedLayout) { | |
const url = new URL(window.location); | |
const currentLayout = url.host.split('.')[0] == 'www' ? 'Desktop' : 'Mobile'; | |
let shouldReloadYT = false; | |
if (wantedLayout != 'Default' || | |
(wantedLayout = GM_getValue('YTDefaultLayout')) !== undefined) { | |
if (currentLayout != wantedLayout) { | |
url.host = wantedLayout == 'Desktop' ? 'www.youtube.com' : 'm.youtube.com'; | |
shouldReloadYT = true; | |
} | |
} | |
if (url.searchParams.has('app')) { | |
url.searchParams.delete('app'); | |
shouldReloadYT = true; | |
} | |
if (url.searchParams.has('persist_app')) { | |
url.searchParams.delete('persist_app'); | |
shouldReloadYT = true; | |
} | |
if (shouldReloadYT) { | |
window.location.replace(url); | |
} | |
return shouldReloadYT; | |
} | |
GM_config.init( | |
{ | |
id: 'YTLayoutConfig', | |
title: 'YouTube layout', | |
fields: | |
{ | |
YTLayout: | |
{ | |
type: 'radio', | |
options: ['Default', 'Desktop', 'Mobile'] | |
}, | |
ReloadOnClose: | |
{ | |
type: 'checkbox', | |
label: 'Reload if layout has changed', | |
default: true | |
} | |
}, | |
events: | |
{ | |
save: function() | |
{ | |
GM_config.close(); | |
if (GM_config.get('ReloadOnClose')) { | |
reloadYTSite(GM_config.get('YTLayout')); | |
} | |
} | |
}, | |
css: '#YTLayoutConfig { background-color: #de7a7a; } #YTLayoutConfig_field_style { font-size: 13px; height: 6em; } #YTLayoutConfig .config_var { position: relative; top: 6px; text-align: center; } #YTLayoutConfig_buttons_holder { text-align: center; } #YTLayoutConfig_closeBtn { display: none; } #YTLayoutConfig .reset_holder { display: none }' | |
}); | |
const savedYTLayout = GM_config.get('YTLayout'); | |
switch (savedYTLayout) { | |
case 'Desktop': | |
updateYTPrefCookie(true, false); | |
break; | |
case 'Mobile': | |
updateYTPrefCookie(true, true); | |
break; | |
case 'Default': | |
updateYTPrefCookie(false); | |
} | |
if (!reloadYTSite(savedYTLayout)) { | |
GM_registerMenuCommand('YouTube layout', openYTConfig); | |
} | |
})(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment