$ 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> &rarr; <code>\\n</code> , <code>\t</code> &rarr; <code>\\t</code><br></li>
	<li>replace all: <code>\s+</code> &rarr; <code>' '</code> , and trim each cell<br></li>
	</ol>
</p>

<hr>
Drag link to bookmarks:
<a href="javascript:(function(){ ; (function(window,$){ if(!$){return alert(&apos;$ missing&apos;);} function getTableText(target, keepwhitespace){ if($(target).closest(&apos;table&apos;).find(&apos;td,th&apos;).filter(&apos;[rowspan][rowspan!=&quot;1&quot;],[colspan][colspan!=&quot;1&quot;]&apos;).length){ if(!confirm(&apos;Warning, table has rowspan or colspan! things might not align as expected!\nContinue?&apos;)){ return; } } var trs = [], tr_strings = [], tds = [], $td, t, colspan, rowspan, i; $(target).closest(&apos;table&apos;).find(&apos;&gt; tbody &gt; tr, &gt; thead &gt; tr&apos;).each(function(itr,tr){ tds = []; $(tr).find(&apos;td,th&apos;).each(function(itd,td){ $td = $(td); rowspan = $td.attr(&apos;rowspan&apos;); colspan = $td.attr(&apos;colspan&apos;); if(!rowspan){rowspan = 1;} if(!colspan){colspan = 1;} t = &apos;&apos;+ $(td).text(); if(keepwhitespace){ t = t.replace(/\r?\n/g,&apos;\\n&apos;); t = t.replace(/\t/g,&apos;\\t&apos;); }else{ t = t.replace(/\s+/g,&apos; &apos;); t = $.trim(t); } if(colspan &gt; 1){ for(i = colspan ; i &gt; 1 ; i--){ t = t + &apos;\t&apos;; } } tds.push(t); }); trs.push(tds); }); tr_strings = []; for(i=0 ; i &lt; trs.length ; i++){ tr_strings[i] = trs[i].join(&apos;\t&apos;); } var text = tr_strings.join(&apos;\n&apos;) + &apos;\n&apos;; window.TABLETEXTCOPY_LAST = text; console.log(&apos;see window.TABLETEXTCOPY_LAST for last table text copy value.&apos;); clipboard_copy(text); return text; } function clipboard_copy(str) { var ta = document.createElement(&apos;textarea&apos;); 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 &&...