User:Polygnotus/Scripts/WikiEditorToolbarHelperConfigurator.js: Difference between revisions

Content deleted Content added
No edit summary
No edit summary
 
(One intermediate revision by the same user not shown)
Line 1:
/**
* WikiEditor Button Configurator
* WikiEditorToolbarHelperConfigurator
* @version 2025-06-07
* PURE JAVASCRIPT CONFIGURATION TOOL - NO BUTTON DEFINITIONS
* Usage: Add ?wikieditorconfig=1 to any Wikipedia URL
*/
Line 14 ⟶ 15:
 
var buttons = [];
var defaults = {
section: 'advanced',
group: 'insert',
type: 'ooui'
};
 
// Create simple interface
function init() {
// Add CSS
var css = [
'#wec-overlay { position: fixed; top: 0; left: 0; width: 100%; height: 100%; background: rgba(0,0,0,0.8); z-index: 10000; overflow: auto; }',
Line 31 ⟶ 38:
'#wec-json { width: 100%; height: 200px; font-family: monospace; font-size: 12px; }'
].join(' ');
 
$('<style>').text(css).appendTo('head');
 
var html = [createInterface();
'<div id="wec-overlay">',loadExistingConfig();
}
'<div id="wec-container">',
 
'<h1 style="text-align: center; color: #0645ad; margin-bottom: 20px;">',
function createInterface() {
'WikiEditor Button Configuration Tool',
var $overlay = $('<div id="wec-overlay">');
'<button onclick="document.getElementById(\'wec-overlay\').remove()" style="float: right; background: none; border: none; font-size: 20px; cursor: pointer;">&times;</button>',
var $container = $('</h1div id="wec-container">',);
 
'<div id="wec-form">',
// '<div class="wec-panel">',Header
var $header = $('<h3>Button Configuration</h3h1>',).css({
textAlign: 'center',
'<div class="wec-field"><label class="wec-label">Button ID*</label><input class="wec-input" id="wec-id"></div>',
color: '#0645ad',
'<div class="wec-field"><label class="wec-label">Label*</label><input class="wec-input" id="wec-label"></div>',
marginBottom: '20px'
'<div class="wec-field"><label class="wec-label">Tooltip</label><input class="wec-input" id="wec-tooltip"></div>',
}).text('WikiEditor Button Configuration Tool');
'<div class="wec-field"><label class="wec-label">Type</label><select class="wec-input" id="wec-type">',
 
'<option value="ooui">OOUI</option><option value="traditional">Traditional</option></select></div>',
var $closeBtn = $('<button>').text('×').css({
'<div class="wec-field"><label class="wec-label">Icon</label><input class="wec-input" id="wec-icon"></div>',
position: 'absolute',
'<div class="wec-field"><label class="wec-label">Insert Before</label><textarea class="wec-input" id="wec-before" rows="3"></textarea></div>',
top: '20px',
'<div class="wec-field"><label class="wec-label">Sample Text</label><input class="wec-input" id="wec-sample"></div>',
right: '20px',
'<div class="wec-field"><label class="wec-label">Insert After</label><textarea class="wec-input" id="wec-after" rows="3"></textarea></div>',
background: 'none',
'<div class="wec-field"><label><input type="checkbox" id="wec-prompt"> Has Prompt</label></div>',
border: 'none',
'<div class="wec-field"><label class="wec-label">Prompt Message</label><input class="wec-input" id="wec-promptmsg"></div>',
fontSize: '20px',
'<div class="wec-field"><label><input type="checkbox" id="wec-summary"> Auto Summary</label></div>',
cursor: 'pointer'
'<div class="wec-field"><label class="wec-label">Summary Text</label><input class="wec-input" id="wec-summarytext"></div>',
}).click(function() {
'<div class="wec-field"><label class="wec-label">Namespaces (comma-separated)</label><input class="wec-input" id="wec-ns" placeholder="3,1,0"></div>',
$overlay.remove();
'<div><button class="wec-btn" onclick="wecAddButton()">Add Button</button>',
});
'<button class="wec-btn" onclick="wecClear()">Clear</button>',
 
'<button class="wec-btn" onclick="wecExample()">Example</button></div>',
// '</div>',Form
var $form = $('<div classid="wec-panelform">',);
$form.append(createFormPanel(), createListPanel());
'<h3>Button List</h3>',
 
'<div class="wec-list" id="wec-list">No buttons added yet</div>',
// JSON section
'<button class="wec-btn wec-btn-danger" onclick="wecClearAll()">Clear All</button>',
var $jsonSection = '</div>',createJsonSection();
'</div>',
'<h3>Generated JSON</h3>',
'<textarea id="wec-json">Click Generate JSON</textarea>',
'<div style="margin-top: 10px;">',
'<button class="wec-btn" onclick="wecGenerate()">Generate JSON</button>',
'<button class="wec-btn" onclick="wecCopy()">Copy</button>',
'<button class="wec-btn" onclick="wecSave()">Save to User Page</button>',
'</div>',
'</div>',
'</div>'
].join('');
 
$('body')container.append(html$header, $closeBtn, $form, $jsonSection);
wecLoad$overlay.append($container);
$('body').append($overlay);
}
 
