User:Andrybak/Scripts/Special:Preferences ID helper.js

// ==UserScript==

// @name Wikipedia: Special:Preferences ID helper

// @namespace https://andrybak.dev

// @version 1

// @description This userscript helps implement https://en.wikipedia.org/wiki/Template:Myprefs

// @author https://en.wikipedia.org/wiki/User:Andrybak

// @license MIT

// @match https://en.wikipedia.org/wiki/Special:Preferences*

// @require http://code.jquery.com/jquery-3.7.1.min.js

// @grant none

// ==/UserScript==

/*

* How to use:

*

* 1. Go to https://en.wikipedia.org/wiki/Special:Preferences?uselang=en

* 2. Copy output from browser console into an empty spreadsheet into the first column

* 3. Go to https://en.wikipedia.org/wiki/Special:Preferences?uselang=qqx

* 4. Copy output into the second column in the spreadsheet

* 5. Combine the two columns by copy-pasting the first two columns into a plain text editor.

*/

(function() {

'use strict';

const LOG_PREFIX = `[Special:Preferences ID helper]:`;

function error(...toLog) {

console.error(LOG_PREFIX, ...toLog);

}

function warn(...toLog) {

console.warn(LOG_PREFIX, ...toLog);

}

function info(...toLog) {

console.info(LOG_PREFIX, ...toLog);

}

function debug(...toLog) {

console.debug(LOG_PREFIX, ...toLog);

}

function unParenthesize(s) {

if (!s) {

return s;

}

if (s[0] !== '(' || s[s.length-1] !== ')') {

return s;

}

return s.slice(1, s.length - 1);

}

const ID_2_TABNAME = {

'mw-prefsection-personal': "User profile",

'mw-prefsection-rendering': "Appearance",

'mw-prefsection-editing': "Editing",

'mw-prefsection-rc': "Recent changes",

'mw-prefsection-watchlist': "Watchlist",

'mw-prefsection-searchoptions': "Search",

'mw-prefsection-gadgets': "Gadgets",

'mw-prefsection-centralnotice-banners': "Banners",

'mw-prefsection-betafeatures': "Beta features",

'mw-prefsection-echo': "Notifications",

};

function getTabName(id) {

for (let [key, value] of Object.entries(ID_2_TABNAME)) {

if (id.startsWith(key)) {

return value;

}

}

return 'Unknown tab';

}

function leftHandSide(id, text) {

const tabName = getTabName(id);

return `|${tabName}/${text}=`;

}

function rightHandSide(id, text) {

const uiMessageId = unParenthesize(text);

return `{{int:${uiMessageId}}}`;

}

/*

* Example of full text:

* |Recent changes/Display options={{int:prefs-displayrc}}

*/

function generateText(id, text) {

if (document.location.href.includes('uselang=qqx')) {

return rightHandSide(id, text);

} else {

return leftHandSide(id, text);

}

}

function printMyprefsBoilerplate() {

const fieldSets = document.querySelectorAll('fieldset');

const output = [];

for (let i = 0; i < fieldSets.length; i++) {

const fieldSet = fieldSets[i];

const headerText = fieldSet.querySelector('.oo-ui-fieldsetLayout-header .oo-ui-labelElement-label')?.innerText;

const id = fieldSet.id;

output.push(generateText(id, headerText));

}

info(output.join('\n'));

}

/*

function handlePortletClick() {

printMyprefsBoilerplate();

}

function scriptMain() {

const portletLink = mw.util.addPortletLink("p-cactions", "#", "Prefs·IDs", "ca-prefsIdHelperAndrybak", "Help write Template:Myprefs", null, null);

$(portletLink).click(handlePortletClick);

}

function lazyLoadPrefsIdsHelper() {

if ($ == undefined || mw == undefined || mw.loader == undefined || mw.loader.using == undefined) {

// info($, mw, mw?.loader, mw?.loader?.using);

setTimeout(1000, lazyLoadPrefsIdsHelper);

return;

}

$.when(mw.loader.using(['mediawiki.util', 'mediawiki.api']), $.ready).done(scriptMain);

}

lazyLoadPrefsIdsHelper();

*/

printMyprefsBoilerplate(); // do the printing without clicking

})();