Project

General

Profile

« Previous | Next » 

Revision ba1e6824

Added by koszko over 1 year ago

refactor yewtu.be URL generation into a js library

View differences:

content/byoutube/index.json
1
{
2
"type" : "bag",
3
"name" : "youtube_2_yewtube_redirection",
4
"components" : [
5
["script", "youtube.com_2_yewtu.be_URL_converter"],
6
["script", "youtube_2_yewtube_redirection"]
7
]
8
}
content/pyoutube/index.json
1 1
{
2 2
"type" : "page",
3 3
"pattern" : "https://www.youtube.com/***",
4
"payload" : ["script", "youtube->yewtu.be"]
4
"payload" : ["bag", "youtube_2_yewtube_redirection"]
5 5
}
content/syewtube_urls/index.json
1
{
2
"type" : "script",
3
"name" : "youtube.com_2_yewtu.be_URL_converter",
4
"sha256" : "ae2abac001a7256f4b4a66846264470b2898aa736a55f91022017ce7634dd1ae",
5
"location" : "yewtube_urls.js"
6
}
content/syewtube_urls/yewtube_urls.js
1
/**
2
 * Copyright (C) 2021, Wojtek Kosior
3
 *
4
 * This program is free software: you can redistribute it and/or modify
5
 * it under the terms of the CC0 1.0 Universal License as published by
6
 * the Creative Commons Corporation.
7
 *
8
 * This program is distributed in the hope that it will be useful,
9
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
11
 * CC0 1.0 Universal License for more details.
12
 * Available under the terms of Creative Commons Zero.
13
 */
14

  
15
/* Library to convert youtube.com URLs to yewtu.be URLs. */
16

  
17
function decode_query_options(url)
18
{
19
    const query_options = {};
20
    const match = /^[^?]*\?(.*)/.exec(url);
21
    if (!match)
22
	return query_options;
23

  
24
    for (const opt of match[1].split("&")) {
25
	const [key, val] =
26
	      /^([^=]*)=?(.*)/.exec(opt).splice(1, 2).map(decodeURIComponent);
27
	query_options[key] = val;
28
    }
29

  
30
    return query_options;
31
}
32

  
33
function encode_query_options(query_options)
34
{
35
    return Object.entries(query_options)
36
	.map(ar => ar.map(encodeURIComponent).join("=")).join("&");
37
}
38

  
39
function make_yewtube_url(youtube_url)
40
{
41
    const query_options = decode_query_options(youtube_url);
42

  
43
    let endpoint = "";
44

  
45
    const match = /^(?:(?:https?:)?\/\/)?[^/]*\/([^?]*)/.exec(youtube_url);
46
    if (match)
47
	endpoint = match[1];
48

  
49
    if (/^embed\//.test(query_options.v))
50
	endpoint = query_options.v;
51

  
52
    if (/^embed\/.+/.test(endpoint))
53
	delete query_options.v
54

  
55
    const encoded_options = encode_query_options(query_options);
56
    return `https://yewtu.be/${endpoint}?${encoded_options}`;
57
}
content/syoutube/index.json
1 1
{
2 2
"type" : "script",
3
"name" : "youtube->yewtu.be",
4
"sha256" : "8fade4c6d87cb717289c382f71f1aaa95f8122041eb173d5e1267c13e68c56e1",
5
"location" : "youtube.js"
3
"name" : "youtube_2_yewtube_redirection",
4
"sha256" : "5a6b0495f122459cae22d921c3719bc5c449f30eefcc070e8aa2b27cf4e01b70",
5
"location" : "youtube_yewtube_redirection.js"
6 6
}
content/syoutube/youtube.js
1
/**
2
 * Copyright (C) 2021, Wojtek Kosior
3
 *
4
 * This program is free software: you can redistribute it and/or modify
5
 * it under the terms of the CC0 1.0 Universal License as published by
6
 * the Creative Commons Corporation.
7
 *
8
 * This program is distributed in the hope that it will be useful,
9
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
11
 * CC0 1.0 Universal License for more details.
12
 * Available under the terms of Creative Commons Zero.
13
 */
14

  
15
/* Use with https://www.youtube.com/*** */
16

  
17
/* Redirect youtube.com to yewtu.be */
18

  
19
function decode_query_options(url)
20
{
21
    const query_options = {};
22
    const match = /^[^?]*\?(.*)/.exec(url);
23
    if (!match)
24
	return query_options;
25

  
26
    for (const opt of match[1].split("&")) {
27
	const [key, val] =
28
	      /^([^=]*)=?(.*)/.exec(opt).splice(1, 2).map(decodeURIComponent);
29
	query_options[key] = val;
30
    }
31

  
32
    return query_options;
33
}
34

  
35
function encode_query_options(query_options)
36
{
37
    return Object.entries(query_options)
38
	.map(ar => ar.map(encodeURIComponent).join("=")).join("&");
39
}
40

  
41
function remake_url(youtube_url)
42
{
43
    const query_options = decode_query_options(youtube_url);
44

  
45
    let endpoint = "";
46

  
47
    const match = /^(?:(?:https?)?\/\/)?[^/]*\/([^?]*)/.exec(youtube_url);
48
    if (match)
49
	endpoint = match[1];
50

  
51
    if (/^embed\//.test(query_options.v))
52
	endpoint = query_options.v;
53

  
54
    if (/^embed\/.+/.test(endpoint))
55
	delete query_options.v
56

  
57
    const encoded_options = encode_query_options(query_options);
58
    return `https://yewtu.be/${endpoint}?${encoded_options}`;
59
}
60

  
61
window.location.href = remake_url(document.URL);
content/syoutube/youtube_yewtube_redirection.js
1
/**
2
 * Copyright (C) 2021, Wojtek Kosior
3
 *
4
 * This program is free software: you can redistribute it and/or modify
5
 * it under the terms of the CC0 1.0 Universal License as published by
6
 * the Creative Commons Corporation.
7
 *
8
 * This program is distributed in the hope that it will be useful,
9
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
11
 * CC0 1.0 Universal License for more details.
12
 * Available under the terms of Creative Commons Zero.
13
 */
14

  
15
/* Use with https://www.youtube.com/*** */
16

  
17
/* Redirect youtube.com to yewtu.be */
18

  
19
window.location.href = make_yewtube_url(document.URL);

Also available in: Unified diff