Youtube: 觀看5分鐘影片後自動按喜歡
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// ==UserScript== | |
// @name Youtube: Automatically press like after watching 5 minutes of video | |
// @name:zh Youtube: 觀看5分鐘影片後自動按喜歡 | |
// @version 1.1.2 | |
// @description Youtube: Automatically press like after watching 5 minutes of video | |
// @author 琳(jim60105) | |
// @match https://www.youtube.com/* | |
// @icon https://www.google.com/s2/favicons?sz=64&domain=youtube.com | |
// ==/UserScript== | |
(function () { | |
'use strict'; | |
/** | |
* @type {number} waitForSeconds - The number of seconds to wait before pressing the button. | |
*/ | |
const waitForSeconds = 5 * 60; | |
/////////////////////////// DO NOT TOUCH ANYTHING BELOW THIS LINE /////////////////////////// | |
let timeout; | |
window.addEventListener('load', function () { | |
// Youtube doesn't refresh the page when you switch videos within the site. | |
DoWhenUrlChange(function () { | |
if (!CheckUrl()) return; | |
WaitAndPressTheButton(); | |
}); | |
}); | |
function DoWhenUrlChange(func) { | |
// Youtube refreshes this element when you click on a link between homepage and video page. | |
const headerElement = document.getElementById('masthead'); | |
// Youtube refreshes this element when you click on a link and jump BETWEEN video pages. | |
let videoElement; | |
// Init with empty string so that the first time the page loads, the function will be executed. | |
let previousUrl = ''; | |
const config = { attributes: true, childList: false, characterData: false }; | |
const observer = new MutationObserver(function (mutations) { | |
if (location.href !== previousUrl) { | |
previousUrl = location.href; | |
// console.log(`URL changed to ${location.href}`); | |
func(); | |
// The video element is not available when entering from the home page. | |
if (!videoElement) { | |
videoElement = document.getElementsByTagName('video')[0]; | |
if (videoElement) { | |
observer.observe(videoElement, config); | |
} | |
} | |
} | |
}); | |
if (headerElement) observer.observe(headerElement, config); | |
} | |
function CheckUrl() { | |
return window.location.pathname.match('^/watch'); | |
} | |
function WaitAndPressTheButton() { | |
console.log(`Automatically press like button after waiting ${waitForSeconds} seconds...`); | |
clearTimeout(timeout); | |
timeout = setTimeout(function () { | |
const button = document | |
.getElementById('segmented-like-button') | |
.getElementsByTagName('button')[0]; | |
if (button.getAttribute('aria-pressed') === 'true') return; | |
button.click(); | |
console.log('Automatically liked this video!'); | |
}, waitForSeconds * 1000); | |
} | |
})(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment