[Solved] Any way to exclude iframes on certain domains only?

redigerte October 2017 i Development Chrome
Any TamperMonkey setting would be fine. Basically I have an autoplay script for a streaming site who's players are embedded everywhere. For the most part, I want them to autoplay, but on certain domains it's annoying. I haven't figured out how to exclude iframes on a per-domain basis, but I'm hoping it's possible.

Kommentarer

  • Try document.referrer:

    var excludeDomains = [
      'foo.com',
      'bar.net',
    ];
    
    if (excludeDomains.some(d => new URL(document.referrer).hostname.includes(d))) {
      // skip
    }
    

    or use a regexp:

    if (/foo|bar/.test(document.referrer)) {
      // skip
    }
    
  • redigerte October 2017 Chrome
    Hey wOxxOm. Looks promising, but I'm getting nowhere slowly. The script is just a simple auto-click on a couple elements.
    // ==UserScript==
    // @name Openload auto-play
    // @version 0.1
    // @description auto-click
    // @include /https?://(www\.)?o(pen)?load\.(com?|net|tv|stream).*/*/
    // @grant none
    // ==/UserScript==

    $("#videooverlay,.vjs-poster").click();
    Example of a domain I'd like to exclude.
  • redigerte October 2017 Chrome
    (() => {
      if ([
        'www.alluc.ee',
      ].some(d => new URL(document.referrer).hostname.includes(d))) {
        return;
      }
    
      $("#videooverlay, .vjs-poster").click();
    })();
    

    The code is inside an IIFE to isolate variables from the page. There are no variables right now, but generally this is the safe approach for @grant none userscripts.

  • Doesn't seem to exclude iframes on alluc.ee, unless I somehow managed to mess up a copypasta.
  • redigerte October 2017 Chrome

    It excludes on www.alluc.ee, not alluc.ee.
    Anyway, the problem may be caused by an intermediate iframe that has a different URL.
    Add console.log(document.referrer) and see what it prints.

  • Yeah, www.alluc.ee. Basically, it doesn't work on the example URL. Just the one iframe I'm seeing. console.log(document.referrer) shows me nothing in the console, so I'm probably doing it wrong.
  • redigerte October 2017 PaleMoon

    window.top.location.hostname will give you the hostname of the domain hosting the iframe. So something like this if I am understanding correctly what you are asking.

    <br />let excludedDomains = ["greasyfork.org", "alluc.ee", "etc..."];
    
    if ( excludedDomains.includes(window.top.location.hostname.replace("www.", "")) )
    {
        return;
    }
    
    
  • redigerte October 2017 Chrome

    window.top.location isn't accessible in cross-origin iframes.

    console.log(document.referrer) should be outside of condition check, simply put it immediately after the header.

  • wOxxOm sa:

    simply put it immediately after the header.


    I've tried putting it everywhere, including directly under the header, and I see exactly jack shit about it in the console. Only thing I know is that it doesn't exclude the script from running in iframes on specified domains.
  • redigerte October 2017 Chrome

    I see http://www.alluc.ee/l/cocain-war-for-the-planet-of-the-apes-2017-bdrip-x264-Esub-mp4/e8jnl74n if I add console.log. And the script in my second post (4th overall) works correctly: it doesn't invoke click on that page. If you wanted to exclude the userscript from Tampermonkey menu then there's no way.

  • I don't care about it showing in the popup, only that it successfully excludes. I think I see the issue now. (4th overall) is the one I've been using, but now that you said it's working, I was scratching my head. Seems like the exclusion fails when opening the link above in a new tab. If I reload the page, it excludes properly. You get the same failure if opened in a new tab?
  • redigerte October 2017 Chrome

    Ugh, that's a really weird player. Apparently, it rewrites its own iframe so the userscript runs twice, and the 2nd run doesn't have a real referrer URL. Here's a hack that seems to work:

    // ==UserScript==
    // @name         Openload auto-play
    // @version      0.1
    // @description  auto-click
    // @include      /https?://(www\.)?o(pen)?load\.(com?|net|tv|stream).*/*/
    // @grant        none
    // ==/UserScript==
    
    (() => {
      const parentURL = new URL(document.referrer).hostname == 'openload.co' ?
        localStorage[GM_info.script.name] :
        document.referrer;
      if (!parentURL) {
        localStorage[GM_info.script.name] = document.referrer;
        return;
      }
      delete localStorage[GM_info.script.name];
    
      const parentDomain = new URL(parentURL).hostname;
      if ([
        'www.alluc.ee',
      ].some(d => parentDomain.includes(d))) {
        return;
      }
    
      $("#videooverlay, .vjs-poster").click();
    
    })();
    
  • Yup. Appears to do the trick. Thanks wOxxOm!

    image
  • Goddamnit, I spoke too soon. It now has the opposite issue, where it randomly fails to apply in iframes where it shouldn't be excluded. I suppose this also has to do with "rewriting its own iframe". The host is a POS PITA when it comes to anti-adblock and forcing their malware popups, so I imagine the weird loading has something to do with that.

    I suspect that either version of the document.referrer script would work otherwise, so I'll leave it marked as solved. Not sure that the objective here warrants any further aggravation, but if you have any suggestions, I'd be curious to test them.
  • Okay, here's the heavy-duty version adapted from my other script. It runs on all urls and installs a message listener in the main page instance that responds to an iframe instance with the actual page url.

    // ==UserScript==
    // @name         Openload auto-play
    // @version      0.1
    // @description  auto-click
    // @include      *
    // @grant        none
    // ==/UserScript==
    
    (() => {
      function main() {
        $("#videooverlay, .vjs-poster").click();
      }
    
      function allowedIframeURL() {
        return /https?:\/\/(www\.)?o(pen)?load\.(com?|net|tv|stream)/.test(location.href);
      }
    
      function allowedTopURL(url) {
        return ![
          'www.alluc.ee',
        ].some(d => new URL(url).hostname.includes(d));
      }
    
      if (window == top) {
        // main page responder
        window.addEventListener('message', e => {
          if (e.source != top && e.data == GM_info.script.name) {
            e.source.postMessage(GM_info.script.name + '\n' + location.href, '*');
          }
        });
        return;
      } else if (!allowedIframeURL()) {
        return;
      }
    
      // request main page's URL
      window.addEventListener('message', e => {
        if (e.source != top || typeof e.data != 'string' || !e.data.startsWith(GM_info.script.name))
          return;
        if (!allowedTopURL(e.data.split('\n')[1]))
          return;
        main();
      }, {once: true});
      top.postMessage(GM_info.script.name, '*');
    })();
    
  • A Rube Goldberg userscript! Perhaps the most elaborate auto-click code ever written? Seems solid where the other had issues. I will put this to good use, so I appreciate the extra effort.
Logg inn eller Registrer deg for å kommentere.