Update ForceImagePreviews.plugin.js

This commit is contained in:
Mirco Wittrien 2020-08-25 22:50:57 +02:00
parent d4918755ff
commit cebe97b77a
1 changed files with 75 additions and 64 deletions

View File

@ -1,10 +1,12 @@
//META{"name":"ForceImagePreviews","authorId":"278543574059057154","invite":"Jx3TjNS","donate":"https://www.paypal.me/MircoWittrien","patreon":"https://www.patreon.com/MircoWittrien","website":"https://github.com/mwittrien/BetterDiscordAddons/tree/master/Plugins/ForceImagePreviews","source":"https://raw.githubusercontent.com/mwittrien/BetterDiscordAddons/master/Plugins/ForceImagePreviews/ForceImagePreviews.plugin.js"}*//
var ForceImagePreviews = (_ => {
var loadedEmbeds, requestedEmbeds;
return class ForceImagePreviews {
getName () {return "ForceImagePreviews";}
getVersion () {return "1.1.9";}
getVersion () {return "1.2.0";}
getAuthor () {return "DevilBro";}
@ -12,7 +14,7 @@ var ForceImagePreviews = (_ => {
constructor () {
this.changelog = {
"fixed":[["Youtube","No longer double embeds some special youtube links like 'youtu.be'... again"]]
"improved":[["Requests","No longer tries to request an embed more than one time"]]
};
this.patchedModules = {
@ -21,6 +23,11 @@ var ForceImagePreviews = (_ => {
}
};
}
initConstructor () {
loadedEmbeds = {};
requestedEmbeds = [];
}
// Legacy
load () {
@ -76,73 +83,77 @@ var ForceImagePreviews = (_ => {
processSimpleMessageAccessories (e) {
if (e.instance.props.message.content) {
let message = new BDFDB.DiscordObjects.Message(e.instance.props.message);
for (let word of e.instance.props.message.content.split(/\n|\s|\r|\t|\0/g)) if (word.indexOf("https://") > -1 || word.indexOf("http://") > -1) {
word = word.indexOf("<") == 0 && word.indexOf(">") == word.length-1 ? word.slice(1,-1) : word;
if (!this.isEmbedded(message.embeds, word)) this.injectEmbed(e.instance, message.embeds, word);
for (let link of e.instance.props.message.content.split(/\n|\s|\r|\t|\0/g)) if (link.indexOf("https://") > -1 || link.indexOf("http://") > -1) {
link = link.indexOf("<") == 0 && link.indexOf(">") == link.length - 1 ? link.slice(1, -1) : link;
if (!this.isEmbedded(message.embeds, link)) {
if (!requestedEmbeds.includes(link)) {
requestedEmbeds.push(link);
BDFDB.LibraryRequires.request(link, (error, response, result) => {
if (response && response.headers["content-type"] && response.headers["content-type"].indexOf("image") > -1) {
let imageThrowaway = document.createElement("img");
imageThrowaway.src = link;
imageThrowaway.onload = _ => {
if (!this.isEmbedded(message.embeds, link)) {
loadedEmbeds[link] = {
image: {
url: link,
proxyURL: link,
height: imageThrowaway.naturalHeight,
width: imageThrowaway.naturalWidth
},
type: "image",
url: link
};
message.embeds.push(loadedEmbeds[link]);
BDFDB.ReactUtils.forceUpdate(e.instance);
}
};
}
else if (response && response.headers["server"] && response.headers["server"].toLowerCase().indexOf("youtube") > -1 && result.indexOf("yt-user-info") > -1) {
if (!this.isEmbedded(message.embeds, link)) {
result = result.replace(/[\r|\n|\t]|[\s]{2,}/g, "");
let width = result.split(new RegExp(BDFDB.StringUtils.regEscape('<meta itemprop="width" content="'), "i"))[1].split('"')[0];
let height = result.split(new RegExp(BDFDB.StringUtils.regEscape('<meta itemprop="height" content="'), "i"))[1].split('"')[0];
let thumbnail = result.split(new RegExp(BDFDB.StringUtils.regEscape('<link itemprop="thumbnailUrl" href="'), "i"))[1].split('"')[0];
loadedEmbeds[link] = {
author: {
name: result.split(new RegExp(BDFDB.StringUtils.regEscape('<div class="yt-user-info"><a href="'), "i"))[1].split('>')[1].split('<')[0],
url: `https://www.youtube.com${result.split(new RegExp(BDFDB.StringUtils.regEscape('<div class="yt-user-info"><a href="'), "i"))[1].split('"')[0]}`
},
color: "#ff0000",
provider: {
name: "YouTube",
url: "https://www.youtube.com/"
},
rawDescription: result.split(new RegExp(BDFDB.StringUtils.regEscape('<meta property="og:description" content="'), "i"))[1].split('"')[0],
rawTitle: result.split(new RegExp(BDFDB.StringUtils.regEscape('<meta property="og:title" content="'), "i"))[1].split('"')[0],
thumbnail: {
url: thumbnail,
proxyURL: thumbnail,
width: width,
height: height
},
type: "video",
url: link,
video: {
url: result.split(new RegExp(BDFDB.StringUtils.regEscape('<link itemprop="embedUrl" href="'), "i"))[1].split('"')[0],
width: width,
height: height
}
};
message.embeds.push(loadedEmbeds[link]);
BDFDB.ReactUtils.forceUpdate(e.instance);
}
}
});
}
else if (loadedEmbeds[link]) message.embeds.push(loadedEmbeds[link]);
}
}
e.instance.props.message = message;
}
}
injectEmbed (instance, embeds, link) {
BDFDB.LibraryRequires.request(link, (error, response, result) => {
if (response && response.headers["content-type"] && response.headers["content-type"].indexOf("image") > -1) {
let imagethrowaway = document.createElement("img");
imagethrowaway.src = link;
imagethrowaway.onload = _ => {
if (!this.isEmbedded(embeds, link)) {
embeds.push({
image: {
url: link,
proxyURL: link,
height: imagethrowaway.naturalHeight,
width: imagethrowaway.naturalWidth
},
type: "image",
url: link
});
BDFDB.ReactUtils.forceUpdate(instance);
}
};
}
else if (response && response.headers["server"] && response.headers["server"].toLowerCase().indexOf("youtube") > -1 && result.indexOf("yt-user-info") > -1) {
if (!this.isEmbedded(embeds, link)) {
result = result.replace(/[\r|\n|\t]|[\s]{2,}/g, "");
let width = result.split(new RegExp(BDFDB.StringUtils.regEscape('<meta itemprop="width" content="'), "i"))[1].split('"')[0];
let height = result.split(new RegExp(BDFDB.StringUtils.regEscape('<meta itemprop="height" content="'), "i"))[1].split('"')[0];
let thumbnail = result.split(new RegExp(BDFDB.StringUtils.regEscape('<link itemprop="thumbnailUrl" href="'), "i"))[1].split('"')[0];
embeds.push({
author: {
name: result.split(new RegExp(BDFDB.StringUtils.regEscape('<div class="yt-user-info"><a href="'), "i"))[1].split('>')[1].split('<')[0],
url: `https://www.youtube.com${result.split(new RegExp(BDFDB.StringUtils.regEscape('<div class="yt-user-info"><a href="'), "i"))[1].split('"')[0]}`
},
color: "#ff0000",
provider: {
name: "YouTube",
url: "https://www.youtube.com/"
},
rawDescription: result.split(new RegExp(BDFDB.StringUtils.regEscape('<meta property="og:description" content="'), "i"))[1].split('"')[0],
rawTitle: result.split(new RegExp(BDFDB.StringUtils.regEscape('<meta property="og:title" content="'), "i"))[1].split('"')[0],
thumbnail: {
url: thumbnail,
proxyURL: thumbnail,
width: width,
height: height
},
type: "video",
url: link,
video: {
url: result.split(new RegExp(BDFDB.StringUtils.regEscape('<link itemprop="embedUrl" href="'), "i"))[1].split('"')[0],
width: width,
height: height
}
});
BDFDB.ReactUtils.forceUpdate(instance);
}
}
});
}
isEmbedded (embeds, link) {
if (link.indexOf("youtube.") > -1 || link.indexOf("youtu.be") > -1) {
let videoId = (link.split("watch?v=")[1] || link.split("?")[0].split("/").pop() || "").split("&").shift();