Javascript 如何一次取消订阅所有 Youtube 频道?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/48874382/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to unsubscribe from all the Youtube channels at once?
提问by Mark Twain
I have subscribed to more than 300 Youtube channels in past 10 years, and now I have to clean my Youtube, unsubscribing all one by one will take some time, is there a way to unsubscribe all the cannels at once?
过去10年订阅了300多个YouTube频道,现在我要清理我的Youtube,一个一个地退订需要一些时间,有没有办法一次退订所有频道?
回答by Yogie
Step 1: Go to https://www.youtube.com/feed/channelsand scroll to the bottom of the page to populate all items to the screen.
第 1 步:转到https://www.youtube.com/feed/channels并滚动到页面底部以将所有项目填充到屏幕上。
Step 2: Right-click anywhere on the page and click "Inspect Element" (or just "Inspect"), then click "Console", then copy–paste the below script, then hit return.
第 2 步:右键单击页面上的任意位置并单击“检查元素”(或仅单击“检查”),然后单击“控制台”,然后复制粘贴以下脚本,然后点击return。
Step 3:
第 3 步:
var i = 0;
var myVar = setInterval(myTimer, 3000);
function myTimer () {
var els = document.getElementById("grid-container").getElementsByClassName("ytd-expanded-shelf-contents-renderer");
if (i < els.length) {
els[i].querySelector("[aria-label^='Unsubscribe from']").click();
setTimeout(function () {
var unSubBtn = document.getElementById("confirm-button").click();
}, 2000);
setTimeout(function () {
els[i].parentNode.removeChild(els[i]);
}, 2000);
}
i++;
console.log(i + " unsubscribed by YOGIE");
console.log(els.length + " remaining");
}
Step 4: Sit back and watch the magic!
第 4 步:坐下来观看魔术!
Enjoy!!
享受!!
NOTE:If the script stops somewhere, please refresh the page and follow all four steps again.
注意:如果脚本在某处停止,请刷新页面并再次执行所有四个步骤。
回答by Jani K?rkk?inen
Updating the answer provided by everyone else (as the latest update did not work for me):
更新其他人提供的答案(因为最新的更新对我不起作用):
var i = 0;
var count = document.querySelectorAll("ytd-channel-renderer:not(.ytd-item-section-renderer)").length;
myTimer();
function myTimer () {
if (count == 0) return;
el = document.querySelector('.ytd-subscribe-button-renderer');
el.click();
setTimeout(function () {
var unSubBtn = document.getElementById("confirm-button").click();
i++;
count--;
console.log(i + " unsubscribed");
console.log(count + " remaining");
setTimeout(function () {
el = document.querySelector("ytd-channel-renderer");
el.parentNode.removeChild(el);
myTimer();
}, 250);
}, 250);
}
For me this did the trick.
对我来说,这成功了。
回答by MordorSlave
Most effective values:
最有效的值:
(copy all above this, including the last)
(复制以上所有内容,包括最后一个)
var i = 0;
var myVar = setInterval(myTimer, 200);
function myTimer () {
var els = document.getElementById("grid-container").getElementsByClassName("ytd-expanded-shelf-contents-renderer");
if (i < els.length) {
els[i].querySelector('[aria-label="Unsubscribe from this channel."]').click();
setTimeout(function () {
var unSubBtn = document.getElementById("confirm-button").click();
}, 500);
setTimeout(function () {
els[i].parentNode.removeChild(els[i]);
}, 1000);
}
i++;
console.log(i + " unsubscribed by YOGIE");
console.log(els.length + " remaining");
}
回答by Ahmed Anssaien
This is a little addition to the best answer: You can also use jscompress[dot]com to compress the script, then add javascript:at the beginning of the script, and add it to your bookmarks — you can run it from there — just in case you're not comfortable using console or something like that. Good luck! :)
这是对最佳答案的一点补充:您还可以使用 jscompress[dot]com 来压缩脚本,然后在脚本的开头添加javascript:,并将其添加到您的书签中——您可以从那里运行它——只需如果您不习惯使用控制台或类似的东西。祝你好运!:)
P.S I don't have enough rep points to comment, so, no need to tell me something I know I should've done if it were possible.
PS我没有足够的代表点来评论,所以,没有必要告诉我一些我知道如果可能的话我应该做的事情。
回答by V1NNY
Youtube Channel Unsubscriber (Works April-2020)
Youtube 频道退订者(作品 2020 年 4 月)
- Access the link :
https://www.youtube.com/feed/channels - Press F12
- Insert the code below in your console
- 访问链接:
https://www.youtube.com/feed/channels - 按F12
- 在您的控制台中插入以下代码
function youtubeUnsubscriber() {
var count = document.querySelectorAll("ytd-channel-renderer:not(.ytd-item-section-renderer)").length;
var randomDelay = 500;
if(count == 0) return false;
function unsubscribeVisible(randomDelay) {
if (count == 0) {
window.scrollTo(0,document.body.scrollHeight);
setTimeout(function() {
youtubeUnsubscriber();
}, 10000)
}
unsubscribeButton = document.querySelector('.ytd-subscribe-button-renderer');
unsubscribeButton.click();
setTimeout(function () {
document.getElementById("confirm-button").click()
count--;
console.log("Remaining: ", count);
setTimeout(function () {
unsubscribedElement = document.querySelector("ytd-channel-renderer");
unsubscribedElement.parentNode.removeChild(unsubscribedElement);
unsubscribeVisible(randomDelay)
}, randomDelay);
}, randomDelay);
}
unsubscribeVisible(randomDelay);
}
youtubeUnsubscriber();
References
参考
回答by the_best
Updating Prashant Pimpale answer. Just did it a moment ago.
更新 Prashant Pimpale 的答案。刚才做了。
Go to https://www.youtube.com/feed/channelsand copy/paste the following in the console:
转到https://www.youtube.com/feed/channels并在控制台中复制/粘贴以下内容:
var i = 0;
var myVar = setInterval(myTimer, 200);
function myTimer () {
var els = document.getElementById("contents").getElementsByClassName("ytd-subscribe-button-renderer");
if (i < els.length) {
els[i].querySelector('.ytd-subscribe-button-renderer').click();
setTimeout(function () {
var unSubBtn = document.getElementById("confirm-button").click();
}, 500);
setTimeout(function () {
els[i].parentNode.removeChild(els[i]);
}, 1000);
}
i++;
console.log(i + " unsubscribed");
console.log(els.length + " remaining");
}
回答by krishgopinath
Hmm, wish I'd googled before rolling out my own. I had some fun with asyncand awaitwith this. The screen does some ugly flashing while it's trying to unsubscribe stuff, but this does the job pretty well.
嗯,希望我在推出自己的产品之前先用谷歌搜索一下。我async和await这个玩得很开心。屏幕在尝试取消订阅时会出现一些难看的闪烁,但这很好地完成了这项工作。
One prerequisite for this script to catch all channels in one go is to "exhaust" the scroller in the page ie., keep scrolling until you reach the end of your channel list. As others have stated, head on over to YouTube Channels, open the developer console and paste the script that follows.
此脚本一次性捕获所有频道的一个先决条件是“耗尽”页面中的滚动条,即,继续滚动直到到达频道列表的末尾。正如其他人所说,前往YouTube 频道,打开开发者控制台并粘贴以下脚本。
I've commented in relevant parts, in case this ends up becoming a learning experience for someone ;)
我已经在相关部分发表了评论,以防这最终成为某人的学习经历;)
/**
* Youtube bulk unsubsribe fn.
* Wrapping this in an IIFE for browser compatibility.
*/
(async function iife() {
// This is the time delay after which the "unsubscribe" button is "clicked"; Tweak to your liking!
var UNSUBSCRIBE_DELAY_TIME = 2000
/**
* Delay runner. Wraps `setTimeout` so it can be `await`ed on.
* @param {Function} fn
* @param {number} delay
*/
var runAfterDelay = (fn, delay) => new Promise((resolve, reject) => {
setTimeout(() => {
fn()
resolve()
}, delay)
})
// Get the channel list; this can be considered a row in the page.
var channels = Array.from(document.getElementsByTagName(`ytd-channel-renderer`))
console.log(`${channels.length} channels found.`)
var ctr = 0
for (const channel of channels) {
// Get the subsribe button and trigger a "click"
channel.querySelector(`[aria-label^='Unsubscribe from']`).click()
await runAfterDelay(() => {
// Get the dialog container...
document.getElementsByTagName(`yt-confirm-dialog-renderer`)[0]
// and find the confirm button...
.querySelector(`#confirm-button`)
// and "trigger" the click!
.click()
console.log(`Unsubsribed ${ctr + 1}/${channels.length}`)
ctr++
}, UNSUBSCRIBE_DELAY_TIME)
}
})()

