about summary refs log tree commit diff stats
path: root/src/assets/javascripts/youtubeMusic.js
blob: 90a02ecf6ec05c89841c9d1fcff3ab4ff552d393 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
"use strict";

import utils from './utils.js'

window.browser = window.browser || window.chrome;

const targets = [
    /^https?:\/{2}music\.youtube\.com(\/.*|$)/,
];
let redirects = {
    "beatbump": {
        "normal": [
            "https://beatbump.ml"
        ],
        "tor": []
    },
};

let
    disableYoutubeMusic,
    beatbumpNormalRedirectsChecks,
    beatbumpNormalCustomRedirects;

function init() {
    browser.storage.local.get(
        [
            "disableYoutubeMusic",
            "beatbumpNormalRedirectsChecks",
            "beatbumpNormalCustomRedirects",
        ],
        r => {
            disableYoutubeMusic = r.disableYoutubeMusic;
            beatbumpNormalRedirectsChecks = r.beatbumpNormalRedirectsChecks;
            beatbumpNormalCustomRedirects = r.beatbumpNormalCustomRedirects;
        }
    )
}

init();
browser.storage.onChanged.addListener(init)

/* 
Video
https://music.youtube.com/watch?v=_PkGiKBW-DA&list=RDAMVM_PkGiKBW-DA
https://beatbump.ml/listen?id=_PkGiKBW-DA&list=RDAMVM_PkGiKBW-DA

Playlist
https://music.youtube.com/playlist?list=PLqxd0OMLeWy64zlwhjouj92ISc38FbOns
https://music.youtube.com/playlist?list=PLqxd0OMLeWy7lrJSzt9LnOJjbC1IaruPM
https://music.youtube.com/playlist?list=PLQod4DlD72ZMJmOrSNbmEmK_iZ1oXPzKd
https://beatbump.ml/playlist/VLPLqxd0OMLeWy64zlwhjouj92ISc38FbOns

Channel
https://music.youtube.com/channel/UCfgmMDI7T5tOQqjnOBRe_wg
https://beatbump.ml/artist/UCfgmMDI7T5tOQqjnOBRe_wg

Albums
https://music.youtube.com/playlist?list=OLAK5uy_n-9HVh3cryV2gREZM9Sc0JwEKYjjfi0dU
https://music.youtube.com/playlist?list=OLAK5uy_lcr5O1zS8f6WIFI_yxqVp2RK9Dyy2bbw0
https://beatbump.ml/release?id=MPREb_3DURc4yEUtD
https://beatbump.ml/release?id=MPREb_evaZrV1WNdS

https://music.youtube.com/playlist?list=OLAK5uy_n6OHVllUZUCnlIY1m-gUaH8uqkN3Y-Ca8
https://music.youtube.com/playlist?list=OLAK5uy_nBOTxAc3_RGB82-Z54jdARGxGaCYlpngY
https://beatbump.ml/release?id=MPREb_QygdC0wEoLe

https://music.youtube.com/watch?v=R6gSMSYKhKU&list=OLAK5uy_n-9HVh3cryV2gREZM9Sc0JwEKYjjfi0dU
*/
function redirect(url) {
    if (disableYoutubeMusic) return;
    if (!targets.some(rx => rx.test(url.href))) return;

    let instancesList = [...beatbumpNormalRedirectsChecks, ...beatbumpNormalCustomRedirects];
    if (instancesList.length === 0) return;
    const randomInstance = utils.getRandomInstance(instancesList);
    return `${randomInstance}${url.pathname}${url.search}`
        .replace("/watch?v=", "/listen?id=")
        .replace("/channel/", "/artist/")
        .replace("/playlist?list=", "/playlist/VL");
}

async function initDefaults() {
    await browser.storage.local.set({
        disableYoutubeMusic: true,
        youtubeMusicRedirects: redirects,

        beatbumpNormalRedirectsChecks: [...redirects.beatbump.normal],
        beatbumpNormalCustomRedirects: [],
    })
}

export default {
    redirect,
    initDefaults,
};