Created
December 8, 2022 07:19
-
-
Save harry-private/339d1d3d74844f12a443ed2bc23eb8d3 to your computer and use it in GitHub Desktop.
Simple user script for getting channel name
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 2 Open youtube channels on Social Blade | |
// @namespace Violentmonkey Scripts | |
// @match https://*.youtube.com/* | |
// @grant none | |
// @version 1.0 | |
// @author - | |
// @description This script will open the Youtube channel in the Social Balde | |
// ==/UserScript== | |
// note: this is just a basic script and doesn't consider all the scenarios. | |
const targetNode = document.body || document.documentElement; | |
let oldUrl; | |
let oldChannelName; | |
let maxRetry = 3; | |
let retryCount = 0; | |
const config = { | |
attributes: true, | |
childList: true, | |
subtree: true | |
}; | |
const callback = async (mutationList, observer) => { | |
for (const mutation of mutationList) { | |
if (mutation.type === 'childList') { | |
if (oldUrl == document.URL && !oldChannelName && retryCount <= maxRetry) { | |
console.log(`retry: ${retryCount}`); | |
retryCount++; | |
oldChannelName = await getChannelName(); | |
console.log(oldChannelName); | |
} | |
if (oldUrl != document.URL) { | |
console.log("New url") | |
oldUrl = document.URL; | |
oldChannelName = null; | |
retryCount++; | |
oldChannelName = await getChannelName(); | |
console.log(oldChannelName); | |
} | |
} | |
} | |
} | |
const observer = new MutationObserver(callback); | |
observer.observe(targetNode, config); | |
// observer.disconnect();/ | |
function getChannelName() { | |
return new Promise((resolve, reject) => { | |
setTimeout(() => { | |
let channelName; | |
let channelNameElementScenario1 = document.querySelector("#channel-handle"); | |
let channelNameElementScenario2 = document.querySelector(".yt-simple-endpoint.ytd-video-owner-renderer"); | |
if (channelNameElementScenario1) { | |
channelName = channelNameElementScenario1.innerText; | |
} else if (channelNameElementScenario2) { | |
channelName = channelNameElementScenario2.getAttribute("href"); | |
} | |
resolve(channelName); | |
}, 10000); | |
}); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment