Created
April 30, 2022 21:15
-
-
Save boardstretcher/698a878e418918989814c7b63c1ae26d to your computer and use it in GitHub Desktop.
Get youtube info
This file contains hidden or 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
const getYoutubeData = () => { | |
const videoElem = document.querySelector('ytd-watch-flexy.ytd-page-manager'); | |
const titleElem = document.querySelector('title'); | |
const video = videoElem?.getAttribute?.('video-id') ?? ''; | |
const videoString = `Video=${video}`; | |
const title = titleElem?.innerText ?? ''; | |
const titleFormatted = title?.trim?.()?.replace?.(' - YouTube', '').replace?.(/ /g, '_'); | |
const titleString = `Title=${titleFormatted}`; | |
// guard against incomplete data | |
if (!video || !title) return ''; | |
return `${videoString},${titleString},COMMAND`; | |
}; | |
const aggregateYoutubeData = () => { | |
const youtubeData = window.localStorage.getItem('youtubeData'); | |
const newYoutubeData = getYoutubeData(); | |
const newAggregateString = `${youtubeData || ''}${!!youtubeData ? '\n' : ''}${getYoutubeData()}` | |
// guard against incomplete data or duplicates | |
if (!newYoutubeData || youtubeData?.includes?.(newYoutubeData)) return; | |
window.localStorage.setItem('youtubeData', newAggregateString); | |
}; | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment