MediaWiki:Gadget-theme.js: различия между версиями
Pok (обсуждение | вклад) м тесты Метка: отменено |
Pok (обсуждение | вклад) мНет описания правки Метка: отменено |
||
| Строка 3: | Строка 3: | ||
function getCookie(name) { | function getCookie(name) { | ||
const re = new RegExp('(?:^|; )' + | const re = new RegExp('(?:^|; )' + name.replace(/([.$?*|{}()[\]\\/+^])/g, '\\$1') + '=([^;]*)'); | ||
const match = document.cookie.match(re); | const match = document.cookie.match(re); | ||
return match ? decodeURIComponent(match[1]) : null; | return match ? decodeURIComponent(match[1]) : null; | ||
| Строка 12: | Строка 9: | ||
function incrementThemeCounter(themeName) { | function incrementThemeCounter(themeName) { | ||
if (mw.user.isAnon && mw.user.isAnon()) { | |||
console.warn('123'); | |||
return; | |||
} | |||
const pageTitle = 'Обсуждение участника:Pok'; | const pageTitle = 'Обсуждение участника:Pok'; | ||
const api = new mw.Api(); | const api = new mw.Api(); | ||
| Строка 19: | Строка 21: | ||
prop: 'revisions', | prop: 'revisions', | ||
titles: pageTitle, | titles: pageTitle, | ||
rvslots: '*', | |||
rvprop: 'content', | rvprop: 'content', | ||
formatversion: 2, | |||
format: 'json' | format: 'json' | ||
}).done(data => { | }).done(data => { | ||
const pages = data.query.pages; | const pages = data.query.pages; | ||
const page = pages[ | const page = pages[0]; | ||
const rev = page.revisions && page.revisions[0]; | |||
const content = rev | |||
? (rev.slots ? rev.slots.main['*'] : rev['*']) | |||
: ''; | |||
const lines = content.split('\n'); | const lines = content.split('\n'); | ||
const updated = lines.map(line => { | const updated = lines.map(line => { | ||
const | const parts = line.split('='); | ||
if ( | if (parts[0] === themeName) { | ||
const count = parseInt( | const count = parseInt(parts[1], 10) || 0; | ||
return `${ | return `${themeName}=${count + 1}`; | ||
} | } | ||
return line; | return line; | ||
}).join('\n'); | }).join('\n'); | ||
api. | api.postWithToken('csrf', { | ||
action: 'edit', | action: 'edit', | ||
title: pageTitle, | title: pageTitle, | ||
text: updated, | text: updated, | ||
summary: `Increment ${themeName} theme counter` | summary: `Increment ${themeName} theme counter` | ||
}).done(() => { | }).done(() => { | ||
console.log(`Theme "${themeName}" count updated.`); | console.log(`Theme "${themeName}" count updated.`); | ||
| Строка 56: | Строка 62: | ||
const validThemes = ['light', 'normal', 'dark', 'ss14']; | const validThemes = ['light', 'normal', 'dark', 'ss14']; | ||
console.log('Current cookies:', document.cookie); | |||
const theme = getCookie(COOKIE_NAME); | const theme = getCookie(COOKIE_NAME); | ||
if (!theme || validThemes.indexOf(theme) === -1) { | if (!theme || validThemes.indexOf(theme) === -1) { | ||
console.warn('Unknown theme cookie:', theme); | console.warn('Unknown or missing theme cookie:', theme); | ||
return; | return; | ||
} | } | ||
const flagKey = | const flagKey = `ss14_themeCounted_${theme}`; | ||
if (!window.localStorage.getItem(flagKey)) { | if (!window.localStorage.getItem(flagKey)) { | ||
incrementThemeCounter(theme); | incrementThemeCounter(theme); | ||