Skip to content

Instantly share code, notes, and snippets.

@yuitest
Last active June 18, 2024 17:39
Show Gist options
  • Save yuitest/0fab9f1ad3413214e84922e2647757ac to your computer and use it in GitHub Desktop.
Save yuitest/0fab9f1ad3413214e84922e2647757ac to your computer and use it in GitHub Desktop.
YouTube でチャンネルをおすすめに表示しないを自動で押すスクリプト
const denyWords = ['反応集', '切り抜き', '世間の反応', '2ch'];
const allowWords = ['公式'];
const $$ = (selector) => [...document.querySelectorAll(selector)]
const sleep = msec => new Promise(resolve => setTimeout(resolve, msec));
const block = async (element) => {
const channelTitle = getChannelTitle(element);
const videoTitle = getVideoTitle(element);
console.log(`block: ${channelTitle} - ${videoTitle}`)
const menuButton = element.querySelector('button#button.style-scope.yt-icon-button');
menuButton.click();
await sleep(200);
const blockButton = $$('yt-formatted-string.style-scope.ytd-menu-service-item-renderer').filter((e) => {
return e.textContent === 'チャンネルをおすすめに表示しない'
})[0]
blockButton.click();
}
const getChannelTitle = (element) => {
return element.querySelector('a.yt-simple-endpoint.style-scope.yt-formatted-string').textContent;
}
const getVideoTitle = (element) => {
return element.querySelector('#video-title').textContent;
}
const isBlockTarget = (element) => {
const channelTitle = getChannelTitle(element);
const videoTitle = getVideoTitle(element);
const shouldBeBlock = denyWords.some((word) => channelTitle.includes(word) || videoTitle?.includes(word));
const shouldBePass = allowWords.some((word) => channelTitle.includes(word) || videoTitle?.includes(word));
return shouldBeBlock && !shouldBePass;
}
const main = async () => {
const blockTargets = $$('ytd-rich-item-renderer').filter(isBlockTarget);
for (const element of blockTargets) {
await block(element);
}
}
await main();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment