|
|
| Строка 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 validThemes = ['light', 'normal', 'dark', 'ss14'];
| |
| const api = new mw.Api();
| |
|
| |
| api.get({
| |
| action: 'query',
| |
| prop: 'revisions',
| |
| titles: pageTitle,
| |
| rvslots: '*',
| |
| rvprop: 'content',
| |
| formatversion: 2,
| |
| format: 'json'
| |
| }).done(data => {
| |
| const page = data.query.pages[0];
| |
| const rev = page.revisions && page.revisions[0];
| |
| let content = rev ? (rev.slots ? rev.slots.main['*'] : rev['*']) : '';
| |
|
| |
| const counts = {};
| |
| validThemes.forEach(t => counts[t] = 0);
| |
| if (content.trim()) {
| |
| content.split('\n').forEach(line => {
| |
| const [name, cnt] = line.split('=');
| |
| if (validThemes.includes(name)) {
| |
| counts[name] = parseInt(cnt, 10) || 0;
| |
| }
| |
| });
| |
| }
| |
|
| |
| counts[themeName] = (counts[themeName] || 0) + 1;
| |
|
| |
| const updatedText = validThemes.map(t => `${t}=${counts[t]}`).join('\n');
| |
|
| |
| api.postWithToken('csrf', {
| |
| action: 'edit',
| |
| title: pageTitle,
| |
| text: updatedText,
| |
| summary: `Increment ${themeName} theme counter`,
| |
| createonly: false
| |
| }).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'];
| |
|
| |
| console.log('Loaded theme-counter script. Cookies:', document.cookie);
| |
| const theme = getCookie(COOKIE_NAME);
| |
| if (!theme || !validThemes.includes(theme)) {
| |
| console.warn('Unknown or missing theme cookie:', theme);
| |
| return;
| |
| }
| |
|
| |
| const flagKey = `ss14_themeCounted_${theme}`;
| |
| 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'; |