module.exports = (Plugin, Api, Vendor) => { if (typeof BDFDB !== "object") global.BDFDB = {$: Vendor.$, BDv2Api: Api}; const {$} = Vendor; return class extends Plugin { initConstructor () { this.waitTime = 3000; } onStart () { var libraryScript = null; if (typeof BDFDB !== "object" || typeof BDFDB.isLibraryOutdated !== "function" || BDFDB.isLibraryOutdated()) { libraryScript = document.querySelector('head script[src="https://mwittrien.github.io/BetterDiscordAddons/Plugins/BDFDB.js"]'); if (libraryScript) libraryScript.remove(); libraryScript = document.createElement("script"); libraryScript.setAttribute("type", "text/javascript"); libraryScript.setAttribute("src", "https://mwittrien.github.io/BetterDiscordAddons/Plugins/BDFDB.js"); document.head.appendChild(libraryScript); } this.startTimeout = setTimeout(() => {this.initialize();}, 30000); if (typeof BDFDB === "object" && typeof BDFDB.isLibraryOutdated === "function") this.initialize(); else libraryScript.addEventListener("load", () => {this.initialize();}); return true; } initialize () { if (typeof BDFDB === "object") { BDFDB.loadMessage(this); var observer = null; observer = new MutationObserver((changes, _) => { changes.forEach( (change, i) => { if (change.type == "characterData") { setTimeout(() => {this.addPreviews(change.target.parentElement);},this.waitTime); } if (change.addedNodes) { change.addedNodes.forEach((node) => { if ($(node).attr("class") == BDFDB.disCN.message) setTimeout(() => {this.addPreviews($(node).find(BDFDB.dotCN.messagemarkup)[0]);},this.waitTime); }); } } ); }); BDFDB.addObserver(this, null, {name:"messageChangeObserver",instance:observer,multi:true}, {childList:true, characterData:true, subtree:true}); observer = new MutationObserver((changes, _) => { changes.forEach( (change, i) => { if (change.addedNodes) { change.addedNodes.forEach((node) => { if (node && node.tagName && node.querySelector(BDFDB.dotCN.message)) { BDFDB.addObserver(this, node, {name:"messageChangeObserver",multi:true}, {childList:true, characterData:true, subtree:true}); node.querySelectorAll(BDFDB.dotCN.messagemarkup).forEach(message => { setTimeout(() => {this.addPreviews(message);},this.waitTime); }); } }); } } ); }); BDFDB.addObserver(this, BDFDB.dotCN.messages, {name:"chatWindowObserver",instance:observer}, {childList:true}); this.addAllPreviews(); return true; } else { console.error(this.name + ": Fatal Error: Could not load BD functions!"); return false; } } onStop () { if (typeof BDFDB === "object") { document.querySelectorAll(".FIP-embed").forEach(embed => {embed.remove();}); BDFDB.unloadMessage(this); return true; } else { return false; } } onSwitch () { if (typeof BDFDB === "object") { this.addAllPreviews(); BDFDB.addObserver(this, BDFDB.dotCN.messages, {name:"chatWindowObserver"}, {childList:true, subtree:true}); } } // begin of own functions addAllPreviews () { document.querySelectorAll(".FIP-embed").forEach(embed => {embed.remove();}); document.querySelectorAll(BDFDB.dotCN.messagegroup).forEach(messageContainer => { BDFDB.addObserver(this, messageContainer, {name:"messageChangeObserver",multi:true}, {childList:true, characterData:true, subtree:true}); messageContainer.querySelectorAll(BDFDB.dotCN.messagemarkup).forEach(message => { this.addPreviews(message); }); }); } addPreviews (message) { let scroller = document.querySelector(BDFDB.dotCNS.chat + BDFDB.dotCN.messages); if (!message || !scroller) return; var messageData = BDFDB.getKeyInformation({node:message,key:"message",up:true}); if (!messageData) return; let accessory = this.getAccessoryOfMessage(message); if (accessory) { let links = []; for (let word of messageData.content.split(new RegExp("\\n|\\s|\\r|\\t|\\0"))) { if (word.indexOf("https://") > -1 || word.indexOf("http://") > -1) { if (word.indexOf("<") == 0 && word.indexOf(">") == word.length-1) links.push({src:word.slice(1,-1),embedded:false}); else if (!accessory.querySelector(`${BDFDB.dotCN.embedimage}[href="${this.parseSrc(word)}"]`) && !accessory.querySelector(`${BDFDB.dotCN.embedtitlelink}[href="${this.parseSrc(word)}"]`)) { links.push({src:word,embedded:false}); } else links.push({src:word,embedded:true}); } } if (links.length > 0) this.addItemToAccessory(null, links, accessory, scroller); } } addItemToAccessory (previmage, links, accessory, scroller) { let item = links.shift(); if (!item) return; else if (item.embedded) this.addItemToAccessory(item, links, accessory, scroller); else { let itemsrc = this.parseSrc(item.src); require("request")(itemsrc, (error, response, result) => { if (response && response.headers["content-type"] && response.headers["content-type"].indexOf("image") > -1) { let imagethrowaway = document.createElement("img"); imagethrowaway.src = itemsrc; imagethrowaway.onload = () => { let width = 400; let height = Math.round(width*(imagethrowaway.naturalHeight/imagethrowaway.naturalWidth)); if (!accessory.querySelector(`${BDFDB.dotCN.embedimage}[href="${itemsrc}"]`)) { let embed = $(`
`)[0]; this.insertEmbed(embed, previmage, links, accessory, scroller); } this.addItemToAccessory(item, links, accessory, scroller); }; } else if (response && response.headers["server"] && response.headers["server"].toLowerCase().indexOf("youtube") > -1 && result.indexOf("yt-user-info") > -1) { if (!accessory.querySelector(`${BDFDB.dotCN.embedtitlelink}[href="${itemsrc}"]`)) { result = result.replace(new RegExp("[\\r|\\n|\\t]|[\\s]{2,}", "g"), ""); let width = 400; let height = Math.round(width*(result.split('
`)[0]; $(embed).on("click." + this.name, BDFDB.dotCN.iconplay, (e) => { let videowrapper = embed.querySelector(BDFDB.dotCN.embedvideo); while (videowrapper.firstChild) videowrapper.firstChild.remove(); $(``).appendTo(videowrapper); }); this.insertEmbed(embed, previmage, links, accessory, scroller); } this.addItemToAccessory(item, links, accessory, scroller); } else this.addItemToAccessory(item, links, accessory, scroller); }); } } insertEmbed (embed, previmage, links, accessory, scroller) { let prev = accessory.querySelector(`${BDFDB.dotCNS.embed + BDFDB.dotCN.embedimage}[href="${previmage ? this.parseSrc(previmage.src) : void 0}"]`); let next = accessory.querySelector(`${BDFDB.dotCNS.embed + BDFDB.dotCN.embedimage}[href="${links[0] ? this.parseSrc(links[0].src) : void 0}"]`); prev = prev ? prev : accessory.querySelector(`${BDFDB.dotCNS.embed + BDFDB.dotCN.embedtitlelink}[href="${previmage ? this.parseSrc(previmage.src) : void 0}"]`); next = next ? next : accessory.querySelector(`${BDFDB.dotCNS.embed + BDFDB.dotCN.embedtitlelink}[href="${links[0] ? this.parseSrc(links[0].src) : void 0}"]`); accessory.insertBefore(embed, prev ? prev.nextSibling : next); scroller.scrollTop += embed.getBoundingClientRect().height; } getAccessoryOfMessage (message) { var accessory = null; while (message && !message.querySelector(BDFDB.dotCN.messagegroup) && !accessory) { accessory = message.querySelector(BDFDB.dotCN.messageaccessory); message = message.parentElement; } return accessory; } parseSrc (src) { return src.replace(/"/g, ""); } } };