mirror of
https://github.com/younesaassila/ttv-lol-pro.git
synced 2025-04-29 14:04:26 +02:00
58 lines
1.6 KiB
JavaScript
58 lines
1.6 KiB
JavaScript
function stripUnusedParams(str, params) {
|
|
if (!params) {
|
|
params = [ 'token', 'sig' ];
|
|
}
|
|
var tempUrl = new URL('https://localhost/' + str);
|
|
for (var i = 0; i < params.length; i++) {
|
|
tempUrl.searchParams.delete(params[i]);
|
|
}
|
|
return tempUrl.pathname.substring(1) + tempUrl.search;
|
|
}
|
|
|
|
function onPlaylistBeforeRequest(details) {
|
|
|
|
details.url = stripUnusedParams(details.url, null);
|
|
|
|
// (hls\/|vod\/)(.+?)$
|
|
const match = /(hls|vod)\/(.+?)$/gim.exec(details.url);
|
|
|
|
if (match !== null && match.length > 1) {
|
|
var playlistType = match[1] == "vod" ? "vod" : "playlist";
|
|
|
|
var req = new XMLHttpRequest();
|
|
req.open("GET", `https://api.ttv.lol/ping`, false);
|
|
req.send();
|
|
|
|
// validate that our API is online, if not fallback to standard stream with ads
|
|
if (req.status != 200) {
|
|
return {
|
|
redirectUrl: details.url
|
|
};
|
|
} else {
|
|
return {
|
|
redirectUrl: `https://api.ttv.lol/${playlistType}/${encodeURIComponent(match[2])}`,
|
|
};
|
|
}
|
|
|
|
}
|
|
}
|
|
|
|
chrome.webRequest.onBeforeRequest.addListener(
|
|
onPlaylistBeforeRequest,
|
|
{ urls: ["https://usher.ttvnw.net/api/channel/hls/*", "https://usher.ttvnw.net/vod/*"] },
|
|
["blocking", "extraHeaders"]
|
|
);
|
|
|
|
function onBeforeSendHeaders(req) {
|
|
req.requestHeaders.push({ name: 'X-Donate-To', value: "https://ttv.lol/donate" })
|
|
return {
|
|
requestHeaders: req.requestHeaders
|
|
}
|
|
}
|
|
|
|
chrome.webRequest.onBeforeSendHeaders.addListener(
|
|
onBeforeSendHeaders,
|
|
{ urls: ["https://api.ttv.lol/playlist/*", "https://api.ttv.lol/vod/*"] },
|
|
["blocking", "requestHeaders"]
|
|
);
|