Jump to content
Wikipedia The Free Encyclopedia

User:Polygnotus/Scripts/WikiEditorToolbarHelperConfigurator.js

From Wikipedia, the free encyclopedia
The printable version is no longer supported and may have rendering errors. Please update your browser bookmarks and please use the default browser print function instead.
Code that you insert on this page could contain malicious content capable of compromising your account. If you import a script from another page with "importScript", "mw.loader.load", "iusc", or "lusc", take note that this causes you to dynamically load a remote script, which could be changed by others. Editors are responsible for all edits and actions they perform, including by scripts. User scripts are not centrally supported and may malfunction or become inoperable due to software changes. A guide to help you find broken scripts is available. If you are unsure whether code you are adding to this page is safe, you can ask at the appropriate village pump.
This code will be executed when previewing this page.
Note: After saving, you have to bypass your browser's cache to see the changes. Google Chrome, Firefox, Microsoft Edge and Safari: Hold down the ⇧ Shift key and click the Reload toolbar button. For details and instructions about other browsers, see Wikipedia:Bypass your cache.
 /**
  * WikiEditor Button Configurator
  * @version 2025年06月07日
  * PURE JAVASCRIPT CONFIGURATION TOOL - NO BUTTON DEFINITIONS
  * Usage: Add ?wikieditorconfig=1 to any Wikipedia URL
  */
 (function($,mw){
 'use strict';

 // Check for configuration parameter
 varurlParams=newURLSearchParams(window.location.search);
 if(urlParams.get('wikieditorconfig')!=='1'){
 return;
 }

 varbuttons=[];
 vardefaults={
 section:'advanced',
 group:'insert',
 type:'ooui'
 };

 // Create interface
 functioninit(){
 // Add CSS
 varcss=[
 '#wec-overlay { position: fixed; top: 0; left: 0; width: 100%; height: 100%; background: rgba(0,0,0,0.8); z-index: 10000; overflow: auto; }',
 '#wec-container { max-width: 900px; margin: 20px auto; background: #fff; border-radius: 8px; padding: 20px; }',
 '#wec-form { display: grid; grid-template-columns: 1fr 1fr; gap: 20px; margin-bottom: 20px; }',
 '.wec-panel { background: #f8f9fa; padding: 15px; border-radius: 5px; }',
 '.wec-field { margin-bottom: 10px; }',
 '.wec-label { display: block; margin-bottom: 5px; font-weight: bold; }',
 '.wec-input { width: 100%; padding: 5px; border: 1px solid #ccc; border-radius: 3px; }',
 '.wec-btn { background: #0645ad; color: #fff; border: none; padding: 8px 12px; margin: 0 5px 5px 0; border-radius: 3px; cursor: pointer; }',
 '.wec-btn-danger { background: #d33; }',
 '.wec-list { max-height: 300px; overflow-y: auto; border: 1px solid #ccc; padding: 10px; background: #fff; }',
 '.wec-item { border: 1px solid #ddd; padding: 8px; margin-bottom: 5px; border-radius: 3px; }',
 '#wec-json { width: 100%; height: 200px; font-family: monospace; font-size: 12px; }'
 ].join(' ');
 $('<style>').text(css).appendTo('head');

 createInterface();
 loadExistingConfig();
 }

 functioncreateInterface(){
 var$overlay=$('<div id="wec-overlay">');
 var$container=$('<div id="wec-container">');

 // Header
 var$header=$('<h1>').css({
 textAlign:'center',
 color:'#0645ad',
 marginBottom:'20px'
 }).text('WikiEditor Button Configuration Tool');

 var$closeBtn=$('<button>').text(×ばつ').css({
 position:'absolute',
 top:'20px',
 right:'20px',
 background:'none',
 border:'none',
 fontSize:'20px',
 cursor:'pointer'
 }).click(function(){
 $overlay.remove();
 });

 // Form
 var$form=$('<div id="wec-form">');
 $form.append(createFormPanel(),createListPanel());

 // JSON section
 var$jsonSection=createJsonSection();

 $container.append($header,$closeBtn,$form,$jsonSection);
 $overlay.append($container);
 $('body').append($overlay);
 }

 functioncreateFormPanel(){
 var$panel=$('<div class="wec-panel">');
 $panel.append($('<h3>').text('Button Configuration'));

 // Form fields
 $panel.append(
 createField('Button ID*','text','wec-id'),
 createField('Label*','text','wec-label'),
 createField('Tooltip','text','wec-tooltip'),
 createSelectField('Type','wec-type',[
 {value:'ooui',text:'OOUI'},
 {value:'traditional',text:'Traditional'},
 {value:'element',text:'Element'}
 ]),
 createField('Icon','text','wec-icon'),
 createField('Insert Before','textarea','wec-before'),
 createField('Sample Text','text','wec-sample'),
 createField('Insert After','textarea','wec-after'),
 createCheckField('Has Prompt','wec-prompt'),
 createField('Prompt Message','text','wec-promptmsg'),
 createCheckField('Auto Summary','wec-summary'),
 createField('Summary Text','text','wec-summarytext'),
 createField('Namespaces (comma-separated)','text','wec-ns')
 );

 // Buttons
 var$buttons=$('<div>').css({marginTop:'15px'});
 $buttons.append(
 $('<button class="wec-btn">').text('Add Button').click(addButton),
 $('<button class="wec-btn">').text('Clear Form').click(clearForm)
 );

 $panel.append($buttons);
 return$panel;
 }

 functioncreateListPanel(){
 var$panel=$('<div class="wec-panel">');
 $panel.append(
 $('<h3>').text('Button List'),
 $('<div class="wec-list" id="wec-list">').text('No buttons added yet'),
 $('<div>').css({marginTop:'15px'}).append(
 $('<button class="wec-btn wec-btn-danger">').text('Clear All').click(clearAll)
 )
 );
 return$panel;
 }

 functioncreateJsonSection(){
 var$section=$('<div>').css({
 borderTop:'1px solid #ccc',
 paddingTop:'20px'
 });

 $section.append(
 $('<h3>').text('Generated JSON Configuration'),
 $('<textarea id="wec-json">').val('Configuration will appear here'),
 $('<div>').css({marginTop:'10px'}).append(
 $('<button class="wec-btn">').text('Generate JSON').click(generateJson),
 $('<button class="wec-btn">').text('Copy').click(copyJson),
 $('<button class="wec-btn">').text('Save to User Page').click(saveToUserPage)
 )
 );

 return$section;
 }

 functioncreateField(label,type,id){
 var$field=$('<div class="wec-field">');
 var$label=$('<label class="wec-label">').text(label);
 var$input=type==='textarea'
 ?$('<textarea class="wec-input" rows="3">').attr('id',id)
 :$('<input class="wec-input">').attr({type:type,id:id});

 $field.append($label,$input);
 return$field;
 }

 functioncreateSelectField(label,id,options){
 var$field=$('<div class="wec-field">');
 var$label=$('<label class="wec-label">').text(label);
 var$select=$('<select class="wec-input">').attr('id',id);

 options.forEach(function(opt){
 $select.append($('<option>').val(opt.value).text(opt.text));
 });

 $field.append($label,$select);
 return$field;
 }

 functioncreateCheckField(label,id){
 var$field=$('<div class="wec-field">');
 var$wrapper=$('<label>').css({display:'flex',alignItems:'center',gap:'5px'});
 var$checkbox=$('<input>').attr({type:'checkbox',id:id});
 $wrapper.append($checkbox,$('<span>').text(label));
 $field.append($wrapper);
 return$field;
 }

 // Button management functions
 functionaddButton(){
 vardata=collectFormData();
 if(!data.id||!data.label){
 alert('ID and Label are required!');
 return;
 }

 if(buttons.some(function(b){returnb.id===data.id;})){
 alert('Button ID must be unique!');
 return;
 }

 buttons.push(data);
 updateButtonList();
 clearForm();
 }

 functioncollectFormData(){
 vardata={
 id:$('#wec-id').val(),
 label:$('#wec-label').val(),
 type:$('#wec-type').val()
 };

 // Optional fields
 varoptionalFields={
 'wec-tooltip':'tooltip',
 'wec-icon':'icon',
 'wec-before':'insertBefore',
 'wec-sample':'sampleText',
 'wec-after':'insertAfter',
 'wec-promptmsg':'promptMessage',
 'wec-summarytext':'summaryText',
 'wec-ns':'namespaces'
 };

 Object.keys(optionalFields).forEach(function(fieldId){
 varvalue=$('#'+fieldId).val();
 if(value){
 varkey=optionalFields[fieldId];
 if(key==='namespaces'){
 data[key]=value.split(',').map(function(n){
 returnparseInt(n.trim());
 }).filter(function(n){return!isNaN(n);});
 }else{
 data[key]=value;
 }
 }
 });

 // Handle prompt
 if($('#wec-prompt').is(':checked')&&data.promptMessage){
 data.prompt={
 type:'simple',
 message:data.promptMessage
 };
 deletedata.promptMessage;
 }

 // Handle auto summary
 if($('#wec-summary').is(':checked')&&data.summaryText){
 data.autoSummary={
 summary:data.summaryText,
 position:'append'
 };
 deletedata.summaryText;
 }

 returndata;
 }

 functionupdateButtonList(){
 var$list=$('#wec-list');
 $list.empty();

 if(buttons.length===0){
 $list.text('No buttons added yet');
 return;
 }

 buttons.forEach(function(btn,i){
 var$item=$('<div class="wec-item">');
 var$title=$('<strong>').text(btn.label+' ('+btn.id+')');
 var$type=$('<span>').text(' - '+btn.type).css('color','#666');
 var$deleteBtn=$('<button class="wec-btn wec-btn-danger">').text('Delete').click(function(){
 if(confirm('Delete this button?')){
 buttons.splice(i,1);
 updateButtonList();
 }
 });

 $item.append($title,$type,$('<br>'),$deleteBtn);
 $list.append($item);
 });
 }

 functionclearForm(){
 $('input, select, textarea').each(function(){
 if($(this).attr('type')==='checkbox'){
 $(this).prop('checked',false);
 }else{
 $(this).val('');
 }
 });
 $('#wec-type').val('ooui');
 }

 functionclearAll(){
 if(confirm('Clear all buttons?')){
 buttons=[];
 updateButtonList();
 }
 }

 functiongenerateJson(){
 varconfig={
 defaults:defaults,
 buttons:buttons.map(function(btn){
 // Escape newlines for JSON storage
 varprocessed=JSON.parse(JSON.stringify(btn));
 ['insertBefore','insertAfter','sampleText'].forEach(function(field){
 if(processed[field]){
 processed[field]=processed[field].replace(/\n/g,'\\n');
 }
 });
 returnprocessed;
 })
 };

 $('#wec-json').val(JSON.stringify(config,null,2));
 }

 functioncopyJson(){
 vartextarea=document.getElementById('wec-json');
 textarea.select();
 try{
 document.execCommand('copy');
 alert('Copied to clipboard!');
 }catch(e){
 alert('Copy failed. Please select and copy manually.');
 }
 }

 functionsaveToUserPage(){
 varusername=mw.config.get('wgUserName');
 if(!username){
 alert('You must be logged in to save.');
 return;
 }

 varjson=$('#wec-json').val();
 if(!json||json.indexOf('{')!==0){
 alert('Please generate JSON first!');
 return;
 }

 varapi=newmw.Api();
 api.postWithToken('csrf',{
 action:'edit',
 title:'User:'+username+'/Data/WikiEditorToolbarConfig.json',
 text:json,
 summary:'Updated WikiEditor button configuration',
 contentmodel:'json'
 }).done(function(){
 alert('Configuration saved successfully!');
 }).fail(function(){
 alert('Save failed. Please try again.');
 });
 }

 functionloadExistingConfig(){
 varusername=mw.config.get('wgUserName');
 if(!username)return;

 varapi=newmw.Api();
 api.get({
 action:'query',
 titles:'User:'+username+'/Data/WikiEditorToolbarConfig.json',
 prop:'revisions',
 rvprop:'content',
 rvslots:'main',
 formatversion:2
 }).done(function(data){
 if(data.query&&data.query.pages&&data.query.pages[0]&&!data.query.pages[0].missing){
 try{
 varcontent=data.query.pages[0].revisions[0].slots.main.content;
 varconfig=JSON.parse(content);

 if(config.buttons){
 // Unescape newlines for editing
 buttons=config.buttons.map(function(btn){
 ['insertBefore','insertAfter','sampleText'].forEach(function(field){
 if(btn[field]){
 btn[field]=btn[field].replace(/\\n/g,'\n');
 }
 });
 returnbtn;
 });
 updateButtonList();
 }

 if(config.defaults){
 defaults=config.defaults;
 }
 }catch(e){
 console.warn('Could not parse existing configuration:',e);
 }
 }
 });
 }

 // Initialize
 $(function(){
 init();
 });

 }(jQuery,mediaWiki));

AltStyle によって変換されたページ (->オリジナル) /