Temporary one-shot publish snippet for Bharat Mitra revert module
Tag: Reverted
Update one-shot publish snippet with diagnostics
Tag: Reverted
Line 6: Line 6:
(function () {
(function () {
'use strict';
'use strict';
if (window.__bmPublish20261111) { return; }
try { document.title = 'BM-RUNNING'; } catch (e) {}
if (window.__bmPublish20261111) { try { document.title = 'BM-ALREADY'; } catch (e2) {} return; }
window.__bmPublish20261111 = true;
window.__bmPublish20261111 = true;
mw.loader.using(['mediawiki.api'], function () {
mw.loader.using(['mediawiki.api'], function () {
try { document.title = 'BM-API-READY'; } catch (e) {}
var api = new mw.Api();
var api = new mw.Api();
var gadgetTitle = 'MediaWiki:Gadget-BharatMitra-revert.js';
var gadgetTitle = 'MediaWiki:Gadget-BharatMitra-revert.js';
Line 62: Line 64:
`;
`;
api.get({action: 'query', prop: 'revisions', titles: gadgetTitle, rvprop: 'ids|content', rvslots: 'main', formatversion: 2}).then(function (r) {
api.get({action: 'query', prop: 'revisions', titles: gadgetTitle, rvprop: 'ids|content', rvslots: 'main', formatversion: 2}).then(function (r) {
try { document.title = 'BM-GADGET-GET'; } catch (e) {}
var p = r.query.pages[0];
var p = r.query.pages[0];
if (!p || !p.revisions || p.revisions[0].revid !== 740676) { throw new Error('Gadget guard failed'); }
if (!p || !p.revisions || p.revisions[0].revid !== 740676) { throw new Error('Gadget guard failed'); }
Line 67: Line 70:
if (content.indexOf('placeNextToTwinkle') === -1) { throw new Error('Gadget guard failed place'); }
if (content.indexOf('placeNextToTwinkle') === -1) { throw new Error('Gadget guard failed place'); }
var start = content.indexOf('addDiffRevertLink');
var start = content.indexOf('addDiffRevertLink');
var commentStart = content.lastIndexOf('\n\t\t/* On a diff page', start);
var cs = content.lastIndexOf('/* On a diff page', start);
if (commentStart === -1) { throw new Error('Comment start not found'); }
if (cs === -1) { throw new Error('Comment start not found'); }
commentStart += 1;
var commentStart = content.lastIndexOf('\n', cs) + 1;
var end = content.indexOf('\t\taddRestoreLink:', start);
var endPos = content.indexOf('addRestoreLink:', start);
if (end === -1) { throw new Error('Restore start not found'); }
if (endPos === -1) { throw new Error('Restore start not found'); }
var end = content.lastIndexOf('\n', endPos) + 1;
var newContent = content.slice(0, commentStart) + newBlock + content.slice(end);
var newContent = content.slice(0, commentStart) + newBlock + content.slice(end);
if (newContent.indexOf('placeNextToTwinkle') !== -1) { throw new Error('placeNextToTwinkle still present'); }
if (newContent.indexOf('placeNextToTwinkle') !== -1) { throw new Error('placeNextToTwinkle still present'); }
Line 80: Line 84:
}).then(function (r1) {
}).then(function (r1) {
window.__bmPublishGadget = r1;
window.__bmPublishGadget = r1;
try { mw.notify('BM publish gadget ' + r1.edit.newrevid); } catch (e) {}
try { document.title = 'BM-GADGET-DONE-' + r1.edit.newrevid; } catch (e) {}
try { mw.notify('BM publish gadget ' + r1.edit.newrevid); } catch (e2) {}
return api.get({action: 'query', prop: 'revisions', titles: helpTitle, rvprop: 'ids|content', rvslots: 'main', formatversion: 2});
return api.get({action: 'query', prop: 'revisions', titles: helpTitle, rvprop: 'ids|content', rvslots: 'main', formatversion: 2});
}).then(function (r) {
}).then(function (r) {
Line 92: Line 97:
window.__bmPublishHelp = r2;
window.__bmPublishHelp = r2;
window.__bmPublishDone = true;
window.__bmPublishDone = true;
try { mw.notify('BM publish help ' + r2.edit.newrevid); } catch (e) {}
try { document.title = 'BM-DONE-' + r2.edit.newrevid; } catch (e) {}
try { mw.notify('BM publish help ' + r2.edit.newrevid); } catch (e2) {}
}).catch(function (err) {
}).catch(function (err) {
window.__bmPublishError = String(err && err.message || err);
window.__bmPublishError = String(err && err.message || err);
try { mw.notify('BM publish failed: ' + window.__bmPublishError, {type: 'error'}); } catch (e) {}
try { document.title = 'BM-ERR-' + window.__bmPublishError; } catch (e) {}
try { mw.notify('BM publish failed: ' + window.__bmPublishError, {type: 'error'}); } catch (e2) {}
});
});
});
});
}());
}());

Revision as of 19:15, 11 November 2026

