diff --git a/README.md b/README.md index bd1fa24..cd5b371 100644 --- a/README.md +++ b/README.md @@ -32,7 +32,7 @@ Alternatively: - *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) - `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. - The stream is 480p for the duration of the stream. diff --git a/utils.cs b/utils.cs index 45f1514..830790f 100644 --- a/utils.cs +++ b/utils.cs @@ -74,7 +74,7 @@ namespace TwitchAdUtils static void BuildScripts() { - string[] deprecated = { "dyn-skip-midroll-alt", "dyn-skip-midroll", "dyn-video-swap", "dyn", "dyn-skip" }; + string[] deprecated = { }; string baseScriptName = "base"; string suffixConfg = ".cfg"; 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 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.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) @@ -384,7 +442,7 @@ namespace TwitchAdUtils } else { - Console.WriteLine("Failed to get stream token"); + Console.WriteLine("Failed to get stream token mode:" + mode); } } Thread.Sleep(LoopDelay); @@ -619,6 +677,13 @@ namespace TwitchAdUtils public string signature { get; set; } } + [DataContract] + public class ChromeExtensionManifest + { + [DataMember] + public string version { get; set; } + } + class CookieAwareWebClient : WebClient { public CookieContainer CookieContainer { get; set; } diff --git a/vaft/vaft-ublock-origin.js b/vaft/vaft-ublock-origin.js index 49dab7f..3e9ff0d 100644 --- a/vaft/vaft-ublock-origin.js +++ b/vaft/vaft-ublock-origin.js @@ -114,9 +114,9 @@ twitch-videoad.js application/javascript GQLDeviceID = e.data.value; } else if (e.data.key == 'SetHideBlockingMessage') { if (e.data.value == "true") { - HideBlockingMessage = true; - } else if (e.data.value == "false") { HideBlockingMessage = false; + } else if (e.data.value == "false") { + HideBlockingMessage = true; } } }); @@ -208,9 +208,9 @@ twitch-videoad.js application/javascript qualityToSelect = 0; } } + var currentQualityLS = window.localStorage.getItem('video-quality'); lowQuality[qualityToSelect].click(); - var originalQuality = JSON.parse(OriginalVideoPlayerQuality); - window.localStorage.setItem('video-quality', '{"default":"'+originalQuality.group+'"}'); + window.localStorage.setItem('video-quality', currentQualityLS); if (e.data.value != null) { OriginalVideoPlayerQuality = null; IsPlayerAutoQuality = null; @@ -381,6 +381,7 @@ twitch-videoad.js application/javascript var streamM3u8Response = await realFetch(streamM3u8Url); if (streamM3u8Response.status == 200) { var m3u8Text = await streamM3u8Response.text(); + console.log("Blocking ads..."); WasShowingAd = true; if (HideBlockingMessage == false) { postMessage({ @@ -408,6 +409,7 @@ twitch-videoad.js application/javascript } } else { if (WasShowingAd) { + console.log("Done blocking ads, changing back to original quality"); WasShowingAd = false; //Here we put player back to original quality and remove the blocking message. postMessage({ @@ -723,4 +725,4 @@ twitch-videoad.js application/javascript }; } hookFetch(); -})(); \ No newline at end of file +})(); diff --git a/vaft/vaft.user.js b/vaft/vaft.user.js index ad0e38a..32a4efa 100644 --- a/vaft/vaft.user.js +++ b/vaft/vaft.user.js @@ -1,7 +1,7 @@ // ==UserScript== // @name TwitchAdSolutions (vaft) // @namespace https://github.com/pixeltris/TwitchAdSolutions -// @version 5.3.5 +// @version 5.4.0 // @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 @@ -125,9 +125,9 @@ GQLDeviceID = e.data.value; } else if (e.data.key == 'SetHideBlockingMessage') { if (e.data.value == "true") { - HideBlockingMessage = true; - } else if (e.data.value == "false") { HideBlockingMessage = false; + } else if (e.data.value == "false") { + HideBlockingMessage = true; } } }); @@ -219,9 +219,9 @@ qualityToSelect = 0; } } + var currentQualityLS = window.localStorage.getItem('video-quality'); lowQuality[qualityToSelect].click(); - var originalQuality = JSON.parse(OriginalVideoPlayerQuality); - window.localStorage.setItem('video-quality', '{"default":"'+originalQuality.group+'"}'); + window.localStorage.setItem('video-quality', currentQualityLS); if (e.data.value != null) { OriginalVideoPlayerQuality = null; IsPlayerAutoQuality = null; @@ -392,6 +392,7 @@ var streamM3u8Response = await realFetch(streamM3u8Url); if (streamM3u8Response.status == 200) { var m3u8Text = await streamM3u8Response.text(); + console.log("Blocking ads..."); WasShowingAd = true; if (HideBlockingMessage == false) { postMessage({ @@ -419,6 +420,7 @@ } } else { if (WasShowingAd) { + console.log("Done blocking ads, changing back to original quality"); WasShowingAd = false; //Here we put player back to original quality and remove the blocking message. postMessage({ @@ -734,4 +736,4 @@ }; } hookFetch(); -})(); \ No newline at end of file +})();