Slightly improve ad detection speed for mute-black / dyn-video-swap

This commit is contained in:
pixeltris
2020-12-22 20:13:01 +00:00
parent f5bbf0b893
commit 26c88c15f5
2 changed files with 104 additions and 90 deletions
+29 -22
View File
@@ -2,11 +2,35 @@
twitch-videoad.js application/javascript
(function() {
if ( /(^|\.)twitch\.tv$/.test(document.location.hostname) === false ) { return; }
var disabledVideo = null;
var originalVolume = 0;
var originalAppendChild = Element.prototype.appendChild;
Element.prototype.appendChild = function() {
originalAppendChild.apply(this, arguments);
if (arguments[0] && arguments[0].innerHTML && arguments[0].innerHTML.includes('tw-c-text-overlay') && arguments[0].innerHTML.includes('ad-banner')) {
onFoundAd();
}
};
function onFoundAd() {
if (!disabledVideo) {
//get livestream video element
var liveVid = document.getElementsByTagName("video");
if (liveVid.length) {
disabledVideo = liveVid = liveVid[0];
//mute
originalVolume = liveVid.volume;
liveVid.volume = 0;
//black out
liveVid.style.filter = "brightness(0%)";
//hide ad contianers
var adContainers = document.querySelectorAll('[data-test-selector="sad-overlay"]');
for (var i = 0; i < adContainers.length; i++) {
adContainers[i].style.display = "none";
}
}
}
}
window.addEventListener("DOMContentLoaded", function() {
//current state of if video is disabled
var disabledVideo = null;
var originalVolume = 0;
//repeatedly check for ad
function checkForAd() {
//check ad by looking for text banner
var adBanner = document.querySelectorAll("span.tw-c-text-overlay");
@@ -18,24 +42,7 @@ twitch-videoad.js application/javascript
}
}
if (foundAd) {
//if found ad and video is visible, black out video and mute
if (!disabledVideo) {
//get livestream video element
var liveVid = document.getElementsByTagName("video");
if (liveVid.length) {
disabledVideo = liveVid = liveVid[0];
//mute
originalVolume = liveVid.volume;
liveVid.volume = 0;
//black out
liveVid.style.filter = "brightness(0%)";
//hide ad contianers
var adContainers = document.querySelectorAll('[data-test-selector="sad-overlay"]');
for (var i = 0; i < adContainers.length; i++) {
adContainers[i].style.display = "none";
}
}
}
onFoundAd();
} else {
//if no ad and video blacked out, unmute and disable black out
if (disabledVideo) {