0

I have this Greasemonkey script which autoplays embedded Youtube videos.

// ==UserScript==
// @name        Autoplay YT
// @namespace   78
// @include     https://www.youtube.com/embed/*
// @version     1
// @grant       none
// ==/UserScript==

var intv = setInterval(function() {

var btn = document.querySelector('button.ytp-play-button');
  if( typeof btn.getAttribute("title") == 'undefined' || (btn.getAttribute("title").trim() != 'Play (k)' && btn.getAttribute("title").trim() != 'Replay') ) return;
  btn.click()

        }, 1000);

Problem is it autoplays the videos wherever it finds them. I only want it to execute on 1 URL, for example:

https://stackoverflow.com/page/*

This doesn't work at all, the video doesn't play:

// ==UserScript==
// @name        Autoplay YT
// @namespace   78
// @include     https://stackoverflow.com/page/*
// @version     1
// @grant       none
// ==/UserScript==

var intv = setInterval(function() {

var btn = document.querySelector('button.ytp-play-button');
  if( typeof btn.getAttribute("title") == 'undefined' || (btn.getAttribute("title").trim() != 'Play (k)' && btn.getAttribute("title").trim() != 'Replay') ) return;
  btn.click()

        }, 1000);

This works, but again the videos play on any URL:

// ==UserScript==
// @name        Autoplay YT
// @namespace   78
// @include     https://stackoverflow.com/page/*
// @include     https://www.youtube.com/embed/*
// @version     1
// @grant       none
// ==/UserScript==

var intv = setInterval(function() {

var btn = document.querySelector('button.ytp-play-button');
  if( typeof btn.getAttribute("title") == 'undefined' || (btn.getAttribute("title").trim() != 'Play (k)' && btn.getAttribute("title").trim() != 'Replay') ) return;
  btn.click()

        }, 1000);

The video loads in an iFrame, so my script doesn't seem to care about the parent URL, it just picks up the URL from the iFrame and executes. But I would like the script to execute only if the Parent URL and the Include URL are both true.

Scorpys
  • 3
  • 3

0 Answers0