Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save LeonardoCiaccio/2bc363b3328cf0198d4e9b6d3f9a650b to your computer and use it in GitHub Desktop.
Save LeonardoCiaccio/2bc363b3328cf0198d4e9b6d3f9a650b to your computer and use it in GitHub Desktop.
Tampermonkey script to copy selected emails from a Wordpress list to the clipboard
// ==UserScript==
// @name Copy Emails
// @namespace Wordpress Tools
// @version 1.5
// @description Copy selected emails to any table containing an email (ScreenShot : https://i.imgur.com/Qe76ZmG.png)
// @author Leonardo Ciaccio
// @match */wp-admin/*
// @icon https://www.google.com/s2/favicons?domain=wordpress.org
// @grant none
// ==/UserScript==
(function($) {
'use strict';
//--> Checking if I loaded jquery properly
if( $ === 'undefined' ){
alert("This Tampermonkey script require jQuery");
return;
}
// --> Filter only good table with tbody
var $good_tables = $( '#wpbody-content' ).find( "td:contains('@')" ).closest( 'tbody' ).closest( 'table' );
$good_tables = $good_tables.find( 'input[type="checkbox"]' ).closest( 'table' );
//--> If there is no place to insert the button I will exit
if( $good_tables.length < 1 )return;
//--> I insert the button, first position
$good_tables.before( '<div style="padding:30px!important;"><input type="button" class="button copy-selected-email-next-table" value="Copy Selected Emails"></div>' );
//--> Copy all selected emails
$('.button.copy-selected-email-next-table').click( function(evt){
$(evt.target).val('Loading...');
var all_email = '';
$(evt.target).parent().next( "table" ).find('tr').each(function( index, tr ) {
if( $( tr ).find('th').first().find( 'input[type="checkbox"]' ).first().is(':checked') || $( tr ).find('td').first().find( 'input[type="checkbox"]' ).first().is(':checked') ){
if( $( tr ).find( "td:contains('@')" ).length > 0 ){
var testEmail = $( tr ).find( "td:contains('@')" ).first().text().match(/([a-zA-Z0-9._-]+@[a-zA-Z0-9._-]+\.[a-zA-Z0-9_-]+)/gi);
if( testEmail != null )all_email+= (all_email.length < 1) ? testEmail.join(',') : ',' + testEmail.join(',');
}
}
});
// --> Copy to clipboard
var dummy = $('<input>').val(all_email).appendTo('body').select();
document.execCommand('copy')
dummy.remove();
$(evt.target).val('Copyed to clipboard (' + ( (all_email.length < 1) ? 0 : all_email.split(',').length ) + ')' );
setTimeout( function(){
$(evt.target).val('Copy Selected Emails');
}, 1000 );
} );
})(window.jQuery);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment