diff options
author | Wojtek Kosior <koszko@koszko.org> | 2021-12-29 16:53:39 +0100 |
---|---|---|
committer | Wojtek Kosior <koszko@koszko.org> | 2021-12-29 17:34:37 +0100 |
commit | 7ccadc53efcc8919346a5be4a495fc95ad9db7b3 (patch) | |
tree | 57df7e1668170206028aabe5cfbeaadf6f02df51 /content/syoutube/youtube.js | |
parent | 29e283855ce3701191b9206aab929c6e3a584163 (diff) | |
download | hydrilla-fixes-bundle-7ccadc53efcc8919346a5be4a495fc95ad9db7b3.tar.gz hydrilla-fixes-bundle-7ccadc53efcc8919346a5be4a495fc95ad9db7b3.zip |
add script that redirect youtube.com to invidious.snopyta
Diffstat (limited to 'content/syoutube/youtube.js')
-rw-r--r-- | content/syoutube/youtube.js | 61 |
1 files changed, 61 insertions, 0 deletions
diff --git a/content/syoutube/youtube.js b/content/syoutube/youtube.js new file mode 100644 index 0000000..2486bd4 --- /dev/null +++ b/content/syoutube/youtube.js @@ -0,0 +1,61 @@ +/** + * Copyright (C) 2021, Wojtek Kosior + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the CC0 1.0 Universal License as published by + * the Creative Commons Corporation. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * CC0 1.0 Universal License for more details. + * Available under the terms of Creative Commons Zero. + */ + +/* Use with https://www.youtube.com/*** */ + +/* Redirect youtube.com to yewtu.be */ + +function decode_query_options(url) +{ + const query_options = {}; + const match = /^[^?]*\?(.*)/.exec(url); + if (!match) + return query_options; + + for (const opt of match[1].split("&")) { + const [key, val] = + /^([^=]*)=?(.*)/.exec(opt).splice(1, 2).map(decodeURIComponent); + query_options[key] = val; + } + + return query_options; +} + +function encode_query_options(query_options) +{ + return Object.entries(query_options) + .map(ar => ar.map(encodeURIComponent).join("=")).join("&"); +} + +function remake_url(youtube_url) +{ + const query_options = decode_query_options(youtube_url); + + let endpoint = ""; + + const match = /^(?:(?:https?)?\/\/)?[^/]*\/([^?]*)/.exec(youtube_url); + if (match) + endpoint = match[1]; + + if (/^embed\//.test(query_options.v)) + endpoint = query_options.v; + + if (/^embed\/.+/.test(endpoint)) + delete query_options.v + + const encoded_options = encode_query_options(query_options); + return `https://yewtu.be/${endpoint}?${encoded_options}`; +} + +window.location.href = remake_url(document.URL); |