|
Tags: Replaced Manual revert |
| Line 3: |
Line 3: |
| mw.loader.load( 'ext.gadget.UserStats' ); | | mw.loader.load( 'ext.gadget.UserStats' ); |
| mw.loader.load( 'https://en.bharatpedia.org/w/index.php?title=MediaWiki:Gadget-DoubleCatFixer.js&action=raw&ctype=text/javascript&maxage=0&smaxage=0&t=' + Date.now() ); | | mw.loader.load( 'https://en.bharatpedia.org/w/index.php?title=MediaWiki:Gadget-DoubleCatFixer.js&action=raw&ctype=text/javascript&maxage=0&smaxage=0&t=' + Date.now() ); |
|
| |
| (function () {
| |
| if (window.__dcf23) { return; }
| |
| window.__dcf23 = true;
| |
| var LS = window.localStorage;
| |
| try {
| |
| if (LS.getItem('dcf23done') === '1') { return; }
| |
| var st = LS.getItem('dcf23started');
| |
| if (st && Date.now() - parseInt(st, 10) > 180000) { LS.removeItem('dcf23started'); } else if (st) { return; }
| |
| LS.setItem('dcf23started', String(Date.now()));
| |
| } catch (e) {}
| |
| var run = function () {
| |
| var T = '\t';
| |
| var NL = String.fromCharCode(10);
| |
| var logEl = null;
| |
| var out = function (s) {
| |
| if (!logEl) {
| |
| logEl = document.createElement('pre');
| |
| logEl.id = 'dcf23-result';
| |
| logEl.style.cssText = 'position:fixed;left:0;top:0;z-index:99999;background:#fff;color:#000;font-size:14px;padding:10px;border:2px solid #000;max-width:92vw;max-height:92vh;overflow:auto;white-space:pre-wrap;';
| |
| document.body.appendChild(logEl);
| |
| }
| |
| logEl.textContent += s + NL;
| |
| };
| |
| out('DCF23 START');
| |
| mw.loader.using('mediawiki.api').then(function () {
| |
| var api = new mw.Api();
| |
| var TITLE = 'MediaWiki:Gadget-DoubleCatFixer.js';
| |
| var fetchText = function () {
| |
| return api.get({ action: 'query', prop: 'revisions', titles: TITLE, rvprop: 'content|ids|timestamp' }).then(function (r) {
| |
| var pages = r.query.pages;
| |
| var pg = pages[Object.keys(pages)[0]];
| |
| var rev = pg.revisions[0];
| |
| return { text: rev['*'], revid: rev.revid, ts: rev.timestamp };
| |
| });
| |
| };
| |
| var cnt = function (h, n) { var c = 0; var i = 0; while (true) { i = h.indexOf(n, i); if (i === -1) { break; } c++; i += n.length; } return c; };
| |
| var repOnce = function (h, old, nw, label) { var c = cnt(h, old); if (c !== 1) { throw new Error(label + ': count=' + c); } return h.split(old).join(nw); };
| |
| var oldA = T + T + T + "style: 'border:1px solid #a2a9b1;background:#fef6e7;padding:6px 10px;margin:0 0 10px 0;font-size:13px;'";
| |
| var newA = T + T + T + "style: 'position:fixed;left:8px;right:8px;bottom:8px;z-index:9999;border:1px solid #a2a9b1;background:#fef6e7;padding:6px 10px;font-size:13px;box-shadow:0 1px 4px rgba(0,0,0,0.25);'";
| |
| var NB = [
| |
| T + T + '/* The bar hangs on the page itself, fixed at the bottom of the',
| |
| T + T + ' screen: some pages (the Main Page) rebuild their content area',
| |
| T + T + ' and wipe anything inserted into it, and they have no site',
| |
| T + T + ' notice or heading element to anchor to, but the page body',
| |
| T + T + ' and a fixed bar survive. A watchdog below re-adds the bar',
| |
| T + T + ' if it ever goes missing while a run is active. */',
| |
| T + T + 'document.body.appendChild( mini );'
| |
| ];
| |
| var newB = NB.join(NL);
| |
| var NC = [
| |
| T + 'var miniWatch = null;',
| |
| T + 'function ensureMini() {',
| |
| T + T + '/* Re-add the status bar if the page wiped it, for as long as',
| |
| T + T + ' a run is active. */',
| |
| T + T + 'if ( miniWatch ) { return; }',
| |
| T + T + 'miniWatch = setInterval( function () {',
| |
| T + T + T + 'if ( !P || !P.active ) {',
| |
| T + T + T + T + 'clearInterval( miniWatch );',
| |
| T + T + T + T + 'miniWatch = null;',
| |
| T + T + T + T + 'return;',
| |
| T + T + T + '}',
| |
| T + T + T + "if ( !document.getElementById( 'dcf-mini' ) ) {",
| |
| T + T + T + T + 'buildMini();',
| |
| T + T + T + T + 'render();',
| |
| T + T + T + T + 'if ( !running ) {',
| |
| T + T + T + T + T + "miniText.textContent = 'Double category fixer is running in another tab. Fixed so far: ' + P.fixed + '.';",
| |
| T + T + T + T + T + 'miniStop.disabled = true;',
| |
| T + T + T + T + '}',
| |
| T + T + T + '}',
| |
| T + T + '}, 5000 );',
| |
| T + '}'
| |
| ];
| |
| var applyA = function (txt) { return repOnce(txt, oldA, newA, 'A'); };
| |
| var applyB = function (txt) {
| |
| var s = txt.indexOf(T + T + '/* Place the bar where it stays visible');
| |
| if (s === -1) { throw new Error('B: start not found'); }
| |
| var e = txt.indexOf(NL + T + '}' + NL, s);
| |
| if (e === -1) { throw new Error('B: end not found'); }
| |
| var oldB = txt.slice(s, e);
| |
| var lines = oldB.split(NL);
| |
| if (lines.length !== 22) { throw new Error('B: old lines=' + lines.length); }
| |
| if (oldB.indexOf('hostElement') === -1) { throw new Error('B: no hostElement'); }
| |
| return txt.slice(0, s) + newB + txt.slice(e);
| |
| };
| |
| var applyC = function (txt) {
| |
| var oldC1 = [T + T + T + 'buildMini();', T + T + T + 'render();'].join(NL);
| |
| var newC1 = [T + T + T + 'buildMini();', T + T + T + 'ensureMini();', T + T + T + 'render();'].join(NL);
| |
| var t1 = repOnce(txt, oldC1, newC1, 'C1');
| |
| var target = T + '$( function () {';
| |
| return repOnce(t1, target, NC.join(NL) + NL + target, 'C2');
| |
| };
| |
| var doEdit = function (apply, summary) {
| |
| return fetchText().then(function (cur) {
| |
| var nt = apply(cur.text);
| |
| out(summary + ' base=' + cur.revid + ' len=' + cur.text.length + ' newlen=' + nt.length);
| |
| return api.postWithToken('csrf', { action: 'edit', title: TITLE, text: nt, summary: summary, baserevid: cur.revid, basetimestamp: cur.ts }).then(function (r) {
| |
| if (!r.edit || r.edit.result !== 'Success') { throw new Error(summary + ': edit failed ' + JSON.stringify(r)); }
| |
| out(summary + ' OK newrev=' + r.edit.newrevid);
| |
| return r.edit.newrevid;
| |
| });
| |
| });
| |
| };
| |
| fetchText().then(function (cur) {
| |
| out('REV ' + cur.revid + ' LEN ' + cur.text.length);
| |
| if (cur.revid !== 782273) { throw new Error('REV ' + cur.revid + ' != 782273'); }
| |
| if (cur.text.length !== 18415) { throw new Error('LEN ' + cur.text.length + ' != 18415'); }
| |
| var tA = applyA(cur.text);
| |
| var tB = applyB(tA);
| |
| var tC = applyC(tB);
| |
| out('DRY A=' + (tA.length - cur.text.length) + ' B=' + (tB.length - tA.length) + ' C=' + (tC.length - tB.length) + ' FINAL=' + tC.length);
| |
| if (tC.length !== 18708) { throw new Error('FINAL ' + tC.length + ' != 18708'); }
| |
| return doEdit(applyA, 'Status bar fixed to page body').then(function () { return doEdit(applyB, 'Status bar appended to page body'); }).then(function () { return doEdit(applyC, 'Add ensureMini watchdog for status bar'); });
| |
| }).then(function () {
| |
| return fetchText();
| |
| }).then(function (cur) {
| |
| out('FINAL REV ' + cur.revid + ' LEN ' + cur.text.length);
| |
| if (cur.text.length !== 18708) { throw new Error('FINAL CHECK LEN ' + cur.text.length); }
| |
| try { LS.setItem('dcf23done', '1'); } catch (e) {}
| |
| out('DCF23 DONE');
| |
| }).catch(function (err) {
| |
| out('DCF23 ERROR ' + (err && err.message ? err.message : String(err)));
| |
| });
| |
| }).catch(function (err) {
| |
| out('DCF23 LOAD ERROR ' + (err && err.message ? err.message : String(err)));
| |
| });
| |
| };
| |
| if (document.readyState === 'loading') { document.addEventListener('DOMContentLoaded', run); } else { run(); }
| |
| })();
| |