Skip to content

Commit

Permalink
🔀 Merge pull request #5 from younesaassila/v1.1.0
Browse files Browse the repository at this point in the history
🔖 Release v1.0.1
  • Loading branch information
younesaassila authored May 17, 2022
2 parents 6521a5c + 68de8e5 commit 1ee94ea
Show file tree
Hide file tree
Showing 9 changed files with 146 additions and 114 deletions.
6 changes: 5 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ This is a fork of the original project at https://github.com/TTV-LOL/extensions

>
[TTV LOL](https://ttv.lol/) removes livestream ads from [Twitch](https://www.twitch.tv/).
[TTV LOL](https://ttv.lol/) removes livestream ads from [twitch.tv](https://www.twitch.tv/)

This fork:

Expand All @@ -44,6 +44,8 @@ This fork:

### Chrome

`❌ The extension does not update automatically.`

1. Download the latest version of this extension in the "Releases" section (ZIP file)
1. Unzip the ZIP file you just downloaded
1. Go to `chrome://extensions`
Expand All @@ -53,6 +55,8 @@ This fork:

### Firefox

`✅ The add-on updates automatically.`

1. Download the latest version of this extension in the "Releases" section (XPI file)
1. Go to `about:addons`
1. Click on the gear icon then select "Install Add-on From File…"
Expand Down
4 changes: 2 additions & 2 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "ttv-lol",
"version": "1.0.0-younesaassila",
"version": "1.0.1-younesaassila",
"description": "TTV LOL removes livestream ads from twitch.tv",
"targets": {
"webext-dev": {
Expand All @@ -11,6 +11,7 @@
},
"webext-prod": {}
},
"browserslist": "> 0.5%, last 2 versions, not dead",
"scripts": {
"dev": "parcel src/manifest.json --host localhost --target webext-dev",
"lint": "prettier --write ./src",
Expand Down
4 changes: 2 additions & 2 deletions src/manifest.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,10 @@
"manifest_version": 2,
"name": "TTV LOL (Younes Aassila's Fork)",
"description": "TTV LOL removes livestream ads from twitch.tv",
"version": "1.0.0",
"version": "1.0.1",
"background": {
"persistent": true,
"scripts": ["ts/background.ts"]
"scripts": ["ts/background/background.ts"]
},
"browser_action": {
"default_icon": {
Expand Down
87 changes: 0 additions & 87 deletions src/ts/background.ts

This file was deleted.

22 changes: 22 additions & 0 deletions src/ts/background/background.ts
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"]
);
79 changes: 79 additions & 0 deletions src/ts/background/handlers/onBeforeRequest.ts
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({}));
});
}
}
12 changes: 12 additions & 0 deletions src/ts/background/handlers/onBeforeSendHeaders.ts
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,
};
}
43 changes: 22 additions & 21 deletions src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,40 +4,41 @@ export enum PlaylistType {
}

export interface Token {
adblock: boolean;
adblock?: boolean;
authorization: {
forbidden: boolean;
reason: string;
};
blackout_enabled: boolean;
channel: string;
channel_id: number;
blackout_enabled?: boolean;
channel?: string;
channel_id?: number;
chansub: {
restricted_bitrates?: number[];
restricted_bitrates?: string[];
view_until: number;
};
ci_gb: boolean;
geoblock_reason: string;
ci_gb?: boolean;
geoblock_reason?: string;
device_id: string;
expires: number;
extended_history_allowed: boolean;
game: string;
hide_ads: boolean;
extended_history_allowed?: boolean;
game?: string;
hide_ads?: boolean;
https_required: boolean;
mature: boolean;
partner: boolean;
platform: string;
player_type: string;
private: {
mature?: boolean;
partner?: boolean;
platform?: string;
player_type?: string;
private?: {
allowed_to_view: boolean;
};
privileged: boolean;
role: string;
server_ads: boolean;
show_ads: boolean;
subscriber: boolean;
turbo: boolean;
role?: string;
server_ads?: boolean;
show_ads?: boolean;
subscriber?: boolean;
turbo?: boolean;
user_id?: number;
user_ip: string;
user_ip?: string;
version: number;
vod_id?: number;
}

0 comments on commit 1ee94ea

Please sign in to comment.