2020-02-27 08:44:03 +01:00
//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"}*//
2018-10-11 10:21:26 +02:00
2020-02-04 08:20:40 +01:00
var ForceImagePreviews = ( _ => {
2020-08-25 22:50:57 +02:00
var loadedEmbeds , requestedEmbeds ;
2020-02-04 08:20:40 +01:00
return class ForceImagePreviews {
getName ( ) { return "ForceImagePreviews" ; }
2019-01-17 23:48:29 +01:00
2020-08-25 22:50:57 +02:00
getVersion ( ) { return "1.2.0" ; }
2019-01-17 23:48:29 +01:00
2020-02-04 08:20:40 +01:00
getAuthor ( ) { return "DevilBro" ; }
2019-01-17 23:48:29 +01:00
2020-02-04 08:20:40 +01:00
getDescription ( ) { return "Forces embedded Image Previews, if Discord doesn't do it itself. Caution: Externals Images can contain malicious code and reveal your IP!" ; }
2019-01-26 22:45:19 +01:00
2020-02-04 08:20:40 +01:00
constructor ( ) {
this . changelog = {
2020-08-25 22:50:57 +02:00
"improved" : [ [ "Requests" , "No longer tries to request an embed more than one time" ] ]
2020-02-04 08:20:40 +01:00
} ;
this . patchedModules = {
before : {
SimpleMessageAccessories : "default"
}
} ;
}
2020-08-25 22:50:57 +02:00
initConstructor ( ) {
loadedEmbeds = { } ;
requestedEmbeds = [ ] ;
}
2018-10-11 10:21:26 +02:00
2020-04-11 19:32:58 +02:00
// Legacy
2020-08-21 16:17:47 +02:00
load ( ) {
if ( window . BDFDB && typeof BDFDB === "object" && BDFDB . loaded ) BDFDB . PluginUtils . load ( this ) ;
}
2018-10-11 10:21:26 +02:00
2020-02-04 08:20:40 +01:00
start ( ) {
if ( ! window . BDFDB ) window . BDFDB = { myPlugins : { } } ;
if ( window . BDFDB && window . BDFDB . myPlugins && typeof window . BDFDB . myPlugins == "object" ) window . BDFDB . myPlugins [ this . getName ( ) ] = this ;
let libraryScript = document . querySelector ( "head script#BDFDBLibraryScript" ) ;
if ( ! libraryScript || ( performance . now ( ) - libraryScript . getAttribute ( "date" ) ) > 600000 ) {
if ( libraryScript ) libraryScript . remove ( ) ;
libraryScript = document . createElement ( "script" ) ;
libraryScript . setAttribute ( "id" , "BDFDBLibraryScript" ) ;
libraryScript . setAttribute ( "type" , "text/javascript" ) ;
libraryScript . setAttribute ( "src" , "https://mwittrien.github.io/BetterDiscordAddons/Plugins/BDFDB.min.js" ) ;
libraryScript . setAttribute ( "date" , performance . now ( ) ) ;
libraryScript . addEventListener ( "load" , _ => { this . initialize ( ) ; } ) ;
document . head . appendChild ( libraryScript ) ;
}
else if ( window . BDFDB && typeof BDFDB === "object" && BDFDB . loaded ) this . initialize ( ) ;
this . startTimeout = setTimeout ( _ => {
try { return this . initialize ( ) ; }
catch ( err ) { console . error ( ` %c[ ${ this . getName ( ) } ]%c ` , "color: #3a71c1; font-weight: 700;" , "" , "Fatal Error: Could not initiate plugin! " + err ) ; }
} , 30000 ) ;
2019-05-26 13:55:26 +02:00
}
2018-10-11 10:21:26 +02:00
2020-02-04 08:20:40 +01:00
initialize ( ) {
if ( window . BDFDB && typeof BDFDB === "object" && BDFDB . loaded ) {
if ( this . started ) return ;
BDFDB . PluginUtils . init ( this ) ;
BDFDB . ModuleUtils . forceAllUpdates ( this ) ;
2020-03-16 11:01:36 +01:00
BDFDB . MessageUtils . rerenderAll ( ) ;
2020-02-04 08:20:40 +01:00
}
else console . error ( ` %c[ ${ this . getName ( ) } ]%c ` , "color: #3a71c1; font-weight: 700;" , "" , "Fatal Error: Could not load BD functions!" ) ;
2018-10-11 10:21:26 +02:00
}
2020-02-04 08:20:40 +01:00
stop ( ) {
if ( window . BDFDB && typeof BDFDB === "object" && BDFDB . loaded ) {
this . stopping = true ;
BDFDB . ModuleUtils . forceAllUpdates ( this ) ;
2020-03-16 11:01:36 +01:00
BDFDB . MessageUtils . rerenderAll ( ) ;
2020-02-04 08:20:40 +01:00
BDFDB . PluginUtils . clear ( this ) ;
}
2018-10-11 10:21:26 +02:00
}
2019-01-26 22:45:19 +01:00
2020-04-11 19:32:58 +02:00
// Begin of own functions
2020-02-04 08:20:40 +01:00
processSimpleMessageAccessories ( e ) {
if ( e . instance . props . message . content ) {
let message = new BDFDB . DiscordObjects . Message ( e . instance . props . message ) ;
2020-08-25 22:50:57 +02:00
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 ) ;
}
}
2020-02-04 08:20:40 +01:00
} ) ;
}
2020-08-25 22:50:57 +02:00
else if ( loadedEmbeds [ link ] ) message . embeds . push ( loadedEmbeds [ link ] ) ;
2018-10-11 10:21:26 +02:00
}
}
2020-08-25 22:50:57 +02:00
e . instance . props . message = message ;
}
2020-02-04 08:20:40 +01:00
}
isEmbedded ( embeds , link ) {
2020-03-17 08:58:51 +01:00
if ( link . indexOf ( "youtube." ) > - 1 || link . indexOf ( "youtu.be" ) > - 1 ) {
2020-06-04 16:18:21 +02:00
let videoId = ( link . split ( "watch?v=" ) [ 1 ] || link . split ( "?" ) [ 0 ] . split ( "/" ) . pop ( ) || "" ) . split ( "&" ) . shift ( ) ;
2020-03-17 08:58:51 +01:00
if ( videoId ) for ( let embed of embeds ) if ( embed . url == link || embed . video && embed . url . indexOf ( videoId ) > - 1 ) return true ;
}
else {
for ( let embed of embeds ) if ( embed . url == link || embed . image && embed . image . url == link ) return true ;
}
2020-02-04 08:20:40 +01:00
return false ;
}
2018-10-11 10:21:26 +02:00
}
2020-07-26 16:39:51 +02:00
} ) ( ) ;
module . exports = ForceImagePreviews ;