MediaWiki:Common.js: Difference between revisions
MediaWiki interface page
More actions
Add CSV export button above every wikitable on all content pages |
Fix: use .children(caption) not .find(> caption) for correct detection |
||
| (2 intermediate revisions by the same user not shown) | |||
| Line 1: | Line 1: | ||
/* ================================================================ | /* ================================================================ | ||
CSV Export Button — injects "⬇ Export CSV" above | CSV Export Button — injects "⬇ Export CSV" only above wikitables | ||
that have a <caption> as direct child (numeric/calculable tables). | |||
Descriptive tables without a caption are left untouched. | |||
================================================================ */ | ================================================================ */ | ||
$(function () { | |||
var pageName = mw.config.get('wgTitle') || 'export'; | var pageName = mw.config.get('wgTitle') || 'export'; | ||
var safeTitle = pageName.replace(/ /g, '_'); | var safeTitle = pageName.replace(/ /g, '_'); | ||
var exportIndex = 0; | |||
$ | $('#mw-content-text table.wikitable').each(function () { | ||
var $table = $(this); | var $table = $(this); | ||
// | // Use .children() to check for direct caption child only | ||
if ($table.children('caption').length === 0) return; | |||
exportIndex++; | |||
var idx = exportIndex; | |||
var $btn = $('<button>') | var $btn = $('<button>') | ||
.text('⬇ Export CSV') | .text('⬇ Export CSV') | ||
| Line 33: | Line 40: | ||
var line = []; | var line = []; | ||
cells.each(function () { | cells.each(function () { | ||
var txt = $(this).text().replace(/\s+/g, ' ').trim(); | var txt = $(this).text().replace(/\s+/g, ' ').trim(); | ||
if (txt.indexOf(',') !== -1 || txt.indexOf('"') !== -1) { | |||
if (txt.indexOf(',') !== -1 || txt.indexOf('" | |||
txt = '"' + txt.replace(/"/g, '""') + '"'; | txt = '"' + txt.replace(/"/g, '""') + '"'; | ||
} | } | ||
line.push(txt); | line.push(txt); | ||
}); | }); | ||
if (line.length > 0) | if (line.length > 0) csv.push(line.join(',')); | ||
}); | }); | ||
var | var blob = new Blob([csv.join('\n')], { type: 'text/csv;charset=utf-8;' }); | ||
var url = URL.createObjectURL(blob); | var url = URL.createObjectURL(blob); | ||
var suffix = | var suffix = idx > 1 ? '_' + idx : ''; | ||
var $a = $('<a>').attr('href', url).attr('download', safeTitle + suffix + '.csv').css('display', 'none'); | |||
var $a = $('<a>') | |||
$('body').append($a); | $('body').append($a); | ||
$a[0].click(); | $a[0].click(); | ||
| Line 62: | Line 59: | ||
}); | }); | ||
$table.before($('<div>').css('margin', '0 0 2px 0').append($btn)); | |||
}); | }); | ||
}); | }); | ||
Latest revision as of 20:50, 17 May 2026
/* ================================================================
CSV Export Button — injects "⬇ Export CSV" only above wikitables
that have a <caption> as direct child (numeric/calculable tables).
Descriptive tables without a caption are left untouched.
================================================================ */
$(function () {
var pageName = mw.config.get('wgTitle') || 'export';
var safeTitle = pageName.replace(/ /g, '_');
var exportIndex = 0;
$('#mw-content-text table.wikitable').each(function () {
var $table = $(this);
// Use .children() to check for direct caption child only
if ($table.children('caption').length === 0) return;
exportIndex++;
var idx = exportIndex;
var $btn = $('<button>')
.text('⬇ Export CSV')
.attr('title', 'Download this table as a CSV file')
.css({
cursor: 'pointer',
background: '#1e1e1e',
color: '#e07000',
border: '1px solid #e07000',
borderRadius: '4px',
padding: '3px 12px',
fontSize: '0.82em',
fontWeight: 'bold',
marginBottom: '4px',
display: 'inline-block'
})
.on('click', function () {
var rows = $table.find('tr');
var csv = [];
rows.each(function () {
var cells = $(this).find('th, td');
var line = [];
cells.each(function () {
var txt = $(this).text().replace(/\s+/g, ' ').trim();
if (txt.indexOf(',') !== -1 || txt.indexOf('"') !== -1) {
txt = '"' + txt.replace(/"/g, '""') + '"';
}
line.push(txt);
});
if (line.length > 0) csv.push(line.join(','));
});
var blob = new Blob([csv.join('\n')], { type: 'text/csv;charset=utf-8;' });
var url = URL.createObjectURL(blob);
var suffix = idx > 1 ? '_' + idx : '';
var $a = $('<a>').attr('href', url).attr('download', safeTitle + suffix + '.csv').css('display', 'none');
$('body').append($a);
$a[0].click();
$a.remove();
setTimeout(function () { URL.revokeObjectURL(url); }, 1000);
});
$table.before($('<div>').css('margin', '0 0 2px 0').append($btn));
});
});