MediaWiki:Gadget-theme.js: различия между версиями
Pok (обсуждение | вклад) мНет описания правки Метка: отменено |
Pok (обсуждение | вклад) мНет описания правки Метка: отменено |
||
| Строка 9: | Строка 9: | ||
function incrementThemeCounter(themeName) { | function incrementThemeCounter(themeName) { | ||
const pageTitle = 'Обсуждение участника:Pok'; | const pageTitle = 'Обсуждение участника:Pok'; | ||
const validThemes = ['light', 'normal', 'dark', 'ss14']; | const validThemes = ['light', 'normal', 'dark', 'ss14']; | ||
| Строка 29: | Строка 24: | ||
const page = data.query.pages[0]; | const page = data.query.pages[0]; | ||
const rev = page.revisions && page.revisions[0]; | const rev = page.revisions && page.revisions[0]; | ||
let content = rev | let content = rev ? (rev.slots ? rev.slots.main['*'] : rev['*']) : ''; | ||
const counts = {}; | const counts = {}; | ||
validThemes.forEach(t => counts[t] = 0); | validThemes.forEach(t => counts[t] = 0); | ||
if ( | if (content.trim()) { | ||
content.split('\n').forEach(line => { | content.split('\n').forEach(line => { | ||
const [name, cnt] = line.split('='); | const [name, cnt] = line.split('='); | ||
if (name | if (validThemes.includes(name)) { | ||
counts[name] = parseInt(cnt, 10) || 0; | counts[name] = parseInt(cnt, 10) || 0; | ||
} | } | ||
| Строка 46: | Строка 39: | ||
counts[themeName] = (counts[themeName] || 0) + 1; | counts[themeName] = (counts[themeName] || 0) + 1; | ||
const | const updatedText = validThemes.map(t => `${t}=${counts[t]}`).join('\n'); | ||
api.postWithToken('csrf', { | api.postWithToken('csrf', { | ||
action: 'edit', | action: 'edit', | ||
title: pageTitle, | title: pageTitle, | ||
text: | text: updatedText, | ||
summary: `Increment ${themeName} theme counter` | summary: `Increment ${themeName} theme counter`, | ||
createonly: false | |||
}).done(() => { | }).done(() => { | ||
console.log(`Theme "${themeName}" count updated.`); | console.log(`Theme "${themeName}" count updated.`); | ||
| Строка 67: | Строка 61: | ||
const validThemes = ['light', 'normal', 'dark', 'ss14']; | const validThemes = ['light', 'normal', 'dark', 'ss14']; | ||
console.log(' | console.log('Loaded theme-counter script. Cookies:', document.cookie); | ||
const theme = getCookie(COOKIE_NAME); | const theme = getCookie(COOKIE_NAME); | ||
if (!theme || !validThemes.includes(theme)) { | |||
if (!theme || validThemes. | |||
console.warn('Unknown or missing theme cookie:', theme); | console.warn('Unknown or missing theme cookie:', theme); | ||
return; | return; | ||