Last active
November 16, 2023 21:41
-
-
Save SARDONYX-sard/ec822002dc99c82f447b505f7544616d to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// See: https://www.bugbugnow.net/2021/02/user-script.html | |
// ==UserScript== | |
// @name Youtube Block adblock blocker(preload version) | |
// @namespace http://tampermonkey.net/ | |
// @version 0.1 | |
// @description try to take over the world! | |
// @author You | |
// @match https://www.youtube.com/* | |
// @icon https://www.google.com/s2/favicons?sz=64&domain=youtube.com | |
// @grant none | |
// @run-at document-start | |
// ==/UserScript== | |
// MIT License | |
// Copyright (c) 2017 wOxxOm | |
// See: https://greasyfork.org/en/scripts/14164-kinozal-tv-remove-external-scripts/code | |
// https://github.com/Tampermonkey/tampermonkey/issues/211 | |
'use strict'; | |
(async function overwrite(link){ | |
console.info("[Youtube Block adblock blocker(preload)] rewrite 'kevlar_player_check_ad_state_on_stop'"); | |
const xhr = new XMLHttpRequest(); | |
xhr.open('GET', link.href); | |
xhr.onload = () => { | |
let mainDocument =new DOMParser().parseFromString(xhr.responseText, "text/html") | |
const scripts = mainDocument.head.getElementsByTagName("script"); | |
scripts && Array(...scripts).forEach((script)=> { | |
const newHTML = script.innerHTML.replaceAll("kevlar_player_check_ad_state_on_stop\":true","kevlar_player_check_ad_state_on_stop\":false"); | |
if (window.trustedTypes){ | |
const escapeHTMLPolicy = window.trustedTypes.createPolicy("forceInner", { | |
createHTML: (to_escape) => to_escape | |
}) | |
script.innerHTML = escapeHTMLPolicy.createHTML(newHTML); | |
}else{ | |
script.innerHTML = newHTML | |
} | |
}); | |
document.open(); | |
document.write(mainDocument.documentElement.innerHTML); | |
document.close(); | |
link.nodeName && history.pushState(0, document.title, link.href); | |
const prevUrl = location.href; | |
window.addEventListener('popstate', e => { | |
if (!prevUrl.includes('#') && !location.href.includes('#')) { | |
overwrite(location); | |
} | |
}); | |
window.addEventListener('click', e => { | |
const a = e.target.closest('a'); | |
if (a && !a.onclick && a.hostname == location.hostname) { | |
e.preventDefault(); | |
overwrite(a); | |
} | |
}); | |
}; | |
xhr.send(); | |
console.info("[Youtube Block adblock blocker(preload)] done"); | |
})(location) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** Deprecated: This is probably no longer needed as the script is for temporary measures. */ | |
// @ts-check | |
// See: https://www.bugbugnow.net/2021/02/user-script.html | |
// ==UserScript== | |
// @name Youtube Block adblock blocker | |
// @author SARDONYX-sard | |
// @description Prevent Youtube from detecting adblock and stopping playback. | |
// @downloadURL https://raw.githubusercontent.com/SARDONYX-sard/github-userscripts/main/src/youtube-block-adblock-blocker.user.js | |
// @grant none | |
// @icon https://www.google.com/s2/favicons?sz=64&domain=youtube.com | |
// @license Unlicense | |
// @match https://*.youtube.com/watch* | |
// @namespace https://github.com/SARDONYX-sard | |
// @noframes | |
// @run-at document-idle | |
// @updateURL https://raw.githubusercontent.com/SARDONYX-sard/github-userscripts/main/src/youtube-block-adblock-blocker.user.js | |
// @version 0.1.9 | |
// ==/UserScript== | |
"use strict"; | |
(() => { | |
// config | |
/** @type {"debug"|"info"|"warn"|"error"} */ | |
const DEBUG_MODE = "debug"; | |
const INTERVAL_TIME = 1000; | |
const CLEAR_INTERVAL_TIME = 10000; | |
const PLAY_BUTTON_NAME = "再生"; | |
const log = { | |
debug: (...msg) => { | |
/(?:debug)/.test(DEBUG_MODE) && console.debug(...msg); | |
}, | |
info: (...msg) => { | |
/(?:debug|info)/.test(DEBUG_MODE) && console.info(...msg); | |
}, | |
warn: (...msg) => { | |
/(?:debug|info|warn)/.test(DEBUG_MODE) && console.warn(...msg); | |
}, | |
error: (...msg) => { | |
/(?:debug|info|warn|error)/.test(DEBUG_MODE) && console.error(...msg); | |
}, | |
}; | |
const clickPlay = (/** @type {string} */ playBtnName) => { | |
/** @type {HTMLButtonElement|null} */ | |
const playBtn = document.querySelector(`button[data-title-no-tooltip="${playBtnName}"]`); | |
if (playBtn) { | |
log.info("[Youtube block blocker] auto clicked play button"); | |
playBtn.click(); | |
} else { | |
log.debug("[Youtube block blocker] Not found play button"); | |
} | |
}; | |
/** | |
* Add elements from the playlist, etc. to prevent necessary popups from not working. | |
*/ | |
const addEmptyPopup = () => { | |
const popup = document.createElement("ytd-popup-container"); | |
popup.classList.add("style-scope"); | |
popup.classList.add("ytd-app"); | |
popup.innerHTML = "<!--css-build:shady--><!--css-build:shady-->"; | |
const contentNext = document.getElementById("content")?.nextSibling; | |
if (!contentNext) { | |
log.debug("[Youtube block blocker] Not found content id nextSibling."); | |
return; | |
} | |
document.querySelector("body > ytd-app")?.insertBefore(popup, null); | |
}; | |
const removePopup = () => { | |
const popup = document.querySelector("body > ytd-app > ytd-popup-container"); | |
popup ? popup.remove() : log.debug("[Youtube block blocker] Not found popup."); | |
const scrollBlock = document.querySelector("body > tp-yt-iron-overlay-backdrop"); | |
scrollBlock ? scrollBlock.remove() : log.debug("[Youtube block blocker] Not found overlay."); | |
}; | |
const blockBlocker = () => { | |
log.info("[Youtube block blocker] Start to remove popup & click play button."); | |
const intervalId = setInterval(() => { | |
removePopup(); | |
clickPlay(PLAY_BUTTON_NAME); | |
}, INTERVAL_TIME); | |
setTimeout(() => { | |
clearInterval(intervalId); | |
log.info("[Youtube block blocker] Complete to click play button."); | |
addEmptyPopup(); | |
}, CLEAR_INTERVAL_TIME); | |
}; | |
blockBlocker(); | |
let oldHref = document.location.href; | |
const observer = new MutationObserver(async (_mutations, observer) => { | |
log.info("[Youtube block blocker] Script called."); | |
if (oldHref != document.location.href) { | |
oldHref = document.location.href; | |
blockBlocker(); | |
} | |
}); | |
observer.observe(document, { childList: true, subtree: true }); | |
})(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment