Last active
September 24, 2018 13:59
-
-
Save andycochrane/fdbfe8635c4afcfd497cce7747af23b6 to your computer and use it in GitHub Desktop.
Create an alert with the number of YouTube and Vimeo videos on the current page.
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
/** | |
* Create an alert with the number of YouTube and Vimeo | |
* videos on the current page. | |
*/ | |
function countVideosOnPage() { | |
var iframes = document.getElementsByTagName("iframe"); | |
var youtubeVideos = []; | |
var vimeoVideos = []; | |
function isVimeoUrl(url) { | |
return /^(https?:)?\/\/((player|www).)?vimeo.com(?=$|\/)/.test(url); | |
} | |
function isYoutubeUrl(url) { | |
return /^(https?:)?\/\/www.youtube.com(?=$|\/)/.test(url); | |
} | |
for (var i = 0, l = iframes.length; i < l; i++) { | |
var source = iframes[i].getAttribute('src'); | |
if (isVimeoUrl(source)) { | |
vimeoVideos.push(iframes[i]); | |
} | |
if (isYoutubeUrl(source)) { | |
youtubeVideos.push(iframes[i]); | |
} | |
} | |
var plYT = youtubeVideos.length == 1 ? '' : 's'; | |
var plV = vimeoVideos.length == 1 ? '' : 's'; | |
if (youtubeVideos.length > 0) { | |
console.log('YouTube videos:', youtubeVideos); | |
} | |
if (vimeoVideos.length > 0) { | |
console.log('Vimeo videos:', vimeoVideos); | |
} | |
alert('You will find ' + youtubeVideos.length + ' youtube video' + plYT + ' and ' + vimeoVideos.length + ' vimeo video' + plV + ' on this page.'); | |
}; | |
/** | |
* Or as a bookmarklet | |
*/ | |
javascript: (function () { var iframes = document.getElementsByTagName("iframe"); var youtubeVideos = []; var vimeoVideos = []; function isVimeoUrl(url) { return /^(https?:)?\/\/((player|www).)?vimeo.com(?=$|\/)/.test(url); } function isYoutubeUrl(url) { return /^(https?:)?\/\/www.youtube.com(?=$|\/)/.test(url); } for (var i = 0, l = iframes.length; i < l; i++) { var source = iframes[i].getAttribute('src'); if (isVimeoUrl(source)) { vimeoVideos.push(iframes[i]); } if (isYoutubeUrl(source)) { youtubeVideos.push(iframes[i]); } } var plYT = youtubeVideos.length == 1 ? '' : 's'; var plV = vimeoVideos.length == 1 ? '' : 's'; if (youtubeVideos.length > 0) { console.log('YouTube videos:', youtubeVideos); } if (vimeoVideos.length > 0) { console.log('Vimeo videos:', vimeoVideos); } alert('You will find ' + youtubeVideos.length + ' youtube video' + plYT + ' and ' + vimeoVideos.length + ' vimeo video' + plV + ' on this page.'); })(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment