Skip to content

Commit

Permalink
🔀 Merge pull request #33 from younesaassila/v1.5.0
Browse files Browse the repository at this point in the history
🔖 Release version 1.5.0
  • Loading branch information
younesaassila authored Dec 16, 2022
2 parents 3745a7c + f45238b commit 157f3ff
Show file tree
Hide file tree
Showing 17 changed files with 466 additions and 270 deletions.
9 changes: 5 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -71,9 +71,10 @@ This fork:

> ❌ 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. Download the latest version of this extension in the "Releases" section (CRX file)
1. Go to `chrome://extensions`
1. Turn on `Developer mode`
1. Click on `Load unpacked`
1. Select the unzipped folder you just created
1. Drag and drop the CRX file you just downloaded
1. If you see the following message:
![Warning message](https://i.imgur.com/bL08ES3.png)
1. Close and reopen `chrome://extensions`.
109 changes: 26 additions & 83 deletions package-lock.json

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

11 changes: 6 additions & 5 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "ttv-lol-pro",
"version": "1.4.0",
"version": "1.5.0",
"description": "TTV LOL PRO removes livestream ads from Twitch",
"@parcel/bundler-default": {
"minBundles": 10000000,
Expand Down Expand Up @@ -33,11 +33,9 @@
"Younes Aassila (https://github.com/younesaassila)"
],
"license": "GPL-3.0",
"dependencies": {
"private-ip": "^2.3.4"
},
"devDependencies": {
"@parcel/config-webextension": "^2.8.0",
"@types/semver-compare": "^1.0.1",
"@types/webextension-polyfill": "^0.9.1",
"parcel": "^2.8.0",
"postcss": "^8.4.19",
Expand All @@ -47,5 +45,8 @@
"typescript": "^4.9.3",
"webextension-polyfill": "^0.10.0"
},
"private": true
"private": true,
"dependencies": {
"semver-compare": "^1.0.0"
}
}
5 changes: 5 additions & 0 deletions src/background/background.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,12 @@
import browser from "webextension-polyfill";
import isChrome from "../common/ts/isChrome";
import onBeforeRequest from "./handlers/onBeforeRequest";
import onBeforeSendHeaders from "./handlers/onBeforeSendHeaders";
import onHeadersReceived from "./handlers/onHeadersReceived";
import onStartup from "./handlers/onStartup";

// Check for updates on Chrome startup.
if (isChrome) browser.runtime.onStartup.addListener(onStartup);

// Redirect the HLS master manifest request to TTV LOL's API.
browser.webRequest.onBeforeRequest.addListener(
Expand Down
49 changes: 38 additions & 11 deletions src/background/handlers/onBeforeRequest.ts
Original file line number Diff line number Diff line change
Expand Up @@ -43,11 +43,15 @@ export default function onBeforeRequest(

if (token) {
// No redirect if the user is a subscriber, has Twitch Turbo, or is a partner.
if (
const isPremiumUser =
token.subscriber === true ||
token.turbo === true ||
token.partner === true
) {
token.partner === true;
const isIgnoredChannelSubscription =
store.state.ignoredChannelSubscriptions.some(
channel => channel.toLowerCase() === channelName
);
if (isPremiumUser && !isIgnoredChannelSubscription) {
console.log(
`${streamId}: No redirect (User is a subscriber, has Twitch Turbo, or is a partner)`
);
Expand Down Expand Up @@ -82,6 +86,23 @@ export default function onBeforeRequest(
else return redirectFirefox(playlistType, streamId, searchParams);
}

function getPingUrl(server: string): string {
return `${server}${server.endsWith("/") ? "" : "/"}ping`;
}

function getRedirectUrl(
server: string,
playlistType: PlaylistType,
streamId: string,
searchParams: URLSearchParams
): string {
return `${server}${
server.endsWith("/") ? "" : "/"
}${playlistType}/${encodeURIComponent(
`${streamId}.m3u8?${searchParams.toString()}`
)}`;
}

function setStreamStatus(
streamId: string,
redirected: boolean,
Expand All @@ -104,10 +125,13 @@ function redirectChrome(
const servers = store.state.servers;

for (const server of servers) {
const pingUrl = `${server}/ping`;
const redirectUrl = `${server}/${playlistType}/${encodeURIComponent(
`${streamId}.m3u8?${searchParams.toString()}`
)}`;
const pingUrl = getPingUrl(server);
const redirectUrl = getRedirectUrl(
server,
playlistType,
streamId,
searchParams
);

// Synchronous XMLHttpRequest is required for the extension to work in Chrome.
const request = new XMLHttpRequest();
Expand Down Expand Up @@ -148,10 +172,13 @@ function redirectFirefox(
return resolve({});
}

const pingUrl = `${server}/ping`;
const redirectUrl = `${server}/${playlistType}/${encodeURIComponent(
`${streamId}.m3u8?${searchParams.toString()}`
)}`;
const pingUrl = getPingUrl(server);
const redirectUrl = getRedirectUrl(
server,
playlistType,
streamId,
searchParams
);
const fallback = () => {
console.log(`${streamId}: Ping to ${server} failed`);
tryRedirect(servers[++i]);
Expand Down
47 changes: 47 additions & 0 deletions src/background/handlers/onStartup.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
import semverCompare from "semver-compare";
import browser from "webextension-polyfill";
import store from "../../store";

//#region Types
type Update = {
version: string;
update_link: string;
};
//#endregion

export default async function onStartup() {
if (store.readyState !== "complete") {
store.addEventListener("load", () => onStartup());
return;
}

// If the last update check was less than 24 hours ago, don't check again.
if (
!store.state.isUpdateAvailable &&
Date.now() - store.state.lastUpdateCheck < 1000 * 60 * 60 * 24
) {
return;
}

const manifest = browser.runtime.getManifest();
const currentVersion = manifest.version;
const updateUrl = manifest.browser_specific_settings?.gecko?.update_url;
if (!updateUrl) {
store.state.isUpdateAvailable = false;
return;
}

try {
const response = await fetch(updateUrl);
const json = await response.json();
const updates = json.addons["{76ef94a4-e3d0-4c6f-961a-d38a429a332b}"]
.updates as Update[];
updates.sort((a, b) => semverCompare(a.version, b.version));
const latestVersion = updates[updates.length - 1].version;
store.state.isUpdateAvailable =
semverCompare(currentVersion, latestVersion) < 0; // currentVersion < latestVersion
store.state.lastUpdateCheck = Date.now();
} catch {
store.state.isUpdateAvailable = false;
}
}
Loading

0 comments on commit 157f3ff

Please sign in to comment.