function createFormPanel() {
// Global functions
var $panel = $('<div class="wec-panel">');
window.wecAddButton = function() {
$panel.append($('<h3>').text('Button Configuration'));
var data = {
id: $('#wec-id').val(),
label: $('#wec-label').val(),
type: $('#wec-type').val()
};
 
// 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;
}
 
function createListPanel() {
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;
}
 
function createJsonSection() {
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;
}
 
function createField(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;
}
 
function createSelectField(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;
}
 
function createCheckField(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
function addButton() {
var data = collectFormData();
if (!data.id || !data.label) {
alert('ID and Label are required!');
return;
}
 
if (buttons.some(function(b) { return b.id === data.id; })) {
alert('Button ID must be unique!');
return;
}
 
buttons.push(data);
var fields = ['tooltip', 'icon', 'before', 'sample', 'after', 'promptmsg', 'summarytext', 'ns'];
fields.forEachupdateButtonList(function(field) {;
var val = $('#wec-' + field).valclearForm();
}
if (val) {
 
if (field === 'before') data.insertBefore = val;
function collectFormData() {
else if (field === 'sample') data.sampleText = val;
else if (field === 'after')var data.insertAfter = val;{
id: else if $(field === 'ns#wec-id') {.val(),
label: data.namespaces = val.split$(',#wec-label').mapval(function(n) {,
type: $('#wec-type').val()
};
 
// Optional fields
var optionalFields = {
'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) {
var value = $('#' + fieldId).val();
if (value) {
var key = optionalFields[fieldId];
if (key === 'namespaces') {
data[key] = value.split(',').map(function(n) {
return parseInt(n.trim());
}).filter(function(n) { return !isNaN(n); });
} else {
data[fieldkey] = valvalue;
}
}
});
 
if// Handle ($('#wec-prompt').is(':checked') && data.promptmsg) {
if data.($('#wec-prompt = { type: 'simple).is(', message:checked') && data.promptmsgpromptMessage) };{
delete data.promptmsg;prompt = {
type: 'simple',
message: data.promptMessage
};
delete data.promptMessage;
}
 
// Handle auto summary
if ($('#wec-summary').is(':checked') && data.summarytext) {
if ($('#wec-summary').is(':checked') && data.summaryText) {
data.autoSummary = { summary: data.summarytext, position: 'append' };
delete data.summarytext;autoSummary = {
summary: data.summaryText,
position: 'append'
};
delete data.summaryText;
}
 
buttons.push(return data);
}
wecUpdateList();
wecClear();
};
 
window.wecClearfunction = functionupdateButtonList() {
var $list = $('#wec-form input, #wec-form textarea, #wec-form select').val('list');
$list.empty();
$('#wec-form input[type="checkbox"]').prop('checked', false);
$('#wec-type').val('ooui');
};
 
window.wecClearAll = function() {
if (confirm('Clear all buttons?')) {
buttons = [];
wecUpdateList();
}
};
 
window.wecUpdateList = function() {
var list = $('#wec-list');
if (buttons.length === 0) {
$list.text('No buttons added yet');
return;
}
 
list.empty();
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);
wecUpdateListupdateButtonList();
}
});
$item.append($title, $type, $('<br>'), $deleteBtn);
$list.append($item);
});
};
 
window.wecDelete = function(index) {
// This function is no longer needed since we use jQuery click handlers
// But keeping it for backwards compatibility
if (confirm('Delete button?')) {
buttons.splice(index, 1);
wecUpdateList();
}
};
 
