LLWiki正在建设中,欢迎加入我们

“User:Bhsd/tool.js”的版本间差异

来自LLWiki
跳转到导航 跳转到搜索
(以“// 生成JS文件大小 var api = new mw.Api(), table = $('table').first(), list = table.find('td:first-child a'); table.find('th').first().after( '<th>大小</th>' )…”替换内容)
标签替换
第1行: 第1行:
/* 生成JS文件大小
// 生成JS文件大小
var api = new mw.Api(),
var api = new mw.Api(),
table = $('table').first(),
table = $('table').first(),
第11行: 第11行:
ele.parent().after('<td>' + length + '</td>');
ele.parent().after('<td>' + length + '</td>');
});
});
});
*/

// 引自[[wikipedia:User:Kephir/gadgets/table-editor.js]]
$(function() {
if(!['edit', 'submit'].includes( mw.config.get('wgAction') ) ||
mw.config.get('wgPageContentModel') != 'wikitext')
return;

/* 表格列操作 */
var editBox = $('#wpTextbox1');
function getSelection(textarea) {
var start = textarea.prop('selectionStart'),
end = textarea.prop('selectionEnd');
return [textarea.val().substring(0, start),
textarea.val().substring(start, end),
textarea.val().substring(end)
];
}
function readTable(table) {
table = table.trim();
if( !/^[\s]*{\|[\s\S]*\n[\s]*\|}$/.test(table) ) { return false; }
table = table.substring(2, table.length - 2);
var tableProperty = table.match(/^([\s\S]*?)\n/)[1],
tbody = table.substring( tableProperty.length ).trim(),
tr = tbody.split('|-').filter(e => /[\|!]/.test(e)).map(e => e.trim());
trProperty = tr.map(e => {
if( /^[\|!]/.test(e) ) { return ""; }
return e.match(/^([\s\S]*?\n[\S]*?)[\|!]/)[1];
}),
td = tr.map( (e, i) => [e.charAt(trProperty[i].length), ...e.substring( trProperty[i].length + 1 ).split(/(\|\||\n[\s]*\||!!|\n[\s]*!)/)] );
return {tableProperty: tableProperty, trProperty: trProperty, td: td};
}
function parseTable(table) {
var result = "{|" + table.tableProperty.trim() + "\n",
td = table.td;
table.trProperty.forEach(function(e, i) {
if(i || e.trim()) { result += "|-" + e.trim() + "\n"; }
for(var j = 0; j < td[i].length / 2; j++) {
result += td[i][j * 2].slice(-1) + td[i][j * 2 + 1].trim() + "\n";
}
});
return result + "|}";
}
function parseCompactTable(table) {
var result = "{|" + table.tableProperty.trim() + "\n",
td = table.td;
table.trProperty.forEach(function(e, i) {
if(i || e.trim()) { result += "|-" + e.trim() + "\n"; }
for(var j = 0; j < td[i].length / 2; j++) {
result += td[i][j * 2].slice(-1) + td[i][j * 2 + 1].trim() + ' ' + (td[i][j * 2 + 2] || "\n").slice(-1);
}
});
return result + "|}";
}
function insertCol(table) {
var init = prompt( wgULS("请输入插入列的初始内容:", "請輸入插入列的初始內容:") );
table.td = table.td.map(e => [...e, '|', init]);
return table;
}
function permuteCol(table) {
var order = prompt( wgULS("请输入新的列序。原第一列记为0,依次类推。以半角符号(,)分隔,省略想要删除的列的序号", "請輸入新的列序。原第一列記為0,依此類推。以半形符號(,)分隔,省略想要刪除的列的序號") ).split(',');
if( order.find(e => isNaN(e) || e < 0) ) {
mw.notify("列序输入格式不正确", "列序輸入格式不正確");
return table;
}
table.td = table.td.map(e => {
var tdArray = [];
order.forEach(function(i) {
tdArray.push(e[2 * i] || '|');
tdArray.push(e[2 * i + 1] || '');
});
return tdArray;
});
return table;
}

editBox.wikiEditor('addToToolbar', {
sections: {
'tables': {
type: 'toolbar',
label: '表格'
}
}
});
editBox.wikiEditor('addToToolbar', {
section: 'tables',
groups: {
'tables': {}
}
});
function addButton(text, icon, handler) {
editBox.wikiEditor('addToToolbar', {
section: 'tables',
group: 'tables',
tools: {
ident: {
label: text,
type: 'button',
icon: icon,
action: {
type: 'callback',
execute: function() {
var parts = getSelection(editBox),
table = readTable( parts[1] );
if(!table) {
mw.notify( wgULS("表格不完整或存在错误", "表格不完整或存在錯誤") );
return;
}
editBox.val( parts[0] + handler(table) + parts[2] );
}
}
}
}
});
}
addButton(wgULS("<tr>分行书写", "<tr>分行書寫"),
"//upload.wikimedia.org/wikipedia/commons/c/c0/Knop_onzijdig.png",
parseTable
);
addButton(wgULS("<tr>一行书写", "<tr>一行書寫"),
"//upload.wikimedia.org/wikipedia/commons/8/8e/Button_number_1.png",
parseCompactTable
);
addButton(wgULS("在最右侧插入一列", "在最右側插入一列"),
"//upload.wikimedia.org/wikipedia/commons/6/60/Button_support.png",
function(table) {
return parseCompactTable( insertCol(table) );
}
);
addButton(wgULS("改变列序和/或删除部分列", "改變列序和/或刪除部分列"),
"//upload.wikimedia.org/wikipedia/commons/9/98/Button_oppose.png",
function(table) {
return parseCompactTable( permuteCol(table) );
}
);
});
});

2020年9月11日 (五) 23:55的版本

// 生成JS文件大小
var api = new mw.Api(),
    table = $('table').first(),
    list = table.find('td:first-child a');
table.find('th').first().after( '<th>大小</th>' );
list.each(function() {
    var ele = $(this);
    var title = ele.attr('href').substring(4);
    api.get({ action:'query', formatversion:2, prop:'info', titles:title }).then(function(data) {
        var length = data.query.pages[0].length;
        ele.parent().after('<td>' + length + '</td>');
    });
});