$ table text copy
by Laurens Maneschijn
HTML
<p>
<h1>
JS bookmarklet for copy-pasting text from a table.<br>
</h1>
TR seperated by \n<br>
TD seperated by \t<br>
<br>
<h2>whitespace in each TD, optional:</h2>
<ol>
<li>replace all: <code>\n</code> → <code>\\n</code> , <code>\t</code> → <code>\\t</code><br></li>
<li>replace all: <code>\s+</code> → <code>' '</code> , and trim each cell<br></li>
</ol>
</p>
<hr>
Drag link to bookmarks:
<a href="javascript:(function(){ ; (function(window,$){ if(!$){return alert('$ missing');} function getTableText(target, keepwhitespace){ if($(target).closest('table').find('td,th').filter('[rowspan][rowspan!="1"],[colspan][colspan!="1"]').length){ if(!confirm('Warning, table has rowspan or colspan! things might not align as expected!\nContinue?')){ return; } } var trs = [], tr_strings = [], tds = [], $td, t, colspan, rowspan, i; $(target).closest('table').find('> tbody > tr, > thead > tr').each(function(itr,tr){ tds = []; $(tr).find('td,th').each(function(itd,td){ $td = $(td); rowspan = $td.attr('rowspan'); colspan = $td.attr('colspan'); if(!rowspan){rowspan = 1;} if(!colspan){colspan = 1;} t = ''+ $(td).text(); if(keepwhitespace){ t = t.replace(/\r?\n/g,'\\n'); t = t.replace(/\t/g,'\\t'); }else{ t = t.replace(/\s+/g,' '); t = $.trim(t); } if(colspan > 1){ for(i = colspan ; i > 1 ; i--){ t = t + '\t'; } } tds.push(t); }); trs.push(tds); }); tr_strings = []; for(i=0 ; i < trs.length ; i++){ tr_strings[i] = trs[i].join('\t'); } var text = tr_strings.join('\n') + '\n'; window.TABLETEXTCOPY_LAST = text; console.log('see window.TABLETEXTCOPY_LAST for last table text copy value.'); clipboard_copy(text); return text; } function clipboard_copy(str) { var ta = document.createElement('textarea'); ta.value = str;...
CSS
h1,h2,h3,h4,h5,h6{font-size:1em;padding:0 0 0 0;margin:2px 0 2px 0;}
ul,li{margin:0 0 0 0;}
code{outline:1px dotted #888;padding:1px; margin:1px;}
textarea{
width:100%;
box-sizing: border-box;
}
JavaScript
javascript:;
(function(window,$){
if(!$){return alert('$ missing');}
function getTableText(target, keepwhitespace){
if($(target).closest('table').find('td,th').filter('[rowspan][rowspan!="1"],[colspan][colspan!="1"]').length){
if(!confirm('Warning, table has rowspan or colspan! things might not align as expected!\nContinue?')){
return;
}
}
var trs = [], tr_strings = [], tds = [], $td, t, colspan, rowspan, i;
$(target).closest('table').find('> tbody > tr, > thead > tr').each(function(itr,tr){
tds = [];
$(tr).find('td,th').each(function(itd,td){
$td = $(td);
rowspan = $td.attr('rowspan');
colspan = $td.attr('colspan');
if(!rowspan){rowspan = 1;}
if(!colspan){colspan = 1;}
t = '' + $(td).text();
// remove or escape characters that will mess with tabulation:
if(keepwhitespace){
t = t.replace(/\r?\n/g,'\\n');
t = t.replace(/\t/g,'\\t');
}else{
// t = t.replace(/\r?\n/g,'');
// t = t.replace(/\t/g,'');
t = t.replace(/\s+/g,' ');
t = $.trim(t);
}
if(colspan > 1){
// attempt at pushing next td in correct position:
for(i = colspan ; i > 1 ; i--){
t = t + '\t';
}
}
tds.push(t);
});
trs.push(tds);
});
tr_strings = [];
for(i=0 ; i < trs.length ; i++){
tr_strings[i] = trs[i].join('\t');
}
var text = tr_strings.join('\n') + '\n';
window.TABLETEXTCOPY_LAST = text;
console.log('see window.TABLETEXTCOPY_LAST for last table text copy value.');
clipboard_copy(text);
return text;
}
function clipboard_copy(str) {
var ta = document.createElement('textarea');
ta.value = str;
document.body.appendChild(ta);
ta.select();
try {
document.execCommand('copy');
} catch(e) {
return false;
}
document.body.removeChild(ta);
return true;
}
$(document.body).off('mouseup.tabletextcopy');
$(document.body).on('mouseup.tabletextcopy', 'table', function(e){
if(e.ctrlKey && e.altKey &&...