Skip to content

Instantly share code, notes, and snippets.

@dclamage
Created August 12, 2024 03:37
Show Gist options
  • Save dclamage/faab559fcf6a9dbb1d4c5f118fd8e296 to your computer and use it in GitHub Desktop.
Save dclamage/faab559fcf6a9dbb1d4c5f118fd8e296 to your computer and use it in GitHub Desktop.
Extract youtube comments from current page
// ==UserScript==
// @name Extract YouTube Comments
// @namespace http://tampermonkey.net/
// @version 2024-05-29
// @description Extract YouTube Comments
// @author Rangsk
// @match https://www.youtube.com/watch*
// @icon https://www.google.com/s2/favicons?sz=64&domain=youtube.com
// @grant none
// ==/UserScript==
(function() {
'use strict';
window.extractComments = function() {
return [...document.querySelectorAll('.ytd-comment-view-model')]
.filter(div => div.querySelectorAll('span[role="text"]').length > 0)
.filter(div => {
const span = div.querySelector('span');
return span && span.innerText[0] === '@';
})
.map(div => [...div.querySelectorAll('span')].filter(span => (span.innerText[0] === '@' || span.getAttribute('role') === 'text' || span.classList.contains('yt-core-attributed-string')) && span.innerText != 'Reply').map(span => span.innerText)).filter((_, index) => index % 2 === 0).map(o => o.join(' - ').replace(/\r?\n|\r/g, ' '))
.join('\n');
}
})();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment