Shadowrocket_diy_rules icon indicating copy to clipboard operation
Shadowrocket_diy_rules copied to clipboard

Ads appear on subsequent videos

Open prohoudini opened this issue 1 year ago • 0 comments

As you might have noted yourself, the ad block script works on the first video but fails in subsequent videos.

To address the issue of ads appearing in subsequent YouTube videos even after the initial video's ads are blocked, we'll need to ensure that the ad-blocking code gets reapplied every time a new video loads. This behavior suggests that YouTube might be loading new content dynamically without a full page reload (a common practice in single-page applications, or SPAs), which is why your current setup only blocks ads on the first video.

Given that the script you're using modifies network requests to intercept and alter YouTube responses, it appears to be designed for use with a proxy tool, which might not inherently support detecting when YouTube dynamically loads a new video. Thus, we would need to rely on a method to trigger your blocking logic upon these dynamic changes.

Modification Approach:

JavaScript Modification: Since JavaScript running in the browser has the ability to listen to content changes within the page, you would need to modify the JavaScript file to add a MutationObserver. This observer will watch for changes in the DOM that indicate a new video is being loaded.

Event Listener: An event listener in the script will react to changes such as clicking on a new video or auto-playing to the next video.

Regular Checks: Setting up an interval to periodically check and apply the ad-blocking logic can be a fallback if mutation observers and event listeners don't catch all changes.

Here’s a basic outline of how you might set up a MutationObserver in the JavaScript file to monitor for changes and reapply the ad-blocking logic:

// Assuming all variable and function definitions are done here...

(() => {
  // Place the mutation observer setup here
  const targetNode = document.querySelector('body'); // Adjust the target as needed
  const config = { childList: true, subtree: true };

  const callback = function(mutationsList, observer) {
      for(const mutation of mutationsList) {
          if (mutation.type === 'childList') {
              // Check if the new nodes are video ads and block them
              console.log('A child node has been added or removed.');
          }
      }
  };

  const observer = new MutationObserver(callback);
  observer.observe(targetNode, config);

  // Rest of your script where the main functionality happens...
  var Jt = Object.defineProperty;
  var Gt = (t, e, n) =>
    e in t ? Jt(t, e, { enumerable: !0, configurable: !0, writable: !0, value: n }) : (t[e] = n);
  var K = (t, e, n) => (Gt(t, typeof e != "symbol" ? e + "" : e, n), n);

  // Your function definitions...

  // Existing script functionality continues...
})();

prohoudini avatar May 04 '24 18:05 prohoudini