Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Lysak/3208170bc20b75935b363beb74658f32 to your computer and use it in GitHub Desktop.
Save Lysak/3208170bc20b75935b363beb74658f32 to your computer and use it in GitHub Desktop.
Automatically like comments at studio.youtube.com
// ==UserScript==
// @name Automatically like comments at studio.youtube.com
// @namespace https://github.com/Lysak
// @version 0.2
// @author Dmytrii Lysak @Lysak
// @match https://studio.youtube.com/*
// @require https://code.jquery.com/jquery-3.6.3.min.js
// @grant none
// @run-at document-end
// @description Automatically like comments at studio.youtube.com (Code License: MIT License)
// @icon https://www.google.com/s2/favicons?sz=64&domain=youtube.com
// ==/UserScript==
// Revision history
// 0.1 Initial implementation
// 0.2 Final release
// https://www.youtube.com/watch?v=Nrf4xy9mj74
// https://studio.youtube.com/channel/UCs6yMje_JBxAdrpQRviKoaA/comments/inbox?filter=%5B%5D
// ctrl+shift+k - comment line of code
/* globals jQuery, $, waitForKeyElements */
(function() {
'use strict';
//console.log('init');
const readySelector = '#items #creator-heart-button #button yt-icon';
function like() {
setTimeout(function() {
console.log('like');
const likeList = document.querySelectorAll(readySelector);
let position = 0;
likeList.forEach(function(currentValue, index, array) {
if (currentValue?.id === 'unhearted' && currentValue.style.display !== 'none') {
setTimeout(function() {
console.log(currentValue)
currentValue.click();
console.log(`id: ${index} liked`)
}, 1000);
}
const element = $("#items > div.ytcp-comment-thread.ytcp-comments-section").eq(index);
if (element.length > 0) {
const transformValue = element.css("transform");
const yValue = getYTranslateValue(transformValue);
//console.log(yValue);
//console.log(`id: ${index} found`)
if (yValue > position) {
position = yValue;
element[0].scrollIntoView({
behavior: "smooth", // or "auto" or "instant"
block: "start" // or "end"
});
console.log(position, 'pos');
console.log(element);
}
} else {
//console.log(`id: ${index} not found`)
}
});
}, 1000);
}
// Convenience function to execute your callback only after an element matching readySelector has been added to the page.
// Example: runWhenReady('.search-result', augmentSearchResults);
// Gives up after 1 minute.
// https://github.com/Tampermonkey/tampermonkey/issues/1279#issuecomment-875386821
function runWhenReady(readySelector, callback) {
var numAttempts = 0;
var tryNow = function() {
var elem = document.querySelector(readySelector);
if (elem) {
callback(elem);
} else {
numAttempts++;
if (numAttempts >= 34) {
console.warn('Giving up after 34 attempts. Could not find: ' + readySelector);
} else {
setTimeout(tryNow, 250 * Math.pow(1.1, numAttempts));
}
}
};
tryNow();
}
function getYTranslateValue(transformString) {
const match = transformString.match(/-?\d+/g);
let value = 0;
if (match && match.length) {
const number = parseInt(match[match.length - 1]);
if (number > 0) {
value = number;
}
}
return value;
}
runWhenReady(readySelector, function() {
setInterval(like, 2000);
});
})();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment