Note: After publishing, you may have to bypass your browser's cache to see the changes.

  • Firefox / Safari: Hold Shift while clicking Reload, or press either Ctrl-F5 or Ctrl-R (⌘-R on a Mac)
  • Google Chrome: Press Ctrl-Shift-R (⌘-Shift-R on a Mac)
  • Edge: Hold Ctrl while clicking Refresh, or press Ctrl-F5.
/**
 * AntiVandal — Bharatpedia patrol helper
 *
 * Original userscript for en.bharatpedia.org.
 * Inspired by the general idea of icon-driven revert + notice tools,
 * written from scratch for this wiki's API, templates, and noticeboards.
 *
 * Host: User:WikiDwarf/AntiVandal.js
 * Install (Special:MyPage/common.js):
 *   mw.loader.load('/w/index.php?title=User:WikiDwarf/AntiVandal.js&action=raw&ctype=text/javascript');
 *
 * You are responsible for every action this script performs on your account.
 */
(function () {
	'use strict';

	if (window.AntiVandal && window.AntiVandal.loaded) {
		return;
	}

	var RK = (window.AntiVandal = window.AntiVandal || {});
	RK.loaded = true;
	RK.version = '1.1.0';

	var CONF = {
		storageKey: 'antivandal-prefs-v1',
		packKey: 'antivandal-packs-v1',
		tag: 'AntiVandal',
		aivPage: 'Bharatpedia:Administrator intervention against vandalism',
		rfppPage: 'Bharatpedia:Requests for page protection',
		anPage: 'Bharatpedia:Administrators\' noticeboard',
		privacyPage: 'Bharatpedia:Requests for oversight',
		docsPage: 'Bharatpedia:AntiVandal'
	};

	var PRESETS = [
		{ id: 'vandalism', label: 'Vandalism', summary: 'Reverted unconstructive edit(s)', notice: 'uw-vandalism', level: true },
		{ id: 'blanking', label: 'Blanking', summary: 'Reverted unexplained content removal', notice: 'uw-delete', level: true },
		{ id: 'test', label: 'Test edit', summary: 'Reverted test edit', notice: 'uw-test', level: true },
		{ id: 'unsourced', label: 'Unsourced', summary: 'Reverted unsourced or poorly sourced addition', notice: 'uw-unsourced', level: true },
		{ id: 'spam', label: 'Spam / promo', summary: 'Reverted promotional or spam content', notice: 'uw-spam', level: true },
		{ id: 'npov', label: 'POV / attack', summary: 'Reverted non-neutral or attack content', notice: 'uw-biog', level: true },
		{ id: 'custom', label: 'Custom summary', summary: '', notice: '', level: false }
	];

	var NOTICES = [
		{ id: 'uw-vandalism', label: 'Vandalism', leveled: true },
		{ id: 'uw-test', label: 'Test edit', leveled: true },
		{ id: 'uw-delete', label: 'Removing content', leveled: true },
		{ id: 'uw-unsourced', label: 'Unsourced content', leveled: true },
		{ id: 'uw-error', label: 'Factual error / hoax', leveled: true },
		{ id: 'uw-spam', label: 'Spam / advertising', leveled: true },
		{ id: 'uw-advert', label: 'Promotional tone', leveled: true },
		{ id: 'uw-biog', label: 'BLP / biography issue', leveled: true },
		{ id: 'uw-defamatory', label: 'Defamatory content', leveled: true },
		{ id: 'uw-joke', label: 'Joke edit', leveled: true },
		{ id: 'uw-notvoted', label: 'Not a vote / forum', leveled: false },
		{ id: 'uw-chat', label: 'Talk page as chat', leveled: false },
		{ id: 'welcome', label: 'Welcome (constructive new editor)', leveled: false },
		{ id: 'welcometest', label: 'Welcome after a test edit', leveled: false }
	];

	function defaultPrefs() {
		return {
			barPosition: 'top',
			useRollback: true,
			autoNotice: false
		};
	}

	function loadPrefs() {
		try {
			var raw = localStorage.getItem(CONF.storageKey);
			return raw ? Object.assign(defaultPrefs(), JSON.parse(raw)) : defaultPrefs();
		} catch (e) {
			return defaultPrefs();
		}
	}

	function savePrefs(p) {
		try {
			localStorage.setItem(CONF.storageKey, JSON.stringify(p));
		} catch (e) { /* ignore quota */ }
	}

	var prefs = loadPrefs();
	var api = null;
	var hasRollback = false;

	function toast(msg, kind) {
		var el = document.getElementById('antivandal-toast');
		if (!el) {
			el = document.createElement('div');
			el.id = 'antivandal-toast';
			el.setAttribute('role', 'status');
			document.body.appendChild(el);
		}
		el.className = 'rk-toast rk-toast-' + (kind || 'info');
		el.textContent = msg;
		el.style.display = 'block';
		clearTimeout(toast._t);
		toast._t = setTimeout(function () {
			el.style.display = 'none';
		}, 4200);
	}

	function pageName() {
		return mw.config.get('wgPageName');
	}

	function relevantUser() {
		return mw.config.get('wgRelevantUserName') || null;
	}

	function talkTitleFor(user) {
		if (!user) {
			return null;
		}
		if (/^(?:(?:25[0-5]|2[0-4]\d|[01]?\d?\d)\.){3}(?:25[0-5]|2[0-4]\d|[01]?\d?\d)$/.test(user) ||
			user.indexOf(':') !== -1 && user.match(/^[0-9a-fA-F:.]+$/)) {
			return 'User talk:' + user;
		}
		return 'User talk:' + user;
	}

	function injectCss() {
		if (document.getElementById('antivandal-css')) {
			return;
		}
		var css = [
			'#antivandal-open.bpneo-title-btn{color:#c62828;}',
			'#antivandal-open.bpneo-title-btn i{color:#c62828;}',
			'#antivandal-tools{display:none;align-items:center;gap:inherit;}',
			'.bpneo-title-actions.av-open > :not(#antivandal-tools){display:none !important;}',
			'.bpneo-title-actions.av-open #antivandal-tools{display:inline-flex;}',
			'#antivandal-tools .bpneo-title-btn.rk-on{color:#2a4a33;}',
			'#antivandal-more{position:absolute;right:0;top:36px;background:#fff;border:1px solid #a2a9b1;',
			'box-shadow:0 2px 8px rgba(0,0,0,.18);min-width:200px;z-index:50;display:none;padding:4px 0;}',
			'#antivandal-more button{display:block;width:100%;text-align:left;border:0;background:none;',
			'padding:8px 12px;cursor:pointer;font:13px sans-serif;}',
			'#antivandal-more button:hover{background:#eaecf0;}',
			'#antivandal-ctx{position:fixed;z-index:1300;background:#fff;border:1px solid #a2a9b1;',
			'box-shadow:0 2px 8px rgba(0,0,0,.2);min-width:180px;padding:4px 0;display:none;}',
			'#antivandal-ctx button{display:block;width:100%;text-align:left;border:0;background:none;',
			'padding:8px 12px;cursor:pointer;font:13px sans-serif;}',
			'#antivandal-ctx button:hover{background:#eaecf0;}',
			'.rk-modal-bg{position:fixed;inset:0;background:rgba(0,0,0,.45);z-index:1100;display:flex;',
			'align-items:flex-start;justify-content:center;padding:8vh 16px 16px;}',
			'.rk-modal{background:#fff;color:#1b1b1b;max-width:560px;width:100%;border-radius:8px;',
			'box-shadow:0 12px 40px rgba(0,0,0,.28);overflow:hidden;}',
			'.rk-modal h2{margin:0;padding:12px 16px;background:#1f2a22;color:#f4efe4;font-size:16px;}',
			'.rk-modal .rk-body{padding:14px 16px;}',
			'.rk-modal label{display:block;font-weight:600;margin:10px 0 4px;}',
			'.rk-modal input[type=text],.rk-modal select,.rk-modal textarea{width:100%;box-sizing:border-box;',
			'padding:7px 8px;font:14px sans-serif;}',
			'.rk-modal textarea{min-height:90px;}',
			'.rk-modal .rk-actions{display:flex;justify-content:flex-end;gap:8px;padding:10px 16px;background:#f3f1ea;}',
			'.rk-modal .rk-actions button{padding:7px 14px;border-radius:4px;border:1px solid #bbb;cursor:pointer;}',
			'.rk-modal .rk-actions .rk-primary{background:#1f2a22;color:#fff;border-color:#1f2a22;}',
			'.rk-toast{position:fixed;right:16px;bottom:16px;z-index:1200;background:#1f2a22;color:#fff;',
			'padding:10px 14px;border-radius:6px;max-width:360px;display:none;}',
			'.rk-toast-err{background:#6b2a22;}',
			'.rk-toast-ok{background:#2a4a33;}',
			'#av-patrol{position:fixed;top:0;right:0;width:320px;height:100vh;background:#111;color:#eee;',
			'z-index:1250;overflow:auto;box-shadow:-4px 0 18px rgba(0,0,0,.3);font:13px sans-serif;}',
			'#av-patrol h3{margin:0;padding:12px 14px;background:#1f2a22;font-size:15px;}',
			'#av-patrol .av-p-item{display:block;padding:8px 12px;border-bottom:1px solid #333;color:#eee;text-decoration:none;}',
			'#av-patrol .av-p-item:hover{background:#2a3a2e;}',
			'#av-patrol .av-neg{background:rgba(180,40,40,.25);}',
			'#av-patrol .av-pos{background:rgba(40,140,70,.2);}',
			'.av-contrib-rev{margin-left:6px;font-size:12px;cursor:pointer;color:#c62828;border:0;background:none;}'
		].join('');
		var style = document.createElement('style');
		style.id = 'antivandal-css';
		style.textContent = css;
		document.head.appendChild(style);
	}

	function closeModal() {
		var bg = document.getElementById('antivandal-modal-bg');
		if (bg) {
			bg.parentNode.removeChild(bg);
		}
	}

	function openModal(title, bodyNode, actions) {
		closeModal();
		var bg = document.createElement('div');
		bg.id = 'antivandal-modal-bg';
		bg.className = 'rk-modal-bg';
		bg.addEventListener('click', function (e) {
			if (e.target === bg) {
				closeModal();
			}
		});
		var box = document.createElement('div');
		box.className = 'rk-modal';
		var h = document.createElement('h2');
		h.textContent = title;
		box.appendChild(h);
		var body = document.createElement('div');
		body.className = 'rk-body';
		body.appendChild(bodyNode);
		box.appendChild(body);
		var foot = document.createElement('div');
		foot.className = 'rk-actions';
		(actions || []).forEach(function (a) {
			var b = document.createElement('button');
			b.type = 'button';
			b.textContent = a.label;
			if (a.primary) {
				b.className = 'rk-primary';
			}
			b.addEventListener('click', a.onClick);
			foot.appendChild(b);
		});
		box.appendChild(foot);
		bg.appendChild(box);
		document.body.appendChild(bg);
		return box;
	}

	function field(labelText, node) {
		var wrap = document.createElement('div');
		var lab = document.createElement('label');
		lab.textContent = labelText;
		wrap.appendChild(lab);
		wrap.appendChild(node);
		return wrap;
	}

	function getLastRev() {
		return api.get({
			action: 'query',
			prop: 'revisions',
			titles: pageName(),
			rvlimit: 2,
			rvprop: 'ids|user|comment|timestamp',
			formatversion: 2
		}).then(function (data) {
			var page = data.query && data.query.pages && data.query.pages[0];
			if (!page || page.missing || !page.revisions || !page.revisions.length) {
				return null;
			}
			return {
				page: page,
				latest: page.revisions[0],
				previous: page.revisions[1] || null
			};
		});
	}

	function revertWithSummary(summary, userToRevert) {
		return getLastRev().then(function (info) {
			if (!info) {
				throw new Error('Could not read this page history.');
			}
			var latest = info.latest;
			var targetUser = userToRevert || latest.user;
			if (prefs.useRollback && hasRollback) {
				return api.postWithToken('rollback', {
					action: 'rollback',
					title: pageName(),
					user: targetUser,
					summary: summary + ' ([[' + CONF.docsPage + '|AntiVandal]])',
					formatversion: 2
				}).then(function (res) {
					if (res.error) {
						throw new Error(res.error.info || res.error.code);
					}
					return { method: 'rollback', data: res };
				});
			}
			if (!info.previous) {
				throw new Error('No earlier revision to restore.');
			}
			return api.postWithEditToken({
				action: 'edit',
				title: pageName(),
				undo: latest.revid,
				undoafter: info.previous.revid,
				summary: summary + ' ([[' + CONF.docsPage + '|AntiVandal]])',
				formatversion: 2
			}).then(function (res) {
				if (res.error) {
					throw new Error(res.error.info || res.error.code);
				}
				if (res.edit && res.edit.result !== 'Success') {
					throw new Error('Edit did not succeed.');
				}
				return { method: 'undo', data: res };
			});
		});
	}

	function restoreRevision(revid) {
		return api.get({
			action: 'query',
			prop: 'revisions',
			revids: revid,
			rvprop: 'ids|content|timestamp|user',
			rvslots: 'main',
			formatversion: 2
		}).then(function (data) {
			var page = data.query && data.query.pages && data.query.pages[0];
			var rev = page && page.revisions && page.revisions[0];
			var text = rev && rev.slots && rev.slots.main && rev.slots.main.content;
			if (!text && text !== '') {
				throw new Error('Could not load that revision.');
			}
			return api.postWithEditToken({
				action: 'edit',
				title: page.title || pageName(),
				text: text,
				summary: 'Restored revision ' + revid + ' ([[' + CONF.docsPage + '|AntiVandal]])',
				basetimestamp: undefined,
				formatversion: 2
			});
		});
	}

	function appendTalkNotice(user, templateName, article, extra) {
		var talk = talkTitleFor(user);
		var subst = '{{subst:' + templateName;
		if (article) {
			subst += '|1=' + article.replace(/_/g, ' ');
		}
		if (extra) {
			subst += '|2=' + extra;
		}
		subst += '}} [[User:WikiDwarf|WikiDwarf]] ([[User talk:WikiDwarf|talk]]) 15:01, 21 September 2026 (IST)';
		return api.postWithEditToken({
			action: 'edit',
			title: talk,
			appendtext: '\n\n' + subst,
			summary: 'Notice regarding [[' + (article || pageName()).replace(/_/g, ' ') + ']] ([[' + CONF.docsPage + '|AntiVandal]])',
			formatversion: 2
		});
	}

	function openRevertDialog() {
		getLastRev().then(function (info) {
			var user = info && info.latest ? info.latest.user : (relevantUser() || '');
			var wrap = document.createElement('div');
			var userInput = document.createElement('input');
			userInput.type = 'text';
			userInput.value = user || '';
			var sel = document.createElement('select');
			PRESETS.forEach(function (p) {
				var o = document.createElement('option');
				o.value = p.id;
				o.textContent = p.label;
				sel.appendChild(o);
			});
			var sum = document.createElement('input');
			sum.type = 'text';
			sum.value = PRESETS[0].summary;
			sel.addEventListener('change', function () {
				var p = PRESETS.filter(function (x) { return x.id === sel.value; })[0];
				if (p && p.summary) {
					sum.value = p.summary;
				}
			});
			var alsoWarn = document.createElement('input');
			alsoWarn.type = 'checkbox';
			alsoWarn.id = 'rk-also-warn';
			var warnLab = document.createElement('label');
			warnLab.htmlFor = 'rk-also-warn';
			warnLab.style.fontWeight = 'normal';
			warnLab.textContent = ' Open notice dialog after revert';
			var row = document.createElement('div');
			row.style.marginTop = '10px';
			row.appendChild(alsoWarn);
			row.appendChild(warnLab);

			wrap.appendChild(field('Editor to revert', userInput));
			wrap.appendChild(field('Reason', sel));
			wrap.appendChild(field('Edit summary', sum));
			wrap.appendChild(row);

			openModal('Revert with AntiVandal', wrap, [
				{ label: 'Cancel', onClick: closeModal },
				{
					label: 'Revert',
					primary: true,
					onClick: function () {
						var summary = (sum.value || 'Reverted edit').trim();
						var who = userInput.value.trim();
						if (!who) {
							toast('Name the editor whose edit you are reverting.', 'err');
							return;
						}
						toast('Reverting…');
						revertWithSummary(summary, who).then(function (res) {
							toast('Reverted with ' + res.method + '.', 'ok');
							closeModal();
							if (alsoWarn.checked) {
								openWarnDialog(who, sel.value);
							} else {
								setTimeout(function () { location.reload(); }, 700);
							}
						}).catch(function (err) {
							toast(err.message || String(err), 'err');
						});
					}
				}
			]);
		}).catch(function (err) {
			toast(err.message || String(err), 'err');
		});
	}

	function openWarnDialog(prefillUser, presetId) {
		var wrap = document.createElement('div');
		var userInput = document.createElement('input');
		userInput.type = 'text';
		userInput.value = prefillUser || relevantUser() || '';
		var articleInput = document.createElement('input');
		articleInput.type = 'text';
		articleInput.value = pageName().replace(/_/g, ' ');
		var sel = document.createElement('select');
		NOTICES.forEach(function (n) {
			var o = document.createElement('option');
			o.value = n.id;
			o.textContent = n.label + (n.leveled ? ' (1–4)' : '');
			sel.appendChild(o);
		});
		if (presetId) {
			var mapped = {
				vandalism: 'uw-vandalism',
				blanking: 'uw-delete',
				test: 'uw-test',
				unsourced: 'uw-unsourced',
				spam: 'uw-spam',
				npov: 'uw-biog'
			}[presetId];
			if (mapped) {
				sel.value = mapped;
			}
		}
		var level = document.createElement('select');
		[1, 2, 3, 4].forEach(function (n) {
			var o = document.createElement('option');
			o.value = String(n);
			o.textContent = 'Level ' + n;
			level.appendChild(o);
		});
		var extra = document.createElement('textarea');
		extra.placeholder = 'Optional extra sentence for templates that accept a second parameter';

		function currentTemplate() {
			var meta = NOTICES.filter(function (n) { return n.id === sel.value; })[0];
			if (meta && meta.leveled) {
				return sel.value + level.value;
			}
			return sel.value;
		}

		wrap.appendChild(field('User', userInput));
		wrap.appendChild(field('Related article', articleInput));
		wrap.appendChild(field('Notice', sel));
		wrap.appendChild(field('Level (if the template has levels)', level));
		wrap.appendChild(field('Extra note', extra));
		var hint = document.createElement('p');
		hint.style.fontSize = '12px';
		hint.style.color = '#555';
		hint.textContent = 'Checking last month’s talk-page notices…';
		wrap.appendChild(hint);
		detectTalkLevel(userInput.value.trim()).then(function (n) {
			level.value = String(n);
			hint.textContent = 'Suggested level ' + n + ' from this month’s talk page (you can change it).';
		});
		userInput.addEventListener('change', function () {
			detectTalkLevel(userInput.value.trim()).then(function (n) {
				level.value = String(n);
				hint.textContent = 'Suggested level ' + n + ' from this month’s talk page.';
			});
		});

		openModal('Post a talk-page notice', wrap, [
			{ label: 'Cancel', onClick: closeModal },
			{
				label: 'Post notice',
				primary: true,
				onClick: function () {
					var user = userInput.value.trim();
					if (!user) {
						toast('Enter a username or IP.', 'err');
						return;
					}
					var tpl = currentTemplate();
					toast('Posting {{' + tpl + '}}…');
					appendTalkNotice(user, tpl, articleInput.value.trim(), extra.value.trim()).then(function (res) {
						if (res.error) {
							throw new Error(res.error.info || res.error.code);
						}
						toast('Notice posted on User talk:' + user, 'ok');
						closeModal();
					}).catch(function (err) {
						toast(err.message || String(err), 'err');
					});
				}
			}
		]);
	}

	function openPrefs() {
		var wrap = document.createElement('div');
		var pos = document.createElement('select');
		['top', 'bottom'].forEach(function (v) {
			var o = document.createElement('option');
			o.value = v;
			o.textContent = v === 'top' ? 'Under the page title' : 'Stick to the bottom';
			pos.appendChild(o);
		});
		pos.value = prefs.barPosition;
		var rb = document.createElement('input');
		rb.type = 'checkbox';
		rb.checked = !!prefs.useRollback;
		var rbLab = document.createElement('label');
		rbLab.style.fontWeight = 'normal';
		rbLab.textContent = ' Prefer software rollback when this account has the right';
		var row = document.createElement('div');
		row.appendChild(rb);
		row.appendChild(rbLab);
		wrap.appendChild(field('Toolbar placement', pos));
		wrap.appendChild(row);
		openModal('AntiVandal preferences', wrap, [
			{ label: 'Close', onClick: closeModal },
			{
				label: 'Save',
				primary: true,
				onClick: function () {
					prefs.barPosition = pos.value;
					prefs.useRollback = rb.checked;
					savePrefs(prefs);
					closeModal();
					toast('Preferences saved. Reload to move the bar.', 'ok');
				}
			}
		]);
	}

	function goLatest() {
		location.href = mw.util.getUrl(pageName(), { diff: 'cur', oldid: 'prev' });
	}

	function reportAiv() {
		getLastRev().then(function (info) {
			var user = (info && info.latest && info.latest.user) || relevantUser() || '';
			var wrap = document.createElement('div');
			var p = document.createElement('p');
			p.textContent = 'This opens the AIV noticeboard. Paste the suggested line, or edit it first.';
			var ta = document.createElement('textarea');
			ta.value = '* {{vandal|' + user + '}} – repeated unconstructive editing on [[' +
				pageName().replace(/_/g, ' ') + ']]. [[User:WikiDwarf|WikiDwarf]] ([[User talk:WikiDwarf|talk]]) 15:01, 21 September 2026 (IST)';
			wrap.appendChild(p);
			wrap.appendChild(ta);
			openModal('Report at AIV', wrap, [
				{ label: 'Cancel', onClick: closeModal },
				{
					label: 'Open AIV',
					primary: true,
					onClick: function () {
						try {
							sessionStorage.setItem('antivandal-aiv-draft', ta.value);
						} catch (e) { /* ignore */ }
						window.open(mw.util.getUrl(CONF.aivPage, { action: 'edit', section: 'new' }), '_blank');
					}
				}
			]);
		});
	}

	function requestProtection() {
		window.open(mw.util.getUrl(CONF.rfppPage, { action: 'edit', section: 'new' }), '_blank');
	}

	function openNewMessage(prefillUser) {
		var wrap = document.createElement('div');
		var userInput = document.createElement('input');
		userInput.type = 'text';
		userInput.value = prefillUser || relevantUser() || '';
		var subj = document.createElement('input');
		subj.type = 'text';
		var body = document.createElement('textarea');
		body.placeholder = 'Wikitext of the new section';
		wrap.appendChild(field('User', userInput));
		wrap.appendChild(field('Subject', subj));
		wrap.appendChild(field('Message', body));
		openModal('New talk-page message', wrap, [
			{ label: 'Cancel', onClick: closeModal },
			{
				label: 'Send',
				primary: true,
				onClick: function () {
					var user = userInput.value.trim();
					if (!user || !subj.value.trim()) {
						toast('Need a user and a subject.', 'err');
						return;
					}
					var text = '\n\n== ' + subj.value.trim() + ' ==\n' + body.value + '\n[[User:WikiDwarf|WikiDwarf]] ([[User talk:WikiDwarf|talk]]) 15:01, 21 September 2026 (IST)';
					toast('Sending…');
					api.postWithEditToken({
						action: 'edit',
						title: talkTitleFor(user),
						appendtext: text,
						summary: 'New message ([[' + CONF.docsPage + '|AntiVandal]])',
						formatversion: 2
					}).then(function (res) {
						if (res.error) {
							throw new Error(res.error.info || res.error.code);
						}
						toast('Message posted.', 'ok');
						closeModal();
					}).catch(function (err) {
						toast(err.message || String(err), 'err');
					});
				}
			}
		]);
	}

	var alertTimer = null;
	var alertLastRev = null;
	function toggleAlert(btn) {
		if (alertTimer) {
			clearInterval(alertTimer);
			alertTimer = null;
			btn.classList.remove('rk-on');
			toast('Alert on change off.');
			return;
		}
		btn.classList.add('rk-on');
		toast('Watching this page for new edits.');
		getLastRev().then(function (info) {
			alertLastRev = info && info.latest ? info.latest.revid : null;
		});
		alertTimer = setInterval(function () {
			getLastRev().then(function (info) {
				var id = info && info.latest ? info.latest.revid : null;
				if (id && alertLastRev && id !== alertLastRev) {
					alertLastRev = id;
					if (window.Notification && Notification.permission === 'granted') {
						new Notification('AntiVandal', { body: pageName().replace(/_/g, ' ') + ' was edited.' });
					}
					toast('New edit on this page.', 'ok');
					goLatest();
				} else if (id) {
					alertLastRev = id;
				}
			});
		}, 12000);
		if (window.Notification && Notification.permission === 'default') {
			Notification.requestPermission();
		}
	}

	function openMoreMenu(anchor) {
		var menu = document.getElementById('antivandal-more');
		if (!menu) {
			return;
		}
		menu.style.display = menu.style.display === 'block' ? 'none' : 'block';
	}

	function bindUserContextMenu() {
		document.addEventListener('contextmenu', function (e) {
			if (!e.shiftKey) {
				return;
			}
			var a = e.target.closest ? e.target.closest('a') : null;
			if (!a || !a.href) {
				return;
			}
			var m = a.href.match(/[?&]title=User:([^&#]+)/i) || a.href.match(/\/wiki\/User:([^?#]+)/);
			if (!m) {
				return;
			}
			e.preventDefault();
			var user = decodeURIComponent(m[1]).replace(/_/g, ' ').split('/')[0];
			var ctx = document.getElementById('antivandal-ctx');
			if (!ctx) {
				ctx = document.createElement('div');
				ctx.id = 'antivandal-ctx';
				document.body.appendChild(ctx);
			}
			ctx.innerHTML = '';
			function item(label, fn) {
				var b = document.createElement('button');
				b.type = 'button';
				b.textContent = label;
				b.addEventListener('click', function () {
					ctx.style.display = 'none';
					fn();
				});
				ctx.appendChild(b);
			}
			item('User page', function () { location.href = mw.util.getUrl('User:' + user); });
			item('Talk page', function () { location.href = mw.util.getUrl('User talk:' + user); });
			item('Contributions', function () { location.href = mw.util.getUrl('Special:Contributions/' + user); });
			item('New message', function () { openNewMessage(user); });
			item('Notice', function () { openWarnDialog(user); });
			item('Quick template', function () { openQuickTemplates(user); });
			ctx.style.left = e.pageX + 'px';
			ctx.style.top = e.pageY + 'px';
			ctx.style.display = 'block';
		});
		document.addEventListener('click', function () {
			var ctx = document.getElementById('antivandal-ctx');
			if (ctx) {
				ctx.style.display = 'none';
			}
			var more = document.getElementById('antivandal-more');
			if (more && more.style.display === 'block') {
				more.style.display = 'none';
			}
		});
	}

	function addDiffRestore() {
		if (mw.config.get('wgDiffNewId') || mw.config.get('wgDiffOldId') ||
			mw.config.get('wgAction') === 'history') {
			var oldid = mw.util.getParamValue('oldid') || mw.config.get('wgRevisionId');
			if (!oldid) {
				return;
			}
			var bar = document.getElementById('antivandal-bar');
			if (!bar) {
				return;
			}
			var b = document.createElement('button');
			b.type = 'button';
			b.textContent = 'Restore this revision';
			b.addEventListener('click', function () {
				if (!window.confirm('Replace the current page text with revision ' + oldid + '?')) {
					return;
				}
				toast('Restoring…');
				restoreRevision(oldid).then(function (res) {
					if (res.error) {
						throw new Error(res.error.info || res.error.code);
					}
					toast('Revision restored.', 'ok');
					setTimeout(function () { location.href = mw.util.getUrl(pageName()); }, 600);
				}).catch(function (err) {
					toast(err.message || String(err), 'err');
				});
			});
			bar.appendChild(b);
		}
	}

	function currentMonthHeading() {
		var months = ['January','February','March','April','May','June','July','August','September','October','November','December'];
		var d = new Date();
		return months[d.getUTCMonth()] + ' ' + d.getUTCFullYear();
	}

	function detectTalkLevel(user) {
		if (!user) {
			return Promise.resolve(1);
		}
		return api.get({
			action: 'query',
			prop: 'revisions',
			titles: talkTitleFor(user),
			rvlimit: 1,
			rvprop: 'content',
			rvslots: 'main',
			formatversion: 2
		}).then(function (data) {
			var page = data.query && data.query.pages && data.query.pages[0];
			var text = page && page.revisions && page.revisions[0] &&
				page.revisions[0].slots && page.revisions[0].slots.main &&
				page.revisions[0].slots.main.content || '';
			var month = currentMonthHeading();
			var slice = text;
			var idx = text.lastIndexOf(month);
			if (idx !== -1) {
				slice = text.slice(idx);
			}
			var max = 0;
			var re = /\{\{(?:subst:)?uw-[\w-]+([1-4])\b/gi;
			var m;
			while ((m = re.exec(slice))) {
				max = Math.max(max, parseInt(m[1], 10));
			}
			return Math.min(4, max + 1) || 1;
		}).catch(function () {
			return 1;
		});
	}

	function loadPacks() {
		try {
			var raw = localStorage.getItem(CONF.packKey);
			if (raw) {
				return JSON.parse(raw);
			}
		} catch (e) { /* ignore */ }
		return [{
			name: 'Starter',
			items: [
				{ label: 'Welcome', wikitext: 'Hello, WikiDwarf/AntiVandal.js, and welcome to Bharatpedia! Thank you for [[Special:Contributions/WikiDwarf/AntiVandal.js|your contributions]]. I hope you like this place and decide to stay. We are working hard to make this one of the most popular websites in the world, and it's only possible through the contributions of editors like you. This website is not just a collection of articles, it's an active community. The real fun here is contributing to Bharatpedia, but don't feel hurt if some of your first few edits get removed, as there are some central guidelines you may not be familiar with.

Some good advice: be bold in your editing, and use the talk pages to discuss with other editors. Be kind to others, because there's a lot you can learn from them, and there's lots they can help you with.

There's lots of resources to help you become a great editor, from our basic introduction to our in-depth manual. But if you have any questions or problems, no matter what they are, leave me a message on [[User talk:WikiDwarf|my talk page]], or ask a question on your talk page. Oh, and please [[Bharatpedia:Signatures|sign your name]] on talk pages using four tildes (<nowiki>~~~~</nowiki>); this will automatically produce your username and the date. Again, welcome! 
' },
				{ label: 'Welcome (test)', wikitext: '{{subst:welcometest}}' },
				{ label: 'Please discuss', wikitext: 'Please use the article talk page to discuss changes rather than repeating them. [[User:WikiDwarf|WikiDwarf]] ([[User talk:WikiDwarf|talk]]) 15:01, 21 September 2026 (IST)' }
			]
		}];
	}

	function savePacks(packs) {
		try {
			localStorage.setItem(CONF.packKey, JSON.stringify(packs));
		} catch (e) { /* ignore */ }
	}

	function openQuickTemplates(prefillUser) {
		var packs = loadPacks();
		var wrap = document.createElement('div');
		var userInput = document.createElement('input');
		userInput.type = 'text';
		userInput.value = prefillUser || relevantUser() || '';
		wrap.appendChild(field('User talk target', userInput));
		var list = document.createElement('div');
		packs.forEach(function (pack) {
			var h = document.createElement('p');
			h.style.fontWeight = '700';
			h.textContent = pack.name;
			list.appendChild(h);
			(pack.items || []).forEach(function (item) {
				var b = document.createElement('button');
				b.type = 'button';
				b.textContent = item.label;
				b.style.margin = '0 6px 6px 0';
				b.addEventListener('click', function () {
					var user = userInput.value.trim();
					if (!user) {
						toast('Enter a user.', 'err');
						return;
					}
					toast('Posting template…');
					api.postWithEditToken({
						action: 'edit',
						title: talkTitleFor(user),
						appendtext: '\n\n' + item.wikitext + '\n[[User:WikiDwarf|WikiDwarf]] ([[User talk:WikiDwarf|talk]]) 15:01, 21 September 2026 (IST)',
						summary: 'Template: ' + item.label + ' ([[' + CONF.docsPage + '|AntiVandal]])',
						formatversion: 2
					}).then(function (res) {
						if (res.error) {
							throw new Error(res.error.info || res.error.code);
						}
						toast('Posted “' + item.label + '”.', 'ok');
						closeModal();
					}).catch(function (err) {
						toast(err.message || String(err), 'err');
					});
				});
				list.appendChild(b);
			});
		});
		wrap.appendChild(list);
		var add = document.createElement('textarea');
		add.placeholder = 'Add a pack as JSON: {"name":"Mine","items":[{"label":"Hi","wikitext":"Hello"}]}';
		wrap.appendChild(field('Install a pack (JSON)', add));
		openModal('Quick templates', wrap, [
			{ label: 'Close', onClick: closeModal },
			{
				label: 'Install pack',
				primary: true,
				onClick: function () {
					try {
						var pack = JSON.parse(add.value);
						if (!pack.name || !pack.items) {
							throw new Error('Need name and items');
						}
						packs.push(pack);
						savePacks(packs);
						toast('Pack saved in this browser.', 'ok');
						closeModal();
						openQuickTemplates(userInput.value);
					} catch (err) {
						toast(err.message || 'Invalid JSON', 'err');
					}
				}
			}
		]);
	}

	function togglePatrol() {
		var box = document.getElementById('av-patrol');
		if (box) {
			box.parentNode.removeChild(box);
			if (togglePatrol._t) {
				clearInterval(togglePatrol._t);
				togglePatrol._t = null;
			}
			return;
		}
		box = document.createElement('div');
		box.id = 'av-patrol';
		box.innerHTML = '<h3>AntiVandal patrol <button type="button" id="av-patrol-x" style="float:right">×</button></h3><div id="av-patrol-list"></div>';
		document.body.appendChild(box);
		document.getElementById('av-patrol-x').addEventListener('click', togglePatrol);
		function refresh() {
			api.get({
				action: 'query',
				list: 'recentchanges',
				rcprop: 'title|ids|sizes|user|comment|timestamp',
				rclimit: 25,
				rctype: 'edit|new',
				formatversion: 2
			}).then(function (data) {
				var list = document.getElementById('av-patrol-list');
				if (!list) {
					return;
				}
				list.innerHTML = '';
				(data.query && data.query.recentchanges || []).forEach(function (rc) {
					var a = document.createElement('a');
					a.className = 'av-p-item ' + ((rc.newlen - rc.oldlen) < 0 ? 'av-neg' : 'av-pos');
					a.href = mw.util.getUrl(rc.title, { diff: rc.revid, oldid: rc.old_revid || 'prev' });
					a.innerHTML = '';
					var t = document.createElement('strong');
					t.textContent = rc.title;
					a.appendChild(t);
					a.appendChild(document.createElement('br'));
					a.appendChild(document.createTextNode(rc.user + ' · ' + (rc.newlen - rc.oldlen)));
					list.appendChild(a);
				});
			}).catch(function (err) {
				toast(err.message || String(err), 'err');
			});
		}
		refresh();
		togglePatrol._t = setInterval(refresh, 8000);
	}

	function openMat() {
		toast('Loading history…');
		api.get({
			action: 'query',
			prop: 'revisions',
			titles: pageName(),
			rvlimit: 30,
			rvprop: 'ids|user|comment|timestamp',
			formatversion: 2
		}).then(function (data) {
			var page = data.query && data.query.pages && data.query.pages[0];
			var revs = (page && page.revisions) || [];
			var seen = {};
			var users = [];
			revs.forEach(function (r) {
				if (r.user && !seen[r.user]) {
					seen[r.user] = true;
					users.push(r.user);
				}
			});
			var wrap = document.createElement('div');
			var p = document.createElement('p');
			p.textContent = 'Tick editors from the last 30 revisions, pick one notice, then post. Review the list before you commit.';
			wrap.appendChild(p);
			var boxes = [];
			users.forEach(function (u) {
				var lab = document.createElement('label');
				lab.style.display = 'block';
				lab.style.fontWeight = 'normal';
				var c = document.createElement('input');
				c.type = 'checkbox';
				c.value = u;
				c.checked = true;
				lab.appendChild(c);
				lab.appendChild(document.createTextNode(' ' + u));
				wrap.appendChild(lab);
				boxes.push(c);
			});
			var sel = document.createElement('select');
			NOTICES.forEach(function (n) {
				var o = document.createElement('option');
				o.value = n.id + (n.leveled ? '1' : '');
				o.textContent = n.label;
				sel.appendChild(o);
			});
			wrap.appendChild(field('Notice to post', sel));
			openModal('Multiple notices', wrap, [
				{ label: 'Cancel', onClick: closeModal },
				{
					label: 'Post to checked',
					primary: true,
					onClick: function () {
						var chosen = boxes.filter(function (c) { return c.checked; }).map(function (c) { return c.value; });
						if (!chosen.length) {
							toast('No users selected.', 'err');
							return;
						}
						if (!window.confirm('Post ' + sel.value + ' to ' + chosen.length + ' talk page(s)?')) {
							return;
						}
						var chain = Promise.resolve();
						chosen.forEach(function (u) {
							chain = chain.then(function () {
								return appendTalkNotice(u, sel.value, pageName().replace(/_/g, ' '), '');
							});
						});
						toast('Posting…');
						chain.then(function () {
							toast('Notices posted.', 'ok');
							closeModal();
						}).catch(function (err) {
							toast(err.message || String(err), 'err');
						});
					}
				}
			]);
		}).catch(function (err) {
			toast(err.message || String(err), 'err');
		});
	}

	function openPendingReview() {
		api.get({
			action: 'query',
			list: 'oldreviewedpages',
			orlimit: 15,
			formatversion: 2
		}).then(function (data) {
			var pages = (data.query && data.query.oldreviewedpages) || [];
			if (!pages.length) {
				toast('No pending-review queue on this wiki (or it is empty).', 'err');
				return;
			}
			var wrap = document.createElement('div');
			pages.forEach(function (p) {
				var a = document.createElement('a');
				a.href = mw.util.getUrl(p.title, { diff: 'cur', oldid: 'prev' });
				a.textContent = p.title;
				a.style.display = 'block';
				wrap.appendChild(a);
			});
			openModal('Pending review', wrap, [{ label: 'Close', onClick: closeModal }]);
		}).catch(function () {
			toast('FlaggedRevs / pending changes is not available on this wiki.', 'err');
		});
	}

	function openAdminReport(kind) {
		var title = kind === 'privacy' ? CONF.privacyPage : CONF.anPage;
		var wrap = document.createElement('div');
		var p = document.createElement('p');
		p.textContent = kind === 'privacy'
			? 'Opens the local suppression / privacy request board. Do not post private data on a public page if a private route exists.'
			: 'Opens the administrators’ noticeboard with a draft about this page.';
		var ta = document.createElement('textarea');
		ta.value = 'Regarding [[' + pageName().replace(/_/g, ' ') + ']]. ';
		wrap.appendChild(p);
		wrap.appendChild(ta);
		openModal(kind === 'privacy' ? 'Privacy / suppression' : 'Report to administrators', wrap, [
			{ label: 'Cancel', onClick: closeModal },
			{
				label: 'Open board',
				primary: true,
				onClick: function () {
					try {
						sessionStorage.setItem('antivandal-report-draft', ta.value);
					} catch (e) { /* ignore */ }
					window.open(mw.util.getUrl(title, { action: 'edit', section: 'new' }), '_blank');
				}
			}
		]);
	}

	function bindContribsRollback() {
		if (mw.config.get('wgCanonicalSpecialPageName') !== 'Contributions') {
			return;
		}
		var list = document.querySelector('.mw-contributions-list') || document.querySelector('#bodyContent ul');
		if (!list) {
			return;
		}
		list.querySelectorAll('li').forEach(function (li) {
			if (li.querySelector('.av-contrib-rev')) {
				return;
			}
			var diff = li.querySelector('a[href*="diff="]');
			if (!diff) {
				return;
			}
			var b = document.createElement('button');
			b.type = 'button';
			b.className = 'av-contrib-rev';
			b.textContent = 'revert';
			b.addEventListener('click', function (e) {
				e.preventDefault();
				var href = diff.getAttribute('href') || '';
				var titleMatch = href.match(/title=([^&]+)/) || href.match(/\/wiki\/([^?]+)/);
				var title = titleMatch ? decodeURIComponent(titleMatch[1].replace(/\+/g, ' ')).replace(/_/g, ' ') : null;
				if (!title) {
					toast('Could not read that title.', 'err');
					return;
				}
				var user = mw.config.get('wgRelevantUserName');
				if (!window.confirm('Revert the latest edit by ' + user + ' on “' + title + '”?')) {
					return;
				}
				var prevPage = pageName;
				pageName = function () { return title.replace(/ /g, '_'); };
				toast('Reverting…');
				revertWithSummary('Reverted unconstructive edit(s)', user).then(function () {
					pageName = prevPage;
					toast('Reverted on “' + title + '”.', 'ok');
				}).catch(function (err) {
					pageName = prevPage;
					toast(err.message || String(err), 'err');
				});
			});
			li.appendChild(b);
		});
	}

	function neoBtn(iconClass, tip, fn) {
		var b = document.createElement('button');
		b.type = 'button';
		b.className = 'bpneo-title-btn';
		b.title = tip;
		b.setAttribute('aria-label', tip);
		var i = document.createElement('i');
		i.className = iconClass;
		b.appendChild(i);
		if (fn) {
			b.addEventListener('click', function (e) {
				e.preventDefault();
				e.stopPropagation();
				fn(b);
			});
		}
		return b;
	}

	function buildBar() {
		if (document.getElementById('antivandal-tools')) {
			return true;
		}
		var row = document.querySelector('.bpneo-title-actions[data-bpneo-actions]') ||
			document.querySelector('.bpneo-title-actions');
		if (!row) {
			return false;
		}

		var openBtn = neoBtn('ri-error-warning-fill', 'AntiVandal tools');
		openBtn.id = 'antivandal-open';

		var tools = document.createElement('span');
		tools.id = 'antivandal-tools';

		var closeBtn = neoBtn('ri-close-line', 'Close AntiVandal', function () {
			row.classList.remove('av-open');
		});
		tools.appendChild(closeBtn);
		tools.appendChild(neoBtn('ri-arrow-go-back-line', 'Revert last editor', openRevertDialog));
		tools.appendChild(neoBtn('ri-alarm-warning-line', 'Post a notice', function () { openWarnDialog(); }));
		tools.appendChild(neoBtn('ri-mail-send-line', 'New talk-page message', function () { openNewMessage(); }));
		tools.appendChild(neoBtn('ri-refresh-line', 'Latest revision', goLatest));
		var alertBtn = neoBtn('ri-radar-line', 'Alert on change', function () { toggleAlert(alertBtn); });
		tools.appendChild(alertBtn);
		tools.appendChild(neoBtn('ri-shield-keyhole-line', 'Request protection', requestProtection));
		tools.appendChild(neoBtn('ri-file-copy-line', 'Quick templates', function () { openQuickTemplates(); }));
		tools.appendChild(neoBtn('ri-pulse-line', 'Live patrol', togglePatrol));

		var moreWrap = document.createElement('span');
		moreWrap.style.position = 'relative';
		var moreBtn = neoBtn('ri-more-2-fill', 'More');
		var menu = document.createElement('div');
		menu.id = 'antivandal-more';
		function menuItem(label, fn) {
			var mb = document.createElement('button');
			mb.type = 'button';
			mb.textContent = label;
			mb.addEventListener('click', function (e) {
				e.stopPropagation();
				menu.style.display = 'none';
				fn();
			});
			menu.appendChild(mb);
		}
		menuItem('Report at AIV', reportAiv);
		menuItem('Report to administrators', function () { openAdminReport('admin'); });
		menuItem('Privacy / suppression request', function () { openAdminReport('privacy'); });
		menuItem('Multiple notices (this page)', openMat);
		menuItem('Pending review queue', openPendingReview);
		menuItem('Restore this revision', function () {
			var oldid = mw.util.getParamValue('oldid') || mw.config.get('wgRevisionId');
			if (!oldid) {
				toast('Open a specific revision first.', 'err');
				return;
			}
			if (!window.confirm('Replace the current page text with revision ' + oldid + '?')) {
				return;
			}
			toast('Restoring…');
			restoreRevision(oldid).then(function (res) {
				if (res.error) {
					throw new Error(res.error.info || res.error.code);
				}
				toast('Revision restored.', 'ok');
				setTimeout(function () { location.href = mw.util.getUrl(pageName()); }, 600);
			}).catch(function (err) {
				toast(err.message || String(err), 'err');
			});
		});
		menuItem('Preferences', openPrefs);
		moreBtn.addEventListener('click', function (e) {
			e.preventDefault();
			e.stopPropagation();
			menu.style.display = menu.style.display === 'block' ? 'none' : 'block';
		});
		moreWrap.appendChild(moreBtn);
		moreWrap.appendChild(menu);
		tools.appendChild(moreWrap);

		openBtn.addEventListener('click', function (e) {
			e.preventDefault();
			e.stopPropagation();
			row.classList.add('av-open');
		});

		var slot = document.getElementById('antivandal-slot');
		var editLink = row.querySelector('a.bpneo-title-btn[title="Edit"]') ||
			row.querySelector('a.bpneo-title-btn[href*="action=edit"]') ||
			row.querySelector('a.bpneo-title-btn .ri-edit-line') &&
				row.querySelector('a.bpneo-title-btn .ri-edit-line').closest('a');
		if (slot) {
			slot.replaceWith(openBtn);
		} else if (editLink) {
			row.insertBefore(openBtn, editLink);
		} else {
			row.insertBefore(openBtn, row.firstChild);
		}
		row.appendChild(tools);

		bindUserContextMenu();
		return true;
	}

	function init() {
		if (!mw.config.get('wgUserName')) {
			return;
		}
		injectCss();
		api = new mw.Api({ parameters: { formatversion: 2 } });
		var rights = mw.config.get('wgUserGroups') || [];
		hasRollback = rights.indexOf('rollbacker') !== -1 ||
			rights.indexOf('sysop') !== -1 ||
			rights.indexOf('steward') !== -1;
		if (!buildBar()) {
			var tries = 0;
			var t = setInterval(function () {
				tries += 1;
				if (buildBar() || tries > 25) {
					clearInterval(t);
				}
			}, 200);
		}
		bindContribsRollback();
	}

	if (document.readyState === 'loading') {
		document.addEventListener('DOMContentLoaded', function () {
			mw.loader.using(['mediawiki.api', 'mediawiki.util']).then(init);
		});
	} else {
		mw.loader.using(['mediawiki.api', 'mediawiki.util']).then(init);
	}
}());