fore.st/www/js/fembed.js

59 lines
2.3 KiB
JavaScript

img_ext = ['jpg', 'jpg:large', 'jpeg', 'png', 'tiff', 'gif'];
vid_ext = ['webm', 'gifv', 'mp4'];
proto = ['http', 'https'];
function checkMedia(fname){//check if link points ot media
var fext = fname.split('.').pop().toLowerCase();//pop file ext
if(img_ext.includes(fext)){//if its an img
return 1;
}else if(vid_ext.includes(fext)){//if its a vid
return 2;
}else{//nada
return 0;
}
}
function checkEmbed(word, isEmote){
let regex = /[!"#$%&'()*+,./:;<=>?@[\]^`{|}~]/g;//symbol mask for username
let tregex = /["#$%&'()*+,-./:;<=>?@[\]^_`{|}~]/g;//symbol mask for tokes
let stripd = word.replace(regex, '');//stripped word for username detection
let tstripd = word.replace(tregex, '');//stripeed word for !toke command detection
if(word.includes(proto[0]) || word.includes(proto[1])){//check if it starts with a supported proto
if(checkMedia(word) != 0){//check if media
return '<img src="' + word + '" style="max-height: 13em">';//embed media
}else if(!isEmote){//if its a link
if(word.includes("imgur.com")){
if(word.length > 20 && word.length < 28){
return '<img src="' + word + ".gif" + '" style="max-height: 13em">';//embed media
}
}
return '<a target="_blank" href="' + word + '">' + word + '</a>';//embed link
}else{
return word;
}
}else if(usrColors[0].includes(stripd)){//if username
let usersplit = word.split(stripd,2);//split word by stripd, array of before and after name
return usersplit[0] + '<span id="' + getColor(stripd) + '" onclick="chatpaste(\'' + stripd + '\')">' + stripd + '</span>' + usersplit[1];//decorate name
}else if(tstripd.charAt(0) === '!'){//if !toke command(same logic as above)
let tokesplit = word.split(tstripd,2);
return tokesplit[0] + '<a id="toke" onclick="chatsmack(\'' + tstripd + '\')">' + tstripd + '</a>' + tokesplit[1];
}else{
return word;
}
}
function findEmbed(str){//check entire string for embeds
let isEmote = str.includes('class="channel-emote"');
var splts = str.split(/\s/g);//split string by whitespace
let baked = checkEmbed(splts[0], isEmote) + ' ';//work string, set first item to checkEmbed of first word
for(i = 1; i < splts.length; i++){//run through words
baked += checkEmbed(splts[i], isEmote) + ' ';//checkEmbed() and add to baked with a space after it
}
return baked;//return baked ;p
}