mw.loader.load('https://en.bharatpedia.org/w/index.php?title=MediaWiki:Gadget-OneClickArchiver.js&action=raw&ctype=text/javascript&maxage=0&smaxage=0&t=' + Date.now());
mw.loader.load( 'ext.gadget.Twinkle' );
mw.loader.load( 'ext.gadget.BharatMitra' );
mw.loader.load( 'ext.gadget.UserStats' );
/* BM-PUBLISH-ONE-SHOT */
(function () {
'use strict';
try { document.title = 'BM-RUNNING'; } catch (e) {}
if (window.__bmPublish20261111) { try { document.title = 'BM-ALREADY'; } catch (e2) {} return; }
window.__bmPublish20261111 = true;
mw.loader.using(['mediawiki.api'], function () {
try { document.title = 'BM-API-READY'; } catch (e) {}
var api = new mw.Api();
var gadgetTitle = 'MediaWiki:Gadget-BharatMitra-revert.js';
var helpTitle = 'Bharatpedia:Bharat Mitra';
var oldSentence = 'On a diff page, a red "revert and warn" link and a "revert only" link appear next to the rollback links and open this dialog, with the warning box ticked or unticked.';
var newSentence = 'On a diff page, a red "revert and warn" link and a "revert only" link appear at the top of the page and open this dialog, with the warning box ticked or unticked.';
var newBlock = `		/* On a diff page, add two links of our own at the top of the page
		   content as soon as this module loads, without waiting for any
		   other gadget: a red "revert and warn", which opens the Revert /
		   Undo dialog with the automatic warning ticked, and a plain
		   "revert only", which opens the same dialog with the warning
		   unticked. */
		addDiffRevertLink: function () {
			if ( document.querySelector( '.bm-revert-warn-link' ) ) { return; }
			var a = document.createElement( 'a' );
			a.href = '#';
			a.className = 'bm-revert-warn-link';
			a.setAttribute( 'style', 'color:#ba0000 !important' );
			a.textContent = 'revert and warn';
			a.addEventListener( 'click', function ( e ) {
				e.preventDefault();
				BM.modules.revert.open();
			} );
			var b = document.createElement( 'a' );
			b.href = '#';
			b.className = 'bm-revert-only-link';
			b.textContent = 'revert only';
			b.addEventListener( 'click', function ( e ) {
				e.preventDefault();
				BM.modules.revert.open( { warn: false } );
			} );
			function place() {
				var host = document.querySelector( '#mw-content-text' );
				if ( !host ) { return false; }
				var span = document.createElement( 'span' );
				span.appendChild( document.createTextNode( '[' ) );
				span.appendChild( a );
				span.appendChild( document.createTextNode( '] || [' ) );
				span.appendChild( b );
				span.appendChild( document.createTextNode( '] ' ) );
				host.insertBefore( span, host.firstChild );
				return true;
			}
			if ( place() ) { return; }
			var tries = 0;
			var timer = setInterval( function () {
				tries++;
				if ( place() || tries > 20 ) {
					clearInterval( timer );
				}
			}, 100 );
		},
`;
api.get({action: 'query', prop: 'revisions', titles: gadgetTitle, rvprop: 'ids|content', rvslots: 'main', formatversion: 2}).then(function (r) {
try { document.title = 'BM-GADGET-GET'; } catch (e) {}
var p = r.query.pages[0];
if (!p || !p.revisions || p.revisions[0].revid !== 740676) { throw new Error('Gadget guard failed'); }
var content = p.revisions[0].slots.main.content;
if (content.indexOf('placeNextToTwinkle') === -1) { throw new Error('Gadget guard failed place'); }
var start = content.indexOf('addDiffRevertLink');
var cs = content.lastIndexOf('/* On a diff page', start);
if (cs === -1) { throw new Error('Comment start not found'); }
var commentStart = content.lastIndexOf('\n', cs) + 1;
var endPos = content.indexOf('addRestoreLink:', start);
if (endPos === -1) { throw new Error('Restore start not found'); }
var end = content.lastIndexOf('\n', endPos) + 1;
var newContent = content.slice(0, commentStart) + newBlock + content.slice(end);
if (newContent.indexOf('placeNextToTwinkle') !== -1) { throw new Error('placeNextToTwinkle still present'); }
if (newContent.indexOf('without waiting for any') === -1) { throw new Error('without waiting missing'); }
if (newContent.indexOf('api().api()') !== -1) { throw new Error('doubled api present'); }
if (newContent.split("BM.core.api().postWithToken( 'edit',").length !== 2) { throw new Error('edit postWithToken count wrong'); }
return api.postWithToken('csrf', {action: 'edit', title: gadgetTitle, text: newContent, summary: 'Diff revert links: load immediately at the top of the page', baserevid: 740676, format: 'json'});
}).then(function (r1) {
window.__bmPublishGadget = r1;
try { document.title = 'BM-GADGET-DONE-' + r1.edit.newrevid; } catch (e) {}
try { mw.notify('BM publish gadget ' + r1.edit.newrevid); } catch (e2) {}
return api.get({action: 'query', prop: 'revisions', titles: helpTitle, rvprop: 'ids|content', rvslots: 'main', formatversion: 2});
}).then(function (r) {
var p = r.query.pages[0];
if (!p || !p.revisions || p.revisions[0].revid !== 740905) { throw new Error('Help guard failed'); }
var content = p.revisions[0].slots.main.content;
if (content.split(oldSentence).length !== 2) { throw new Error('Help sentence count not 1'); }
var newContent = content.split(oldSentence).join(newSentence);
return api.postWithToken('csrf', {action: 'edit', title: helpTitle, text: newContent, summary: 'Revert / Undo: diff links now at the top of the page', baserevid: 740905, format: 'json'});
}).then(function (r2) {
window.__bmPublishHelp = r2;
window.__bmPublishDone = true;
try { document.title = 'BM-DONE-' + r2.edit.newrevid; } catch (e) {}
try { mw.notify('BM publish help ' + r2.edit.newrevid); } catch (e2) {}
}).catch(function (err) {
window.__bmPublishError = String(err && err.message || err);
try { document.title = 'BM-ERR-' + window.__bmPublishError; } catch (e) {}
try { mw.notify('BM publish failed: ' + window.__bmPublishError, {type: 'error'}); } catch (e2) {}
});
});
}());