Advertisement
Guest User

tampermonkey adblock

a guest
Jan 15th, 2024
58
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 12.85 KB | None | 0 0
  1. // ==UserScript==
  2. // @name Remove Adblock Thing
  3. // @namespace http://tampermonkey.net/
  4. // @version 3.4
  5. // @description Removes Adblock Thing
  6. // @author JoelMatic
  7. // @match https://www.youtube.com/*
  8. // @icon https://www.google.com/s2/favicons?sz=64&domain=youtube.com
  9. // @updateURL https://github.com/TheRealJoelmatic/RemoveAdblockThing/raw/main/Youtube-Ad-blocker-Reminder-Remover.user.js
  10. // @downloadURL https://github.com/TheRealJoelmatic/RemoveAdblockThing/raw/main/Youtube-Ad-blocker-Reminder-Remover.user.js
  11. // @grant none
  12. // ==/UserScript==
  13.  
  14. (function()
  15. {
  16. //
  17. // Config
  18. //
  19.  
  20. // Enable The Undetected Adblocker
  21. const adblocker = true;
  22.  
  23. // Enable The Popup remover (pointless if you have the Undetected Adblocker)
  24. const removePopup = false;
  25.  
  26. // Checks for updates (Removes the popup)
  27. const updateCheck = true;
  28.  
  29. // Enable debug messages into the console
  30. const debugMessages = true;
  31.  
  32. //
  33. // CODE
  34. //
  35. // If you have any suggestions, bug reports,
  36. // or want to contribute to this userscript,
  37. // feel free to create issues or pull requests in the GitHub repository.
  38. //
  39. // GITHUB: https://github.com/TheRealJoelmatic/RemoveAdblockThing
  40.  
  41.  
  42. //
  43. // Varables used for the Popup Remover
  44. //
  45. const keyEvent = new KeyboardEvent("keydown", {
  46. key: "k",
  47. code: "KeyK",
  48. keyCode: 75,
  49. which: 75,
  50. bubbles: true,
  51. cancelable: true,
  52. view: window
  53. });
  54.  
  55. let mouseEvent = new MouseEvent("click", {
  56. bubbles: true,
  57. cancelable: true,
  58. view: window,
  59. });
  60.  
  61. //This is used to check if the video has been unpaused already
  62. let unpausedAfterSkip = 0;
  63.  
  64. //
  65. // Varables used for adblock
  66. //
  67.  
  68. // Store the initial URL
  69. let currentUrl = window.location.href;
  70.  
  71. // Used for if there is ad found
  72. let isAdFound = false;
  73.  
  74. //used to see how meny times we have loopped with a ad active
  75. let adLoop = 0;
  76.  
  77. //
  78. // Varables used for updater
  79. //
  80.  
  81. let hasIgnoredUpdate = false;
  82.  
  83. //
  84. // Setup
  85. //
  86.  
  87. //Set everything up here
  88. if (debugMessages) console.log("Remove Adblock Thing: Script started ");
  89.  
  90. if (adblocker) removeAds();
  91. if (removePopup) popupRemover();
  92. if (updateCheck) checkForUpdate();
  93.  
  94. // Remove Them pesski popups
  95. function popupRemover() {
  96. setInterval(() => {
  97.  
  98. const fullScreenButton = document.querySelector(".ytp-fullscreen-button");
  99. const modalOverlay = document.querySelector("tp-yt-iron-overlay-backdrop");
  100. const popup = document.querySelector(".style-scope ytd-enforcement-message-view-model");
  101. const popupButton = document.getElementById("dismiss-button");
  102.  
  103. const video1 = document.querySelector("#movie_player > video.html5-main-video");
  104. const video2 = document.querySelector("#movie_player > .html5-video-container > video");
  105.  
  106. const bodyStyle = document.body.style;
  107.  
  108. bodyStyle.setProperty('overflow-y', 'auto', 'important');
  109.  
  110. if (modalOverlay) {
  111. modalOverlay.removeAttribute("opened");
  112. modalOverlay.remove();
  113. }
  114.  
  115. if (popup) {
  116. if (debugMessages) console.log("Remove Adblock Thing: Popup detected, removing...");
  117.  
  118. if(popupButton) popupButton.click();
  119.  
  120. popup.remove();
  121. unpausedAfterSkip = 2;
  122.  
  123. fullScreenButton.dispatchEvent(mouseEvent);
  124.  
  125. setTimeout(() => {
  126. fullScreenButton.dispatchEvent(mouseEvent);
  127. }, 500);
  128.  
  129. if (debugMessages) console.log("Remove Adblock Thing: Popup removed");
  130. }
  131.  
  132. // Check if the video is paused after removing the popup
  133. if (!unpausedAfterSkip > 0) return;
  134.  
  135. // UnPause The Video
  136. unPauseVideo(video1);
  137. unPauseVideo(video2);
  138.  
  139. }, 1000);
  140. }
  141. // undetected adblocker method
  142. function removeAds()
  143. {
  144. if (debugMessages) console.log("Remove Adblock Thing: removeAds()");
  145.  
  146. setInterval(() =>{
  147.  
  148. var videoPlayback;
  149.  
  150. var video = document.querySelector('video');
  151. if(videoPlayback) videoPlayback = video.playbackRate;
  152. const ad = [...document.querySelectorAll('.ad-showing')][0];
  153.  
  154.  
  155. //remove page ads
  156. if (window.location.href !== currentUrl) {
  157. currentUrl = window.location.href;
  158. removePageAds();
  159. }
  160.  
  161. if (ad)
  162. {
  163. isAdFound = true;
  164. adLoop = adLoop + 1;
  165.  
  166. //
  167. // ad center method
  168. //
  169.  
  170. // If we tryied 10 times we can assume it wont work this time (This stops the wird pause/feaze on the ads)
  171.  
  172. if(adLoop < 10){
  173. const openAdCenterButton = document.querySelector('.ytp-ad-button-icon');
  174. openAdCenterButton?.click();
  175.  
  176. var popupContainer = document.querySelector('body > ytd-app > ytd-popup-container > tp-yt-paper-dialog');
  177. const hidebackdrop = document.querySelector("body > tp-yt-iron-overlay-backdrop");
  178.  
  179. if (popupContainer) popupContainer.style.display = 'none';
  180. if (hidebackdrop) hidebackdrop.style.display = 'none';
  181.  
  182. const blockAdButton = document.querySelector('[label="Block ad"]');
  183. blockAdButton?.click();
  184.  
  185. const blockAdButtonConfirm = document.querySelector('.Eddif [label="CONTINUE"] button');
  186. blockAdButtonConfirm?.click();
  187.  
  188. const closeAdCenterButton = document.querySelector('.zBmRhe-Bz112c');
  189. closeAdCenterButton?.click();
  190. }
  191. else{
  192. if (video) video.play();
  193. }
  194.  
  195. //
  196. // Speed Skip Method
  197. //
  198. if (debugMessages) console.log("Remove Adblock Thing: Found Ad");
  199.  
  200.  
  201. const skipButtons = ['ytp-ad-skip-button-container', 'ytp-ad-skip-button-modern', '.videoAdUiSkipButton', '.ytp-ad-skip-button', '.ytp-ad-skip-button-modern', '.ytp-ad-skip-button' ];
  202.  
  203. // Add a little bit of obfuscation when skipping to the end of the video.
  204. if (video){
  205.  
  206. video.playbackRate = 10;
  207. video.volume = 0;
  208.  
  209. // Iterate through the array of selectors
  210. skipButtons.forEach(selector => {
  211. // Select all elements matching the current selector
  212. const elements = document.querySelectorAll(selector);
  213.  
  214. // Check if any elements were found
  215. if (elements.length > 0 && elements) {
  216. // Iterate through the selected elements and click
  217. elements.forEach(element => {
  218. element?.click();
  219. });
  220. }
  221. });
  222. video.play();
  223.  
  224. let randomNumber = Math.random() * (0.5 - 0.1) + 0.1;
  225. video.currentTime = video.duration + randomNumber || 0;
  226. }
  227.  
  228. if (debugMessages) console.log("Remove Adblock Thing: skipped Ad (✔️)");
  229.  
  230. } else {
  231.  
  232. //check for unreasonale playback speed
  233. if(video?.playbackRate == 10 && video){
  234. video.playbackRate = videoPlayback;
  235. }
  236.  
  237. if (isAdFound){
  238. isAdFound = false;
  239.  
  240. // this is right after the ad is skipped
  241. // fixes if you set the speed to 2x annd a ad plays it sets it back to the dfualt 1x
  242.  
  243. //somthing bugged out default to 1x then
  244. if (videoPlayback == 10){
  245. videoPlayback = 1;
  246.  
  247. var _opupContainer = document.querySelector('body > ytd-app > ytd-popup-container > tp-yt-paper-dialog');
  248. const _idebackdrop = document.querySelector("body > tp-yt-iron-overlay-backdrop");
  249.  
  250. if (_opupContainer) _opupContainer.style.display = "block";
  251. if (_idebackdrop) _idebackdrop.style.display = "block";
  252. }
  253.  
  254. if(video) video.playbackRate = videoPlayback;
  255.  
  256. //set ad loop back to the defualt
  257. adLoop = 0;
  258. }
  259. else{
  260. if(video) videoPlayback = video.playbackRate;
  261. }
  262. }
  263.  
  264. }, 50)
  265.  
  266. removePageAds();
  267. }
  268.  
  269. //removes ads on the page (not video player ads)
  270. function removePageAds(){
  271.  
  272. const sponsor = document.querySelectorAll("div#player-ads.style-scope.ytd-watch-flexy, div#panels.style-scope.ytd-watch-flexy");
  273. const style = document.createElement('style');
  274.  
  275. style.textContent = `
  276. ytd-action-companion-ad-renderer,
  277. ytd-display-ad-renderer,
  278. ytd-video-masthead-ad-advertiser-info-renderer,
  279. ytd-video-masthead-ad-primary-video-renderer,
  280. ytd-in-feed-ad-layout-renderer,
  281. ytd-ad-slot-renderer,
  282. yt-about-this-ad-renderer,
  283. yt-mealbar-promo-renderer,
  284. ad-slot-renderer,
  285. .ytd-video-masthead-ad-v3-renderer,
  286. div#root.style-scope.ytd-display-ad-renderer.yt-simple-endpoint,
  287. div#sparkles-container.style-scope.ytd-promoted-sparkles-web-renderer,
  288. div#main-container.style-scope.ytd-promoted-video-renderer,
  289. div#player-ads.style-scope.ytd-watch-flexy,
  290.  
  291. #masthead-ad {
  292. display: none !important;
  293. }
  294. `;
  295.  
  296. document.head.appendChild(style);
  297.  
  298. sponsor?.forEach((element) => {
  299. if (element.getAttribute("id") === "rendering-content") {
  300. element.childNodes?.forEach((childElement) => {
  301. if (childElement?.data.targetId && childElement?.data.targetId !=="engagement-panel-macro-markers-description-chapters"){
  302. //Skipping the Chapters section
  303. element.style.display = 'none';
  304. }
  305. });
  306. }
  307. });
  308.  
  309. if (debugMessages) console.log("Remove Adblock Thing: Removed page ads (✔️)");
  310. }
  311.  
  312. // Unpause the video Works most of the time
  313. function unPauseVideo(video)
  314. {
  315. if (!video) return;
  316. if (video.paused) {
  317. // Simulate pressing the "k" key to unpause the video
  318. document.dispatchEvent(keyEvent);
  319. unpausedAfterSkip = 0;
  320. if (debugMessages) console.log("Remove Adblock Thing: Unpaused video using 'k' key");
  321. } else if (unpausedAfterSkip > 0) unpausedAfterSkip--;
  322. }
  323.  
  324. //
  325. // Update check
  326. //
  327.  
  328. function checkForUpdate(){
  329.  
  330. if (!(window.location.href.includes("youtube.com"))){
  331. return;
  332. }
  333.  
  334. if (hasIgnoredUpdate){
  335. return;
  336. }
  337.  
  338. const scriptUrl = 'https://raw.githubusercontent.com/TheRealJoelmatic/RemoveAdblockThing/main/Youtube-Ad-blocker-Reminder-Remover.user.js';
  339.  
  340. fetch(scriptUrl)
  341. .then(response => response.text())
  342. .then(data => {
  343. // Extract version from the script on GitHub
  344. const match = data.match(/@version\s+(\d+\.\d+)/);
  345. if (match) {
  346. const githubVersion = parseFloat(match[1]);
  347. const currentVersion = parseFloat(GM_info.script.version);
  348.  
  349. if (githubVersion > currentVersion) {
  350. console.log('Remove Adblock Thing: A new version is available. Please update your script.');
  351.  
  352. var result = window.confirm("Remove Adblock Thing: A new version is available. Please update your script.");
  353.  
  354. if (result) {
  355. window.location.replace(scriptUrl);
  356. }
  357.  
  358. } else {
  359. console.log('Remove Adblock Thing: You have the latest version of the script.');
  360. }
  361. } else {
  362. console.error('Remove Adblock Thing: Unable to extract version from the GitHub script.');
  363. }
  364. })
  365. .catch(error => {
  366. hasIgnoredUpdate = true;
  367. console.error('Remove Adblock Thing: Error checking for updates:', error);
  368. });
  369. hasIgnoredUpdate = true;
  370. }
  371. })();
  372.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement