Fix 360p issue on "vaft"

Also added code to auto reformat the script #63

Ref:
https://github.com/cleanlock/VideoAdBlockForTwitch/issues/36
398c6062dc
This commit is contained in:
pixeltris 2022-04-04 12:01:50 +01:00
parent 57097e9c9c
commit 0fd419cc94
4 changed files with 83 additions and 14 deletions

View File

@ -32,7 +32,7 @@ Alternatively:
- *The ad/non-ad transition takes slightly longer than `notify-strip`*. - *The ad/non-ad transition takes slightly longer than `notify-strip`*.
- vaft - [ublock](https://github.com/pixeltris/TwitchAdSolutions/raw/master/vaft/vaft-ublock-origin.js) / [userscript](https://github.com/pixeltris/TwitchAdSolutions/raw/master/vaft/vaft.user.js) / [ublock (permalink)](https://github.com/pixeltris/TwitchAdSolutions/raw/71bed117fc0f7074a9c7a7e89000dbf7db1feb04/vaft/vaft-ublock-origin.js) - vaft - [ublock](https://github.com/pixeltris/TwitchAdSolutions/raw/master/vaft/vaft-ublock-origin.js) / [userscript](https://github.com/pixeltris/TwitchAdSolutions/raw/master/vaft/vaft.user.js) / [ublock (permalink)](https://github.com/pixeltris/TwitchAdSolutions/raw/71bed117fc0f7074a9c7a7e89000dbf7db1feb04/vaft/vaft-ublock-origin.js)
- `Video Ad-Block, for Twitch` (fork) as a script. - `Video Ad-Block, for Twitch` (fork) as a script.
- low-res [ublock](https://github.com/pixeltris/TwitchAdSolutions/raw/master/low-res/low-res-ublock-origin.js) / [userscript](https://github.com/pixeltris/TwitchAdSolutions/raw/master/low-res/low-res.user.js) / [ublock (permalink)](https://github.com/pixeltris/TwitchAdSolutions/raw/71bed117fc0f7074a9c7a7e89000dbf7db1feb04/low-res/low-res-ublock-origin.js) - low-res - [ublock](https://github.com/pixeltris/TwitchAdSolutions/raw/master/low-res/low-res-ublock-origin.js) / [userscript](https://github.com/pixeltris/TwitchAdSolutions/raw/master/low-res/low-res.user.js) / [ublock (permalink)](https://github.com/pixeltris/TwitchAdSolutions/raw/71bed117fc0f7074a9c7a7e89000dbf7db1feb04/low-res/low-res-ublock-origin.js)
- No ads. - No ads.
- The stream is 480p for the duration of the stream. - The stream is 480p for the duration of the stream.

View File

@ -74,7 +74,7 @@ namespace TwitchAdUtils
static void BuildScripts() static void BuildScripts()
{ {
string[] deprecated = { "dyn-skip-midroll-alt", "dyn-skip-midroll", "dyn-video-swap", "dyn", "dyn-skip" }; string[] deprecated = { };
string baseScriptName = "base"; string baseScriptName = "base";
string suffixConfg = ".cfg"; string suffixConfg = ".cfg";
string suffixUserscript = ".user.js"; string suffixUserscript = ".user.js";
@ -179,6 +179,64 @@ namespace TwitchAdUtils
} }
} }
} }
using (WebClient wc = new WebClient())
{
string response = null, token = null, sig = null;
wc.Proxy = null;
string code = wc.DownloadString("https://raw.githubusercontent.com/cleanlock/VideoAdBlockForTwitch/master/chrome/remove_video_ads.js");
List<string> lines = code.Split(new char[] { '\r', '\n' }, StringSplitOptions.RemoveEmptyEntries).ToList();
for (int i = lines.Count - 1; i >= 0; i--)
{
if (string.IsNullOrWhiteSpace(lines[i]))
{
lines.RemoveAt(i);
}
else
{
lines[i] = " " + lines[i];
}
}
string manifestStr = wc.DownloadString("https://raw.githubusercontent.com/cleanlock/VideoAdBlockForTwitch/master/chrome/manifest.json");
ChromeExtensionManifest manifest = JSONSerializer<ChromeExtensionManifest>.DeSerialize(manifestStr);
Console.WriteLine("vaft: " + manifest.version);
string comment = "// This code is directly copied from https://github.com/cleanlock/VideoAdBlockForTwitch (only change is whitespace is removed for the ublock origin script - also indented)";
StringBuilder sbUserscript = new StringBuilder();
sbUserscript.AppendLine("// ==UserScript==");
sbUserscript.AppendLine("// @name TwitchAdSolutions (vaft)");
sbUserscript.AppendLine("// @namespace https://github.com/pixeltris/TwitchAdSolutions");
sbUserscript.AppendLine("// @version " + manifest.version);
sbUserscript.AppendLine("// @description Multiple solutions for blocking Twitch ads (vaft)");
sbUserscript.AppendLine("// @updateURL https://github.com/pixeltris/TwitchAdSolutions/raw/master/vaft/vaft.user.js");
sbUserscript.AppendLine("// @downloadURL https://github.com/pixeltris/TwitchAdSolutions/raw/master/vaft/vaft.user.js");
sbUserscript.AppendLine("// @author https://github.com/cleanlock/VideoAdBlockForTwitch#credits");
sbUserscript.AppendLine("// @match *://*.twitch.tv/*");
sbUserscript.AppendLine("// @run-at document-start");
sbUserscript.AppendLine("// @grant none");
sbUserscript.AppendLine("// ==/UserScript==");
sbUserscript.AppendLine(comment);
sbUserscript.AppendLine("(function() {");
sbUserscript.AppendLine(" 'use strict';");
StringBuilder sbUblock = new StringBuilder();
sbUblock.AppendLine(comment);
sbUblock.AppendLine("twitch-videoad.js application/javascript");
sbUblock.AppendLine("(function() {");
sbUblock.AppendLine(" if ( /(^|\\.)twitch\\.tv$/.test(document.location.hostname) === false ) { return; }");
foreach (string line in lines)
{
sbUserscript.AppendLine(line);
sbUblock.AppendLine(line);
}
sbUserscript.AppendLine("})();");
sbUblock.AppendLine("})();");
File.WriteAllText(Path.Combine("vaft", "vaft.user.js"), sbUserscript.ToString());
File.WriteAllText(Path.Combine("vaft", "vaft-ublock-origin.js"), sbUblock.ToString());
}
} }
static void Run(RunnerMode mode, string channel) static void Run(RunnerMode mode, string channel)
@ -384,7 +442,7 @@ namespace TwitchAdUtils
} }
else else
{ {
Console.WriteLine("Failed to get stream token"); Console.WriteLine("Failed to get stream token mode:" + mode);
} }
} }
Thread.Sleep(LoopDelay); Thread.Sleep(LoopDelay);
@ -619,6 +677,13 @@ namespace TwitchAdUtils
public string signature { get; set; } public string signature { get; set; }
} }
[DataContract]
public class ChromeExtensionManifest
{
[DataMember]
public string version { get; set; }
}
class CookieAwareWebClient : WebClient class CookieAwareWebClient : WebClient
{ {
public CookieContainer CookieContainer { get; set; } public CookieContainer CookieContainer { get; set; }

View File

@ -114,9 +114,9 @@ twitch-videoad.js application/javascript
GQLDeviceID = e.data.value; GQLDeviceID = e.data.value;
} else if (e.data.key == 'SetHideBlockingMessage') { } else if (e.data.key == 'SetHideBlockingMessage') {
if (e.data.value == "true") { if (e.data.value == "true") {
HideBlockingMessage = true;
} else if (e.data.value == "false") {
HideBlockingMessage = false; HideBlockingMessage = false;
} else if (e.data.value == "false") {
HideBlockingMessage = true;
} }
} }
}); });
@ -208,9 +208,9 @@ twitch-videoad.js application/javascript
qualityToSelect = 0; qualityToSelect = 0;
} }
} }
var currentQualityLS = window.localStorage.getItem('video-quality');
lowQuality[qualityToSelect].click(); lowQuality[qualityToSelect].click();
var originalQuality = JSON.parse(OriginalVideoPlayerQuality); window.localStorage.setItem('video-quality', currentQualityLS);
window.localStorage.setItem('video-quality', '{"default":"'+originalQuality.group+'"}');
if (e.data.value != null) { if (e.data.value != null) {
OriginalVideoPlayerQuality = null; OriginalVideoPlayerQuality = null;
IsPlayerAutoQuality = null; IsPlayerAutoQuality = null;
@ -381,6 +381,7 @@ twitch-videoad.js application/javascript
var streamM3u8Response = await realFetch(streamM3u8Url); var streamM3u8Response = await realFetch(streamM3u8Url);
if (streamM3u8Response.status == 200) { if (streamM3u8Response.status == 200) {
var m3u8Text = await streamM3u8Response.text(); var m3u8Text = await streamM3u8Response.text();
console.log("Blocking ads...");
WasShowingAd = true; WasShowingAd = true;
if (HideBlockingMessage == false) { if (HideBlockingMessage == false) {
postMessage({ postMessage({
@ -408,6 +409,7 @@ twitch-videoad.js application/javascript
} }
} else { } else {
if (WasShowingAd) { if (WasShowingAd) {
console.log("Done blocking ads, changing back to original quality");
WasShowingAd = false; WasShowingAd = false;
//Here we put player back to original quality and remove the blocking message. //Here we put player back to original quality and remove the blocking message.
postMessage({ postMessage({
@ -723,4 +725,4 @@ twitch-videoad.js application/javascript
}; };
} }
hookFetch(); hookFetch();
})(); })();

View File

@ -1,7 +1,7 @@
// ==UserScript== // ==UserScript==
// @name TwitchAdSolutions (vaft) // @name TwitchAdSolutions (vaft)
// @namespace https://github.com/pixeltris/TwitchAdSolutions // @namespace https://github.com/pixeltris/TwitchAdSolutions
// @version 5.3.5 // @version 5.4.0
// @description Multiple solutions for blocking Twitch ads (vaft) // @description Multiple solutions for blocking Twitch ads (vaft)
// @updateURL https://github.com/pixeltris/TwitchAdSolutions/raw/master/vaft/vaft.user.js // @updateURL https://github.com/pixeltris/TwitchAdSolutions/raw/master/vaft/vaft.user.js
// @downloadURL https://github.com/pixeltris/TwitchAdSolutions/raw/master/vaft/vaft.user.js // @downloadURL https://github.com/pixeltris/TwitchAdSolutions/raw/master/vaft/vaft.user.js
@ -125,9 +125,9 @@
GQLDeviceID = e.data.value; GQLDeviceID = e.data.value;
} else if (e.data.key == 'SetHideBlockingMessage') { } else if (e.data.key == 'SetHideBlockingMessage') {
if (e.data.value == "true") { if (e.data.value == "true") {
HideBlockingMessage = true;
} else if (e.data.value == "false") {
HideBlockingMessage = false; HideBlockingMessage = false;
} else if (e.data.value == "false") {
HideBlockingMessage = true;
} }
} }
}); });
@ -219,9 +219,9 @@
qualityToSelect = 0; qualityToSelect = 0;
} }
} }
var currentQualityLS = window.localStorage.getItem('video-quality');
lowQuality[qualityToSelect].click(); lowQuality[qualityToSelect].click();
var originalQuality = JSON.parse(OriginalVideoPlayerQuality); window.localStorage.setItem('video-quality', currentQualityLS);
window.localStorage.setItem('video-quality', '{"default":"'+originalQuality.group+'"}');
if (e.data.value != null) { if (e.data.value != null) {
OriginalVideoPlayerQuality = null; OriginalVideoPlayerQuality = null;
IsPlayerAutoQuality = null; IsPlayerAutoQuality = null;
@ -392,6 +392,7 @@
var streamM3u8Response = await realFetch(streamM3u8Url); var streamM3u8Response = await realFetch(streamM3u8Url);
if (streamM3u8Response.status == 200) { if (streamM3u8Response.status == 200) {
var m3u8Text = await streamM3u8Response.text(); var m3u8Text = await streamM3u8Response.text();
console.log("Blocking ads...");
WasShowingAd = true; WasShowingAd = true;
if (HideBlockingMessage == false) { if (HideBlockingMessage == false) {
postMessage({ postMessage({
@ -419,6 +420,7 @@
} }
} else { } else {
if (WasShowingAd) { if (WasShowingAd) {
console.log("Done blocking ads, changing back to original quality");
WasShowingAd = false; WasShowingAd = false;
//Here we put player back to original quality and remove the blocking message. //Here we put player back to original quality and remove the blocking message.
postMessage({ postMessage({
@ -734,4 +736,4 @@
}; };
} }
hookFetch(); hookFetch();
})(); })();