Last active
January 19, 2024 06:43
-
-
Save krishoang222/e3fd751a64e41993209c146ebe807d93 to your computer and use it in GitHub Desktop.
To scrape data (email, contacts, channel id) from Youtube to Airtable
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
javascript: (function () { | |
function simulateClick(selector) { | |
var element = document.querySelector(selector); | |
if (element) { | |
element.click(); | |
} | |
} | |
simulateClick('#channel-tagline ytd-channel-tagline-renderer a'); | |
const scrapeData = () => { | |
var content = document.querySelector( | |
'.style-scope.ytd-popup-container #content' | |
).innerHTML; | |
var emailRegex = | |
/([a-zA-Z0-9._-]+@[a-zA-Z0-9._-]+(\.[a-zA-Z0-9._-]+)?|@gmail\b)/gi; | |
var emailMatches = content.match(emailRegex); | |
var uniqueEmails = emailMatches ? Array.from(new Set(emailMatches)) : []; | |
var emailsText = ''; | |
var contactsText = ''; | |
if (uniqueEmails.length > 0) { | |
emailsText = uniqueEmails[0]; | |
if (uniqueEmails.length > 1) { | |
console.log('uniqueEmails', uniqueEmails); | |
contactsText = `Other emails: ${uniqueEmails.slice(1).join(', ')}\n\n`; | |
} | |
} | |
emailsText = !!emailsText ? emailsText : ''; | |
var links = document.querySelectorAll( | |
'#link-list-container yt-channel-external-link-view-model' | |
); | |
links.forEach(function (link) { | |
var labelSpan = link.querySelector( | |
'.yt-channel-external-link-view-model-wiz__title' | |
); | |
var label = labelSpan ? labelSpan.textContent.trim() : 'Unknown Label'; | |
var visibleUrl = link.querySelector('a').textContent.trim(); | |
if (contactsText.includes(visibleUrl)) return; | |
contactsText += `${label}: ${visibleUrl}\n`; | |
}); | |
contactsText = | |
contactsText.length > 0 | |
? `"${contactsText.trim().replace(/"/g, "'")}"` | |
: ''; | |
simulateClick('div#share-channel button'); | |
setTimeout(function () { | |
var items = document.querySelectorAll('ytd-menu-service-item-renderer'); | |
items.forEach(function (item) { | |
if (item.textContent.includes('Copy channel ID')) { | |
item.click(); | |
} | |
}); | |
navigator.clipboard.readText().then((channelId) => { | |
var combinedData = | |
emailsText + '\t\t' + contactsText + '\t' + channelId; | |
navigator.clipboard | |
.writeText(combinedData.replace(/\n{3,}/g, '\n\n')) | |
.then(() => { | |
console.log('Data copied to clipboard:\n' + combinedData); | |
}) | |
.catch((err) => alert('Failed to copy data: ' + err)); | |
}); | |
}, 500); | |
}; | |
setTimeout( | |
scrapeData, | |
500 | |
); | |
})(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment