-
-
Notifications
You must be signed in to change notification settings - Fork 30
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
🔀 Merge pull request #5 from younesaassila/v1.1.0
🔖 Release v1.0.1
- Loading branch information
Showing
9 changed files
with
146 additions
and
114 deletions.
There are no files selected for viewing
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
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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
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
This file was deleted.
Oops, something went wrong.
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
import browser from "webextension-polyfill"; | ||
import onBeforeRequest from "./handlers/onBeforeRequest"; | ||
import onBeforeSendHeaders from "./handlers/onBeforeSendHeaders"; | ||
|
||
// Redirect the HLS master manifest request to TTV LOL's API. | ||
browser.webRequest.onBeforeRequest.addListener( | ||
onBeforeRequest, | ||
{ | ||
urls: [ | ||
"https://usher.ttvnw.net/api/channel/hls/*", | ||
"https://usher.ttvnw.net/vod/*", | ||
], | ||
}, | ||
["blocking"] | ||
); | ||
|
||
// Add the `X-Donate-To` header to API requests. | ||
browser.webRequest.onBeforeSendHeaders.addListener( | ||
onBeforeSendHeaders, | ||
{ urls: ["https://api.ttv.lol/playlist/*", "https://api.ttv.lol/vod/*"] }, | ||
["blocking", "requestHeaders"] | ||
); |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,79 @@ | ||
import { WebRequest } from "webextension-polyfill"; | ||
import { PlaylistType, Token } from "../../../types"; | ||
|
||
export default function (details: WebRequest.OnBeforeRequestDetailsType) { | ||
const twitchApiUrlRegex = /\/(hls|vod)\/(.+)\.m3u8(?:\?(.*))?$/gim; | ||
|
||
const match = twitchApiUrlRegex.exec(details.url); | ||
if (match == null) return {}; | ||
|
||
const [_, _type, channelName, _params] = match; | ||
if (_type == null || channelName == null) return {}; | ||
|
||
const playlistType = | ||
_type.toLowerCase() === "vod" ? PlaylistType.VOD : PlaylistType.Playlist; | ||
const searchParams = new URLSearchParams(_params); | ||
|
||
let token: Token; | ||
try { | ||
token = JSON.parse(searchParams.get("token")); | ||
} catch {} | ||
|
||
if (token != null) { | ||
// No redirect if the user is a subscriber, has Twitch Turbo, or is a partner. | ||
if ( | ||
token.subscriber === true || | ||
token.turbo === true || | ||
token.partner === true | ||
) { | ||
console.log( | ||
`${channelName}: TTV LOL disabled (User is a subscriber, has Twitch Turbo, or is a partner)` | ||
); | ||
return {}; | ||
} | ||
|
||
// Remove sensitive information from the token (when possible). | ||
if (playlistType === PlaylistType.Playlist) delete token.device_id; | ||
if (playlistType === PlaylistType.Playlist) delete token.user_id; | ||
delete token.user_ip; | ||
searchParams.set("token", JSON.stringify(token)); | ||
} | ||
|
||
const pingUrl = "https://api.ttv.lol/ping"; | ||
const redirectUrl = `https://api.ttv.lol/${playlistType}/${encodeURIComponent( | ||
`${channelName}.m3u8?${searchParams.toString()}` | ||
)}`; | ||
|
||
// @ts-ignore | ||
const isChrome = !!chrome.app; | ||
if (isChrome) { | ||
// Synchronous XMLHttpRequest is required for the extension to work in Chrome. | ||
const request = new XMLHttpRequest(); | ||
request.open("GET", pingUrl, false); | ||
request.send(); | ||
|
||
if (request.status === 200) { | ||
console.log(`${channelName}: TTV LOL enabled`); | ||
return { | ||
redirectUrl, | ||
}; | ||
} else { | ||
return {}; | ||
} | ||
} else { | ||
return new Promise(resolve => { | ||
fetch(pingUrl) | ||
.then(response => { | ||
if (response.status === 200) { | ||
console.log(`${channelName}: TTV LOL enabled`); | ||
resolve({ | ||
redirectUrl, | ||
}); | ||
} else { | ||
resolve({}); | ||
} | ||
}) | ||
.catch(() => resolve({})); | ||
}); | ||
} | ||
} |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
import { WebRequest } from "webextension-polyfill"; | ||
|
||
export default function (details: WebRequest.OnBeforeSendHeadersDetailsType) { | ||
details.requestHeaders.push({ | ||
name: "X-Donate-To", | ||
value: "https://ttv.lol/donate", | ||
}); | ||
|
||
return { | ||
requestHeaders: details.requestHeaders, | ||
}; | ||
} |
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