Skip to content

Instantly share code, notes, and snippets.

@GeonoTRON2000
Created July 29, 2017 07:17
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
Star You must be signed in to star a gist
Embed
What would you like to do?
Parse a [youtube]videoID[/youtube] or [youtube]videoURL[/youtube] BBCode tag
var posts = document.getElementsByClassName("postmsg");
// don't allow videos in signatures
var j;
for (j = 0; j < posts.length; j++) {
var thishtml = posts[i].innerHTML;
var i;
var vids = [];
for (i = 0; i < thishtml.split("[youtube]").length-1; i++) {
vids[i] = thishtml.split("[youtube]")[i+1].split("[/youtube]")[0];
if (vids[i].indexOf("http://") != -1 || vids[i].indexOf("https://") != -1) {
vids[i] = vids[i].split("v=")[1].split("&")[0];
if (vids[i].indexOf("&") > vids[i].indexOf("v=")) {
vids[i] = vids[i].split("&")[0];
}
vids[i] = decodeURIComponent(vids[i]);
}
}
for (i = 0; i < vids.length; i++) {
thishtml = thishtml.replace(/\[youtube\](.+?)\[\/youtube\]/i, "<iframe width=\"420\" height=\"315\" src=\"http://www.youtube.com/embed/"+vids[i]+"\" frameborder=\"0\" allowfullscreen></iframe>");
}
posts[i].innerHTML = thishtml;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment