User:Polygnotus/Scripts/FavouriteTemplates3.js
Appearance
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.
This code will be executed when previewing this page.
Documentation for this user script can be added at User:Polygnotus/Scripts/FavouriteTemplates3.
// <nowiki>
async function getFavoriteTemplates() {
try {
const apiUrl = mw.config.get('wgScriptPath') + '/api.php';
const params = {
action: 'query',
meta: 'userinfo',
uiprop: 'options',
format: 'json'
};
const response = await $.get(apiUrl, params);
if (response.query && response.query.userinfo && response.query.userinfo.options) {
const options = response.query.userinfo.options;
// Get the favorite templates
const favoriteTemplates = options['templatedata-favorite-templates'];
if (favoriteTemplates) {
console.log('Favorite Templates (Page IDs):', favoriteTemplates);
// Parse as JSON array
let templateIds = [];
if (typeof favoriteTemplates === 'string') {
try {
templateIds = JSON.parse(favoriteTemplates);
} catch (e) {
console.log('Could not parse favorites as JSON');
return;
}
} else {
templateIds = favoriteTemplates;
}
console.log('Template IDs:', templateIds);
// Convert IDs to template names
if (templateIds.length > 0) {
const templateNames = await getTemplateNames(templateIds);
console.log('Template Names:', templateNames);
return templateNames;
}
return templateIds;
} else {
console.log('No favorite templates found.');
return [];
}
}
} catch (error) {
console.error('Error retrieving favorite templates:', error);
}
}
// Function to convert Page IDs to template names
async function getTemplateNames(pageIds) {
try {
const apiUrl = mw.config.get('wgScriptPath') + '/api.php';
// Convert array to pipe-separated string for the API
const pageIdsString = pageIds.join('|');
const params = {
action: 'query',
pageids: pageIdsString,
format: 'json'
};
const response = await $.get(apiUrl, params);
if (response.query && response.query.pages) {
const pages = response.query.pages;
const templateNames = [];
console.log('\n=== Template Details ===');
for (const pageId of pageIds) {
const page = pages[pageId];
if (page && page.title) {
console.log(`ID ${pageId}: ${page.title}`);
templateNames.push({
id: pageId,
title: page.title,
namespace: page.ns
});
} else {
console.log(`ID ${pageId}: Template not found`);
templateNames.push({
id: pageId,
title: 'Unknown',
namespace: null
});
}
}
return templateNames;
}
} catch (error) {
console.error('Error retrieving template names:', error);
return [];
}
}
// Execute the script
getFavoriteTemplates();
// </nowiki>