window.wecGeneratefunction = functionclearForm() {
$('input, select, textarea').each(function() {
// Process buttons to escape newlines for JSON storage
if ($(this).attr('type') === 'checkbox') {
var processedButtons = buttons.map(function(btn) {
var processed = JSON.parse $(JSONthis).stringifyprop(btn)'checked', false); // Deep clone
if} (processed.insertBefore)else {
processed.insertBefore = processed.insertBefore$(this).replaceval(/\n/g, '\\n');
}
if (processed.insertAfter) {
processed.insertAfter = processed.insertAfter.replace(/\n/g, '\\n');
}
if (processed.sampleText) {
processed.sampleText = processed.sampleText.replace(/\n/g, '\\n');
}
return processed;
});
$('#wec-type').val('ooui');
}
 
function clearAll() {
if (confirm('Clear all buttons?')) {
buttons = [];
updateButtonList();
}
}
 
function generateJson() {
var config = {
defaults: {defaults,
buttons: buttons.map(function(btn) section: 'advanced',{
group:// 'insert',Escape newlines for JSON storage
type:var 'ooui',processed = JSON.parse(JSON.stringify(btn));
['insertBefore', 'insertAfter', 'sampleText'].forEach(function(field) {
autoSummary: { position: 'append', delimiter: '; ' }
}, if (processed[field]) {
processed[field] = processed[field].replace(/\n/g, '\\n');
buttons: processedButtons
}
});
return processed;
})
};
 
$('#wec-json').val(JSON.stringify(config, null, 2));
};
 
window.wecCopyfunction = functioncopyJson() {
var textarea = document.getElementById('wec-json');
textarea.select();
try {
document.execCommand('copy');
alert('Copied to clipboard!');
} catch (e) {
alert('Copy failed. Please select and copy manually.');
}
};
 
window.wecSavefunction = functionsaveToUserPage() {
var username = mw.config.get('wgUserName');
if (!username) {
alert('MustYou must be logged in to save.');
return;
}
Line 227 ⟶ 332:
var json = $('#wec-json').val();
if (!json || json.indexOf('{') !== 0) {
alert('GeneratePlease generate JSON first!');
return;
}
Line 234 ⟶ 339:
api.postWithToken('csrf', {
action: 'edit',
title: 'User:' + username + '/Data/WikiEditorButtonsWikiEditorToolbarConfig.json',
text: json,
summary: 'Updated WikiEditor buttonsbutton configuration',
contentmodel: 'json'
}).done(function() {
alert('SavedConfiguration saved successfully!');
}).fail(function() {
alert('Save failed. Please try again.');
});
};
 
window.wecLoadfunction = functionloadExistingConfig() {
var username = mw.config.get('wgUserName');
if (!username) return;
Line 252 ⟶ 357:
api.get({
action: 'query',
titles: 'User:' + username + '/Data/WikiEditorButtonsWikiEditorToolbarConfig.json',
prop: 'revisions',
rvprop: 'content',
Line 262 ⟶ 367:
var content = data.query.pages[0].revisions[0].slots.main.content;
var config = JSON.parse(content);
if (config.buttons) {
// ProcessUnescape buttonsnewlines tofor handle escaped newlinesediting
buttons = config.buttons.map(function(btn) {
if['insertBefore', (btn'insertAfter', 'sampleText'].insertBeforeforEach(function(field) {
btn.insertBefore =if (btn.insertBefore.replace(/\\n/g,[field]) '\n');{
} btn[field] = btn[field].replace(/\\n/g, '\n');
if (btn.insertAfter) { }
btn.insertAfter = btn.insertAfter.replace(/\\n/g, '\n'});
}
if (btn.sampleText) {
btn.sampleText = btn.sampleText.replace(/\\n/g, '\n');
}
return btn;
});
wecUpdateListupdateButtonList();
}
 
if (config.defaults) {
defaults = config.defaults;
}
} catch (e) {
console.warn('Could not parse existing configuration:', e);
}
}
});
};
 
window.wecExample = function() {
buttons = [{
id: 'welcome-template',
label: 'Welcome',
tooltip: 'Welcome new user',
icon: 'userAvatar',
type: 'ooui',
insertBefore: '== Welcome! ==
 
Hi Polygnotus/Scripts! I noticed [[Special:Contributions/Polygnotus/Scripts|your contributions]] and wanted to welcome you to the Wikipedia community. I hope you like it here and decide to stay.
 
As you get started, you may find this short tutorial helpful:
 
{{Clickable button|Help:Introduction|Learn more about editing|class=mw-ui-progressive|style=margin-left: 1.6em;}}
 
Alternatively, the [[Wikipedia:Contributing to Wikipedia|contributing to Wikipedia]] page covers the same topics.
 
If you have any questions, we have a friendly space where experienced editors can help you here:
 
{{Clickable button|Wikipedia:Teahouse|Get help at the Teahouse|style=margin-left: 1.6em;}}
 
If you are not sure where to help out, you can find a task here:
 
{{Clickable button|Wikipedia:Task Center|Volunteer at the Task Center|style=margin-left: 1.6em;}}
 
Happy editing! <!-- Template:Welcome--> [[User:Polygnotus|Polygnotus]] ([[User talk:Polygnotus|talk]]) 07:34, 7 June 2025 (UTC)',
autoSummary: { summary: 'Welcome new user', position: 'replace' },
namespaces: [3]
}, {
id: 'refideas-button',
label: 'Ref Ideas',
tooltip: 'Add reference ideas template',
icon: 'lightbulb',
type: 'ooui',
insertBefore: '{{refideas\n|1=\n}}',
autoSummary: { summary: 'Added {{refideas}} template' },
namespaces: [1]
}, {
id: 'wp-policy-ref',
label: 'WP Policy',
tooltip: 'Reference Wikipedia policy',
icon: 'articles',
type: 'ooui',
prompt: {
type: 'simple',
message: 'Enter policy abbreviation (e.g., NPOV, V, RS):',
defaultValue: 'NPOV'
},
insertBefore: '== [[WP:{input}]] ==\nPlease read [[WP:{input}]], thanks! [[User:Polygnotus|Polygnotus]] ([[User talk:Polygnotus|talk]]) 07:34, 7 June 2025 (UTC)',
placeholderPattern: '{input}',
autoSummary: {
summary: 'Referenced [[WP:{input}]]',
usePlaceholder: true,
placeholderPattern: '{input}'
},
namespaces: [3]
}];
wecUpdateList();
alert('Example loaded!');
};
 
// Initialize