MediaWiki:Gadget-theme.js: различия между версиями
Pok (обсуждение | вклад) мНет описания правки |
Pok (обсуждение | вклад) м тесты Метка: отменено |
||
| Строка 1: | Строка 1: | ||
;(function($, mw) { | |||
'use strict'; | |||
function getCookie(name) { | |||
const re = new RegExp('(?:^|; )' + | |||
name.replace(/([.$?*|{}()[\]\\/+^])/g, '\\$1') + | |||
'=([^;]*)' | |||
); | |||
const match = document.cookie.match(re); | |||
return match ? decodeURIComponent(match[1]) : null; | |||
} | |||
function incrementThemeCounter(themeName) { | |||
const pageTitle = 'Обсуждение участника:Pok'; | |||
const api = new mw.Api(); | |||
api.get({ | |||
action: 'query', | |||
prop: 'revisions', | |||
titles: pageTitle, | |||
rvprop: 'content', | |||
format: 'json' | |||
}).done(data => { | |||
const pages = data.query.pages; | |||
const page = pages[Object.keys(pages)[0]]; | |||
let content = page.revisions[0]['*'] || ''; | |||
const lines = content.split('\n'); | |||
const updated = lines.map(line => { | |||
const [name, cnt] = line.split('='); | |||
if (name === themeName) { | |||
const count = parseInt(cnt, 10) || 0; | |||
return `${name}=${count + 1}`; | |||
} | |||
return line; | |||
}).join('\n'); | |||
api.post({ | |||
action: 'edit', | |||
title: pageTitle, | |||
text: updated, | |||
summary: `Increment ${themeName} theme counter`, | |||
token: mw.user.tokens.get('editToken') | |||
}).done(() => { | |||
console.log(`Theme "${themeName}" count updated.`); | |||
}).fail(err => { | |||
console.error('Edit failed:', err); | |||
}); | |||
}).fail(err => { | |||
console.error('Read failed:', err); | |||
}); | |||
} | |||
$(function() { | |||
const COOKIE_NAME = 'ss14_wikiTheme'; | |||
const validThemes = ['light', 'normal', 'dark', 'ss14']; | |||
const theme = getCookie(COOKIE_NAME); | |||
if (!theme || validThemes.indexOf(theme) === -1) { | |||
console.warn('Unknown theme cookie:', theme); | |||
return; | |||
} | |||
const flagKey = 'ss14_themeCounted'; | |||
if (!window.localStorage.getItem(flagKey)) { | |||
incrementThemeCounter(theme); | |||
window.localStorage.setItem(flagKey, '1'); | |||
} | |||
}); | |||
})(jQuery, mediaWiki); | |||
;(function($, mw){ | ;(function($, mw){ | ||
const COOKIE = 'ss14_wikiTheme'; | const COOKIE = 'ss14_wikiTheme'; | ||