MediaWiki:Gadget-table-csv.js
Revision as of 18:26, 3 October 2022 by Jacmob (talk | contribs) (Created page with "$(function () { function cellToString(cell) { var $cell = $(cell).clone(); $cell.find('a').replaceWith(function(){ var $e = $(this); if ($e.text() === '') { //the handles normal linked images return $e.attr('title'); } return $e.text(); }); $cell.find('img').replaceWith(function(){ //any extra images without a link return $(this).attr('alt') }) return '"\''+$cell.text().trim()+'"'; } function sanitisePagename() { var pn = mw.config.g...")
Note: After publishing, you may have to bypass your browser's cache to see the changes.
- Firefox / Safari: Hold Shift while clicking Reload, or press either Ctrl-F5 or Ctrl-R (⌘-R on a Mac)
- Google Chrome: Press Ctrl-Shift-R (⌘-Shift-R on a Mac)
- Internet Explorer / Edge: Hold Ctrl while clicking Refresh, or press Ctrl-F5
- Opera: Press Ctrl-F5.
1 $(function () {
2 function cellToString(cell) {
3 var $cell = $(cell).clone();
4 $cell.find('a').replaceWith(function(){
5 var $e = $(this);
6 if ($e.text() === '') { //the handles normal linked images
7 return $e.attr('title');
8 }
9 return $e.text();
10 });
11 $cell.find('img').replaceWith(function(){ //any extra images without a link
12 return $(this).attr('alt')
13 })
14 return '"\''+$cell.text().trim()+'"';
15 }
16
17 function sanitisePagename() {
18 var pn = mw.config.get('wgPageName');
19 pn = pn.replaceAll(/[\/\\:?*<>"|]/gi, '-');
20 return pn;
21 }
22
23 function downloadTableCSV(event) {
24 var $targ = $(event.currentTarget);
25 var $table = $targ.parents('table.wikitable').first();
26 var data = [];
27 var rowspans = [];
28 // set up rowspans array
29 $table.find('>tr, >*>tr').first().find('>th,>td').each(function(i,e) {
30 // account for colspan in headers
31 // we are assuming all rows have the same number of columns
32 var cspan = $(e).attr('colspan');
33 cspan = parseInt(cspan)
34 if (cspan === NaN) {
35 cspan = 1;
36 }
37 for (var j=1; j<cspan; j++) {
38 rowspans.push(0);
39 }
40 });
41
42 $table.find('>tr, >*>tr').each(function(i,e){
43 var row = [];
44 var i_real = 0;
45 $(e).find('td,th').each(function(i2,e2){
46 while (true) {
47 while (i_real >= rowspans.length) {
48 rowspans.push(0);
49 }
50 if (rowspans[i_real] > 1) {
51 rowspans[i_real]--;
52 i_real++;
53 row.push('""');
54 } else {
55 break;
56 }
57 }
58 row.push(cellToString(e2));
59 var cspan = $(e2).attr('colspan');
60 cspan = parseInt(cspan);
61 if (cspan !== NaN) {
62 for (var i = 1; i < cspan; i++) {
63 row.push('""');
64 }
65 }
66 var rspan = $(e2).attr('rowspan');
67 rspan = parseInt(rspan);
68 if (rspan === NaN) {
69 rspan = 1;
70 }
71 rowspans[i_real] = rspan;
72 i_real++;
73 });
74 data.push(row.join(','));
75 });
76 var b = new Blob([data.join('\n')], {type:'text/csv'});
77 $table.find('a.rsw-gadget-csv-download').attr({
78 href: window.URL.createObjectURL(b),
79 download: sanitisePagename()+'.table'+$table.attr('data-rsw-csv-num')+'.csv'
80 });
81 }
82
83 function init() {
84 var $tables = $('#mw-content-text .mw-parser-output table.wikitable:not(.infobox)');
85 if ($tables.length===0) {
86 return;
87 }
88 $tables.each(function(i,e){
89 $(e).attr('data-rsw-csv-num', i).append(
90 $('<caption>').append(
91 $('<a>').text('Download CSV').addClass('rsw-gadget-csv-download')
92 ).attr('title', 'Download this table as a CSV')
93 );
94 });
95 $tables.find('a.rsw-gadget-csv-download').on('click', downloadTableCSV);
96 }
97 init()
98 })