Dyskusje » Tworzenie skryptów

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

§
Napisano: 21-10-2017
Edytowano: 23-10-2017

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

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.

woxxomMod
§
Napisano: 22-10-2017

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
}
§
Napisano: 22-10-2017
Edytowano: 22-10-2017

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.

woxxomMod
§
Napisano: 22-10-2017
Edytowano: 22-10-2017
(() => {
  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.

§
Napisano: 22-10-2017

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

woxxomMod
§
Napisano: 22-10-2017
Edytowano: 22-10-2017

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.

§
Napisano: 22-10-2017

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.

§
Napisano: 22-10-2017
Edytowano: 22-10-2017

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.


let excludedDomains = ["greasyfork.org", "alluc.ee", "etc..."];

if ( excludedDomains.includes(window.top.location.hostname.replace("www.", "")) )
{
    return;
}

woxxomMod
§
Napisano: 22-10-2017
Edytowano: 22-10-2017

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.

§
Napisano: 23-10-2017
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.

woxxomMod
§
Napisano: 23-10-2017
Edytowano: 23-10-2017

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.

§
Napisano: 23-10-2017

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?

woxxomMod
§
Napisano: 23-10-2017
Edytowano: 23-10-2017

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();

})();
§
Napisano: 23-10-2017

Yup. Appears to do the trick. Thanks wOxxOm!

image

§
Napisano: 23-10-2017

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.

woxxomMod
§
Napisano: 24-10-2017

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, '*');
})();
§
Napisano: 24-10-2017

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.

Odpowiedz

Zaloguj się, by odpowiedzieć.