Fix preroll on every page refresh #188

This commit is contained in:
pixeltris
2023-10-11 11:08:30 +01:00
parent e8ff2b03a5
commit a285eeda50
4 changed files with 130 additions and 48 deletions
+30 -10
View File
@@ -1,7 +1,7 @@
// ==UserScript==
// @name TwitchAdSolutions (vaft)
// @namespace https://github.com/pixeltris/TwitchAdSolutions
// @version 5.8.4
// @version 5.8.5
// @description Multiple solutions for blocking Twitch ads (vaft)
// @updateURL https://github.com/pixeltris/TwitchAdSolutions/raw/master/vaft/vaft.user.js
// @downloadURL https://github.com/pixeltris/TwitchAdSolutions/raw/master/vaft/vaft.user.js
@@ -322,6 +322,7 @@
StreamInfos[channelName] = streamInfo = {};
}
streamInfo.ChannelName = channelName;
streamInfo.RequestedAds = new Set();
streamInfo.Urls = [];// xxx.m3u8 -> { Resolution: "284x160", FrameRate: 30.0 }
streamInfo.EncodingsM3U8Cache = [];
streamInfo.EncodingsM3U8 = encodingsM3u8;
@@ -482,6 +483,21 @@
var isMidroll = textStr.includes('"MIDROLL"') || textStr.includes('"midroll"');
//Reduces ad frequency. TODO: Reduce the number of requests. This is really spamming Twitch with requests.
if (!isMidroll) {
if (playerType === PlayerType2) {
var lines = textStr.replace('\r', '').split('\n');
for (var i = 0; i < lines.length; i++) {
var line = lines[i];
if (line.startsWith('#EXTINF') && lines.length > i + 1) {
if (!line.includes(',live') && !streamInfo.RequestedAds.has(lines[i + 1])) {
// Only request one .ts file per .m3u8 request to avoid making too many requests
//console.log('Fetch ad .ts file');
streamInfo.RequestedAds.add(lines[i + 1]);
fetch(lines[i + 1]).then((response)=>{response.blob()});
break;
}
}
}
}
try {
//tryNotifyTwitch(textStr);
} catch (err) {}
@@ -669,7 +685,7 @@
}
function gqlRequest(body, realFetch) {
if (ClientIntegrityHeader == null) {
console.warn('ClientIntegrityHeader is null');
//console.warn('ClientIntegrityHeader is null');
//throw 'ClientIntegrityHeader is null';
}
var fetchFunc = realFetch ? realFetch : fetch;
@@ -867,16 +883,20 @@
}
//Client integrity header
ClientIntegrityHeader = init.headers['Client-Integrity'];
twitchMainWorker.postMessage({
key: 'UpdateClientIntegrityHeader',
value: init.headers['Client-Integrity']
});
if (ClientIntegrityHeader && twitchMainWorker) {
twitchMainWorker.postMessage({
key: 'UpdateClientIntegrityHeader',
value: init.headers['Client-Integrity']
});
}
//Authorization header
AuthorizationHeader = init.headers['Authorization'];
twitchMainWorker.postMessage({
key: 'UpdateAuthorizationHeader',
value: init.headers['Authorization']
});
if (AuthorizationHeader && twitchMainWorker) {
twitchMainWorker.postMessage({
key: 'UpdateAuthorizationHeader',
value: init.headers['Authorization']
});
}
}
//To prevent pause/resume loop for mid-rolls.
if (url.includes('gql') && init && typeof init.body === 'string' && init.body.includes('PlaybackAccessToken') && init.body.includes('picture-by-picture')) {