I am trying to automatically click the "Still watching? Video Will Pause Soon" button on YouTube so that my videos don't stop after X amount of time. I have created a Tampermonkey/Greasemonkey script to click the button when it appears using waitForKeyElements. It seemed to work, as it clicks the popup. But sometimes it goes into an endless loop where it clicks the popup button and then refreshes the page. The button will pop up again after the refresh and then the button will get clicked again and repeat ad infinitum.
I have provided a screenshot of what I'm talking about. Notice the YouTube popup in the bottom left corner:
Here's the code for the Tampermonkey script:
// ==UserScript==
// @name YouTube Yes 4
// @include https://www.youtube.com/*
// @require http://ajax.googleapis.com/ajax/libs/jquery/1.8.3/jquery.min.js
// @require https://gist.github.com/raw/2625891/waitForKeyElements.js
// @grant GM_addStyle
// ==/UserScript==
waitForKeyElements (
".yt-button-renderer", click
);
function click (TargetLink) {
TargetLink = $("a:contains('Yes')")
if (TargetLink.length)
window.location.href = TargetLink[0].href
}
Edit: Here is an updated script using .click()
waitForKeyElements (
"tp-yt-paper-button#button.style-scope.yt-button-renderer.style-blue-text.size-default", clicks
);
function clicks () {
$("tp-yt-paper-button#button.style-scope.yt-button-renderer.style-blue-text.size-default:contains('Yes')").click();
}
Any and all help would be deeply appreciated. Happy holidays!
With the help of another Stack Overflow thread and the comments here, I was able to get a solution using a selector with contains() and the word "Yes," which appears in the popup button:
// ==UserScript==
// @name YouTube Yes
// @include https://www.youtube.com/*
// @require http://ajax.googleapis.com/ajax/libs/jquery/1.8.3/jquery.min.js
// @require https://gist.github.com/raw/2625891/waitForKeyElements.js
// @grant GM_addStyle
// ==/UserScript==
waitForKeyElements ("a.yt-simple-endpoint.style-scope.yt-button-renderer:contains('Yes')", clicks);
function clicks (jNode) {
var clickEvent = document.createEvent ('MouseEvents');
clickEvent.initEvent ('click', true, true);
jNode[0].dispatchEvent (clickEvent);
}