LLWiki正在建设中,欢迎加入我们!
User:Bhsd/tool.js
跳转到导航
跳转到搜索
注意:在保存之后,您可能需要清除浏览器缓存才能看到所作出的变更的影响。
- Firefox或Safari:按住Shift的同时单击刷新,或按Ctrl-F5或Ctrl-R(Mac为⌘-R)
- Google Chrome:按Ctrl-Shift-R(Mac为⌘-Shift-R)
- Internet Explorer:按住Ctrl的同时单击刷新,或按Ctrl-F5
- Opera:前往菜单 → 设置(Mac为Opera → Preferences),然后隐私和安全 → 清除浏览数据 → 缓存的图片和文件。
/* 生成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>'); }); }); */ // 引自[[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]*\|}$/.test(table) ) { return false; } table = table.substring(2, table.length - 2).trim(); var tableProperty = table.match(/^([\s\S]*?)\n/)[1], tbody = table.substring( tableProperty.length ).trim(), tr = tbody.split('|-').map(e => e.trim()), trProperty = tr.map(e => e.match(/^([\s\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(tableStruct) { var result = "{|" + tableStruct.tableProperty.trim() + "\n", td = tableStruct.td; tableStruct.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] + "\n"; } }); return result + "|}"; } function parseCompactTable(tableStruct) { var result = "{|" + tableStruct.tableProperty.trim() + "\n", td = tableStruct.td; tableStruct.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] + (td[i][j * 2 + 2] || "\n").slice(-1); } }); return result + "|}"; } 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: handler } } } }); } addButton(wgULS("<tr>分行书写", "<tr>分行書寫"), "//upload.wikimedia.org/wikipedia/commons/c/c0/Knop_onzijdig.png", function() { var parts = getSelection(editBox), newTable = parseTable( readTable( parts[1] ) ); editBox.val( parts[0] + newTable + parts[2] ); }); addButton(wgULS("<tr>一行书写", "<tr>一行書寫"), "//upload.wikimedia.org/wikipedia/commons/8/8e/Button_number_1.png", function() { var parts = getSelection(editBox), newTable = parseCompactTable( readTable( parts[1] ) ); editBox.val( parts[0] + newTable + parts[2] ); }); });