Skip to content

Commit

Permalink
Skip image uploading from same domain, check DOM before insert #53
Browse files Browse the repository at this point in the history
  • Loading branch information
mcguffin committed Oct 27, 2023
1 parent 20a9118 commit 462ee53
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 3 deletions.
4 changes: 2 additions & 2 deletions src/js/admin/mce/the-paste-plugin/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ class PasteOperation {
div.innerHTML = html
images.push( ...Array.from(div.querySelectorAll('img')) )

Array.from(div.childNodes).forEach( node => placeholder.parentNode.insertBefore( node, placeholder ) )
Array.from(div.childNodes).forEach( node => placeholder?.parentNode?.insertBefore( node, placeholder ) )
placeholder?.remove()

if ( images.length ) {
Expand Down Expand Up @@ -237,7 +237,7 @@ tinymce.PluginManager.add( 'the_paste', editor => {
})
})
.on( 'Paste', e => {
if ( !!pasteOnOffBtn && ! pasteOnOffBtn.active() ) {
if ( !!pasteOnOffBtn && ! pasteOnOffBtn.active() || document.body.matches('.modal-open') ) {
return;
}
const pasteOperation = PasteOperation.init(e) //.dumpClipboardData()
Expand Down
10 changes: 9 additions & 1 deletion src/js/lib/converter.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,18 @@ const itemHandler = type => {
return []
},
'text/html': async item => {
const loc = new URL( document.location )
const div = document.createElement('div')
div.innerHTML = await Converter.itemToString( item )

const imgs = Array.from( div.querySelectorAll('img') ).map( img => Converter.elementToFile(img) )
const imgs = Array.from( div.querySelectorAll('img') )
.filter( img => {
// remove images from same domain
const u = new URL(img.src)
return ! ['http:','https:'].includes(u.protocol) || loc.hostname !== u.hostname
} )
.map( img => Converter.elementToFile(img) )

return new Promise( (resolve,reject) => {
Promise.allSettled( imgs ).then( result => resolve( Array.from(result).map( promise => promise.value )) )
})
Expand Down

0 comments on commit 462ee53

Please sign in to comment.