No edit summary
No edit summary
 
(13 intermediate revisions by the same user not shown)
Line 1: Line 1:
/**
/**
  * reFill.js — batch-fill bare URL refs via WMF Citoid
  * reFill.js
  * Toolbar button next to Cite (WikiEditor). BharatpediaNeo + CodeMirror safe.
* Fill bare / [url title] refs only.
* Do not rename or edit existing named refs.
  * New duplicate URLs use the next free :N.
  */
  */
(function () {
(function () {
'use strict';
'use strict';
var CITOID = 'https://en.wikipedia.org/api/rest_v1/data/citation/mediawiki/';
var CITOID = 'https://en.wikipedia.org/api/rest_v1/data/citation/mediawiki/';
var TYPE_MAP = {
var TYPE_MAP = {
artwork: 'Citation',
blogPost: 'Cite web', webpage: 'Cite web', email: 'Cite web', forumPost: 'Cite web',
audioRecording: 'Citation',
newspaperArticle: 'Cite news', magazineArticle: 'Cite news',
bill: 'Citation',
journalArticle: 'Cite journal', conferencePaper: 'Cite journal', presentation: 'Cite journal',
blogPost: 'Cite web',
book: 'Cite book', manuscript: 'Cite book', _default: 'Cite web'
book: 'Cite book',
bookSection: 'Citation',
case: 'Citation',
computerProgram: 'Citation',
conferencePaper: 'Cite journal',
dataset: 'Citation',
dictionaryEntry: 'Citation',
document: 'Citation',
email: 'Cite web',
encyclopediaArticle: 'Citation',
film: 'Citation',
forumPost: 'Cite web',
hearing: 'Citation',
instantMessage: 'Citation',
interview: 'Citation',
journalArticle: 'Cite journal',
letter: 'Citation',
magazineArticle: 'Cite news',
manuscript: 'Cite book',
map: 'Citation',
newspaperArticle: 'Cite news',
patent: 'Cite patent',
podcast: 'Citation',
preprint: 'Citation',
presentation: 'Cite journal',
radioBroadcast: 'Citation',
report: 'Cite report',
standard: 'Citation',
statute: 'Citation',
thesis: 'Cite thesis',
tvBroadcast: 'Citation',
videoRecording: 'Cite AV media',
webpage: 'Cite web',
_default: 'Cite web'
};
};


function $box() {
function $box() { return $('#wpTextbox1'); }
return $('#wpTextbox1');
}
 
function getWikitext() {
function getWikitext() {
var $t = $box();
var $t = $box();
if ($t.length && $.fn.textSelection) {
if ($t.length && $.fn.textSelection) return $t.textSelection('getContents');
return $t.textSelection('getContents');
}
return $t.length ? $t.val() : '';
return $t.length ? $t.val() : '';
}
}
function setWikitext(text) {
function setWikitext(text) {
var $t = $box();
var $t = $box();
if ($t.length && $.fn.textSelection) {
if ($t.length && $.fn.textSelection) { $t.textSelection('setContents', text); return; }
$t.textSelection('setContents', text);
if ($t.length) $t.val(text).trigger('input');
return;
}
}
function setSummary(ok, fail) {
if ($t.length) {
var $s = $('#wpSummary');
$t.val(text).trigger('input');
if (!$s.length) return;
}
var note = 'Filled in ' + ok + ' bare reference' + (ok === 1 ? '' : 's') + ' with [[Bharatpedia:reFill|reFill]]';
if (fail) note += ' (' + fail + ' failed)';
var cur = ($s.val() || '').trim();
if (!cur) $s.val(note);
else if (cur.toLowerCase().indexOf('refill') === -1) $s.val(cur.replace(/\s*$/, '') + '; ' + note);
$s.trigger('change');
}
function showDiff() {
var btn = document.getElementById('wpDiff');
if (btn) { btn.click(); return; }
var form = document.getElementById('editform');
if (!form) return;
var input = document.createElement('input');
input.type = 'hidden';
input.name = 'wpDiff';
input.value = 'Show changes';
form.appendChild(input);
form.submit();
}
function esc(s) { return String(s || '').replace(/\|/g, '{{!}}').replace(/\n/g, ' ').trim(); }
function hostTitle(url) {
try { return new URL(url).hostname.replace(/^www\./, ''); } catch (e) { return ''; }
}
}
function normUrl(url) { return String(url || '').trim().replace(/\/+$/, '').toLowerCase(); }


function esc(s) {
function usedAutoNames(text) {
return String(s || '').replace(/\|/g, '{{!}}').replace(/\n/g, ' ').trim();
var used = {}, maxN = -1, m;
var re = /<ref[^>]*\bname\s*=\s*(?:"([^"]+)"|'([^']+)'|([^\s>/]+))/gi;
while ((m = re.exec(text)) !== null) {
var n = m[1] || m[2] || m[3];
used[n] = true;
var a = String(n).match(/^:(\d+)$/);
if (a) maxN = Math.max(maxN, parseInt(a[1], 10));
}
return { used: used, next: maxN + 1 };
}
}


Line 80: Line 70:
var p = [];
var p = [];
var url = (item && item.url) || fallbackUrl;
var url = (item && item.url) || fallbackUrl;
if (url) {
if (url) p.push('url=' + esc(url));
p.push('url=' + esc(url));
var title = (item && item.title) || (url ? hostTitle(url) : '');
}
if (title) p.push('title=' + esc(title));
if (item && item.title) {
p.push('title=' + esc(item.title));
}
var site = item && (item.websiteTitle || item.publicationTitle || item.publisher);
var site = item && (item.websiteTitle || item.publicationTitle || item.publisher);
if (site) {
if (site) p.push((name === 'Cite news' || name === 'Cite journal' ? 'work=' : 'website=') + esc(site));
if (name === 'Cite news' || name === 'Cite journal') {
if (item && item.date) p.push('date=' + esc(item.date));
p.push('work=' + esc(site));
} else if (name === 'Cite book') {
p.push('publisher=' + esc(site));
} else {
p.push('website=' + esc(site));
}
}
if (item && item.date) {
p.push('date=' + esc(item.date));
}
if (item && item.author && item.author[0]) {
if (item && item.author && item.author[0]) {
var a = item.author[0];
var a = item.author[0];
if (a[1]) {
if (a[1]) p.push('last=' + esc(a[1]));
p.push('last=' + esc(a[1]));
if (a[0]) p.push('first=' + esc(a[0]));
}
if (a[0]) {
p.push('first=' + esc(a[0]));
}
}
}
p.push('access-date=' + new Date().toISOString().slice(0, 10));
p.push('access-date=' + new Date().toISOString().slice(0, 10));
Line 113: Line 86:


function findBare(wikitext) {
function findBare(wikitext) {
var re = /<ref(\s[^>]*)?>\s*(https?:\/\/[^\s<>[\]|]+)\s*<\/ref>/gi;
var out = [], m, seen = {};
var out = [], m;
function add(full, attrs, url, index) {
while ((m = re.exec(wikitext)) !== null) {
if (seen[index]) return;
out.push({ full: m[0], attrs: m[1] || '', url: m[2] });
if (/\bname\s*=/i.test(attrs || '')) return;
if (/\{\{\s*cite/i.test(full)) return;
seen[index] = true;
out.push({ full: full, url: url, index: index });
}
}
var reBare = /<ref(\s[^>]*)?>\s*(https?:\/\/[^\s<>[\]|{]+)\s*(?:\{\{[^}]+\}\})?\s*<\/ref>/gi;
while ((m = reBare.exec(wikitext)) !== null) add(m[0], m[1], m[2], m.index);
var reLink = /<ref(\s[^>]*)?>\s*\[(https?:\/\/[^\s\]]+)\s+[^\]]*\]\s*<\/ref>/gi;
while ((m = reLink.exec(wikitext)) !== null) add(m[0], m[1], m[2], m.index);
out.sort(function (a, b) { return a.index - b.index; });
return out;
return out;
}
}


function fetchOne(url) {
function fetchOne(url) {
return fetch(CITOID + encodeURIComponent(url), {
function once() {
headers: { Accept: 'application/json' }
return fetch(CITOID + encodeURIComponent(url), { headers: { Accept: 'application/json' } })
}).then(function (r) {
.then(function (r) {
if (!r.ok) {
if (!r.ok) throw new Error('HTTP ' + r.status);
throw new Error('HTTP ' + r.status);
return r.json();
}
})
return r.json();
.then(function (data) {
}).then(function (data) {
var item = Array.isArray(data) ? data[0] : data;
return Array.isArray(data) ? data[0] : data;
if (!item) throw new Error('empty');
return item;
});
}
return once().catch(function () {
return new Promise(function (res) { setTimeout(res, 700); }).then(once);
});
});
}
function setBusy(on, n) {
var nodes = document.querySelectorAll('#refill-toolbar-btn, #refill-bare-btn');
for (var i = 0; i < nodes.length; i++) {
nodes[i].disabled = !!on;
if (nodes[i].id === 'refill-toolbar-btn') {
nodes[i].textContent = on ? ('Filling ' + n + '…') : 'reFill';
} else {
nodes[i].textContent = on ? ('Filling ' + n + '…') : 'Fill bare URLs';
}
}
}
}


function run() {
function run() {
var text = getWikitext();
var text = getWikitext();
if (!text) {
if (!text) { window.alert('reFill: no editor text.'); return; }
window.alert('reFill: no editor text.');
 
return;
var names = usedAutoNames(text);
}
var bare = findBare(text);
var bare = findBare(text);
if (!bare.length) {
if (!bare.length) {
window.alert('reFill: no <ref>https://…</ref> found.');
window.alert('reFill finished: 0 filled, 0 failed.');
return;
return;
}
}
setBusy(true, bare.length);
 
var tab = document.querySelector('.tab-refill, [rel="refill"]');
if (tab) tab.textContent = 'Filling…';
 
var groups = {};
var order = [];
bare.forEach(function (item) {
var key = normUrl(item.url);
if (!groups[key]) {
groups[key] = { url: item.url, count: 0, first: true, name: null };
order.push(key);
}
groups[key].count++;
});
 
var next = names.next;
order.forEach(function (key) {
if (groups[key].count > 1) {
while (names.used[':' + next]) next++;
groups[key].name = ':' + next;
names.used[':' + next] = true;
next++;
}
});
 
var metaByUrl = {};
var chain = Promise.resolve();
var chain = Promise.resolve();
var ok = 0;
var fail = 0;
var fail = 0;
bare.forEach(function (item) {
order.forEach(function (key) {
chain = chain.then(function () {
chain = chain.then(function () {
return fetchOne(item.url).then(function (meta) {
return fetchOne(groups[key].url).then(function (meta) {
text = text.replace(
metaByUrl[key] = meta;
item.full,
'<ref' + item.attrs + '>' + templateFromCitoid(meta || {}, item.url) + '</ref>'
);
ok++;
}).catch(function () {
}).catch(function () {
fail++;
fail++;
metaByUrl[key] = { url: groups[key].url };
}).then(function () {
}).then(function () {
return new Promise(function (res) {
return new Promise(function (res) { setTimeout(res, 350); });
setTimeout(res, 350);
});
});
});
});
});
});
});
chain.then(function () {
chain.then(function () {
var ok = 0;
bare.forEach(function (item) {
var key = normUrl(item.url);
var g = groups[key];
var meta = metaByUrl[key] || { url: item.url };
var neu;
if (!g.name) {
neu = '<ref>' + templateFromCitoid(meta, item.url) + '</ref>';
} else if (g.first) {
g.first = false;
neu = '<ref name="' + g.name + '">' + templateFromCitoid(meta, item.url) + '</ref>';
} else {
neu = '<ref name="' + g.name + '" />';
}
var pos = text.indexOf(item.full);
if (pos !== -1) {
text = text.slice(0, pos) + neu + text.slice(pos + item.full.length);
ok++;
}
});
setWikitext(text);
setWikitext(text);
setBusy(false);
setSummary(ok, fail);
window.alert('reFill finished: ' + ok + ' filled, ' + fail + ' failed. Check the text, then Preview.');
if (tab) tab.textContent = 'reFill';
window.alert('reFill finished: ' + ok + ' filled, ' + fail + ' failed.');
showDiff();
});
});
}
}


function addWikiEditorSection($textarea) {
function addSection($textarea) {
if (!$textarea || !$textarea.length || !$textarea.wikiEditor) {
if (!$textarea || !$textarea.length || typeof $textarea.wikiEditor !== 'function') return;
return false;
if ($textarea.data('refillAdded')) return;
}
if ($textarea.data('refillToolbarAdded')) {
return true;
}
try {
try {
$textarea.wikiEditor('addToToolbar', {
$textarea.wikiEditor('addToToolbar', { sections: { refill: { type: 'toolbar', label: 'reFill' } } });
sections: {
$textarea.data('refillAdded', true);
refill: {
} catch (e) {}
type: 'toolbar',
label: 'reFill'
}
}
});
$textarea.wikiEditor('addToToolbar', {
section: 'refill',
groups: {
refill: {
label: '',
tools: {
fillbare: {
label: 'Fill bare URLs',
type: 'button',
action: {
type: 'callback',
execute: function () {
run();
}
}
}
}
}
}
});
$textarea.data('refillToolbarAdded', true);
return true;
} catch (e) {
return false;
}
}
}
 
function bindTab() {
function addTabBesideCite() {
var root = document.querySelector('.wikiEditor-ui-toolbar');
if (document.getElementById('refill-toolbar-btn')) {
if (!root || root.getAttribute('data-refill-bound')) return;
return true;
root.setAttribute('data-refill-bound', '1');
}
root.addEventListener('click', function (e) {
var cite = document.querySelector(
var t = e.target.closest('.tab-refill, [rel="refill"], .wikiEditor-ui-toolbar [rel="refill"]');
'.wikiEditor-ui-toolbar .tab-cite, .wikiEditor-ui-toolbar [rel="cite"], .wikiEditor-ui-toolbar .tabs .tab[rel="cite"]'
if (!t) return;
);
var tabs = document.querySelector('.wikiEditor-ui-toolbar .tabs, .wikiEditor-ui-toolbar .wikiEditor-ui-tabs');
if (!cite && !tabs) {
return false;
}
var btn = document.createElement('button');
btn.id = 'refill-toolbar-btn';
btn.type = 'button';
btn.textContent = 'reFill';
btn.title = 'Fill bare URL references';
btn.className = 'tab tab-refill';
btn.style.cssText = 'margin-left:8px;padding:2px 8px;cursor:pointer;font:inherit;background:transparent;border:0;color:inherit;';
btn.addEventListener('click', function (e) {
e.preventDefault();
e.preventDefault();
e.stopPropagation();
e.stopPropagation();
run();
run();
});
}, true);
if (cite && cite.parentNode) {
cite.parentNode.insertBefore(btn, cite.nextSibling);
} else {
tabs.appendChild(btn);
}
return true;
}
 
function addFallbackFloat() {
if (document.getElementById('refill-bare-btn') || document.getElementById('refill-toolbar-btn')) {
return;
}
var btn = document.createElement('button');
btn.id = 'refill-bare-btn';
btn.type = 'button';
btn.textContent = 'Fill bare URLs';
btn.style.cssText = 'position:fixed;right:16px;bottom:16px;z-index:2147483647;padding:10px 14px;font:600 14px sans-serif;color:#fff;background:#b32424;border:0;border-radius:6px;cursor:pointer;';
btn.addEventListener('click', function (e) {
e.preventDefault();
run();
});
document.body.appendChild(btn);
}
}
function tryPlace() {
addWikiEditorSection($box());
if (!addTabBesideCite()) {
addFallbackFloat();
} else {
var f = document.getElementById('refill-bare-btn');
if (f) {
f.remove();
}
}
}
function start() {
function start() {
tryPlace();
addSection($box());
if (mw.hook) {
bindTab();
mw.hook('wikiEditor.toolbarReady').add(function ($textarea) {
if (mw.hook) mw.hook('wikiEditor.toolbarReady').add(function ($t) { addSection($t); bindTab(); });
addWikiEditorSection($textarea);
setTimeout(function () { addSection($box()); bindTab(); }, 800);
addTabBesideCite();
var f = document.getElementById('refill-bare-btn');
if (f) {
f.remove();
}
});
}
setTimeout(tryPlace, 400);
setTimeout(tryPlace, 1500);
setTimeout(tryPlace, 4000);
}
 
if (document.readyState === 'loading') {
document.addEventListener('DOMContentLoaded', function () {
mw.loader.using(['jquery', 'jquery.textSelection', 'ext.wikiEditor']).then(start).catch(start);
});
} else if (mw.loader && mw.loader.using) {
mw.loader.using(['jquery', 'jquery.textSelection', 'ext.wikiEditor']).then(start).catch(start);
} else {
start();
}
}
mw.loader.using(['jquery', 'jquery.textSelection', 'ext.wikiEditor']).then(start).catch(start);
})();
})();

Latest revision as of 12:02, 29 September 2026

/**
 * reFill.js
 * Fill bare / [url title] refs only.
 * Do not rename or edit existing named refs.
 * New duplicate URLs use the next free :N.
 */
(function () {
	'use strict';
	var CITOID = 'https://en.wikipedia.org/api/rest_v1/data/citation/mediawiki/';
	var TYPE_MAP = {
		blogPost: 'Cite web', webpage: 'Cite web', email: 'Cite web', forumPost: 'Cite web',
		newspaperArticle: 'Cite news', magazineArticle: 'Cite news',
		journalArticle: 'Cite journal', conferencePaper: 'Cite journal', presentation: 'Cite journal',
		book: 'Cite book', manuscript: 'Cite book', _default: 'Cite web'
	};

	function $box() { return $('#wpTextbox1'); }
	function getWikitext() {
		var $t = $box();
		if ($t.length && $.fn.textSelection) return $t.textSelection('getContents');
		return $t.length ? $t.val() : '';
	}
	function setWikitext(text) {
		var $t = $box();
		if ($t.length && $.fn.textSelection) { $t.textSelection('setContents', text); return; }
		if ($t.length) $t.val(text).trigger('input');
	}
	function setSummary(ok, fail) {
		var $s = $('#wpSummary');
		if (!$s.length) return;
		var note = 'Filled in ' + ok + ' bare reference' + (ok === 1 ? '' : 's') + ' with [[Bharatpedia:reFill|reFill]]';
		if (fail) note += ' (' + fail + ' failed)';
		var cur = ($s.val() || '').trim();
		if (!cur) $s.val(note);
		else if (cur.toLowerCase().indexOf('refill') === -1) $s.val(cur.replace(/\s*$/, '') + '; ' + note);
		$s.trigger('change');
	}
	function showDiff() {
		var btn = document.getElementById('wpDiff');
		if (btn) { btn.click(); return; }
		var form = document.getElementById('editform');
		if (!form) return;
		var input = document.createElement('input');
		input.type = 'hidden';
		input.name = 'wpDiff';
		input.value = 'Show changes';
		form.appendChild(input);
		form.submit();
	}
	function esc(s) { return String(s || '').replace(/\|/g, '{{!}}').replace(/\n/g, ' ').trim(); }
	function hostTitle(url) {
		try { return new URL(url).hostname.replace(/^www\./, ''); } catch (e) { return ''; }
	}
	function normUrl(url) { return String(url || '').trim().replace(/\/+$/, '').toLowerCase(); }

	function usedAutoNames(text) {
		var used = {}, maxN = -1, m;
		var re = /<ref[^>]*\bname\s*=\s*(?:"([^"]+)"|'([^']+)'|([^\s>/]+))/gi;
		while ((m = re.exec(text)) !== null) {
			var n = m[1] || m[2] || m[3];
			used[n] = true;
			var a = String(n).match(/^:(\d+)$/);
			if (a) maxN = Math.max(maxN, parseInt(a[1], 10));
		}
		return { used: used, next: maxN + 1 };
	}

	function templateFromCitoid(item, fallbackUrl) {
		var name = TYPE_MAP[item && item.itemType] || TYPE_MAP._default;
		var p = [];
		var url = (item && item.url) || fallbackUrl;
		if (url) p.push('url=' + esc(url));
		var title = (item && item.title) || (url ? hostTitle(url) : '');
		if (title) p.push('title=' + esc(title));
		var site = item && (item.websiteTitle || item.publicationTitle || item.publisher);
		if (site) p.push((name === 'Cite news' || name === 'Cite journal' ? 'work=' : 'website=') + esc(site));
		if (item && item.date) p.push('date=' + esc(item.date));
		if (item && item.author && item.author[0]) {
			var a = item.author[0];
			if (a[1]) p.push('last=' + esc(a[1]));
			if (a[0]) p.push('first=' + esc(a[0]));
		}
		p.push('access-date=' + new Date().toISOString().slice(0, 10));
		return '{{' + name + '|' + p.join('|') + '}}';
	}

	function findBare(wikitext) {
		var out = [], m, seen = {};
		function add(full, attrs, url, index) {
			if (seen[index]) return;
			if (/\bname\s*=/i.test(attrs || '')) return;
			if (/\{\{\s*cite/i.test(full)) return;
			seen[index] = true;
			out.push({ full: full, url: url, index: index });
		}
		var reBare = /<ref(\s[^>]*)?>\s*(https?:\/\/[^\s<>[\]|{]+)\s*(?:\{\{[^}]+\}\})?\s*<\/ref>/gi;
		while ((m = reBare.exec(wikitext)) !== null) add(m[0], m[1], m[2], m.index);
		var reLink = /<ref(\s[^>]*)?>\s*\[(https?:\/\/[^\s\]]+)\s+[^\]]*\]\s*<\/ref>/gi;
		while ((m = reLink.exec(wikitext)) !== null) add(m[0], m[1], m[2], m.index);
		out.sort(function (a, b) { return a.index - b.index; });
		return out;
	}

	function fetchOne(url) {
		function once() {
			return fetch(CITOID + encodeURIComponent(url), { headers: { Accept: 'application/json' } })
				.then(function (r) {
					if (!r.ok) throw new Error('HTTP ' + r.status);
					return r.json();
				})
				.then(function (data) {
					var item = Array.isArray(data) ? data[0] : data;
					if (!item) throw new Error('empty');
					return item;
				});
		}
		return once().catch(function () {
			return new Promise(function (res) { setTimeout(res, 700); }).then(once);
		});
	}

	function run() {
		var text = getWikitext();
		if (!text) { window.alert('reFill: no editor text.'); return; }

		var names = usedAutoNames(text);
		var bare = findBare(text);
		if (!bare.length) {
			window.alert('reFill finished: 0 filled, 0 failed.');
			return;
		}

		var tab = document.querySelector('.tab-refill, [rel="refill"]');
		if (tab) tab.textContent = 'Filling…';

		var groups = {};
		var order = [];
		bare.forEach(function (item) {
			var key = normUrl(item.url);
			if (!groups[key]) {
				groups[key] = { url: item.url, count: 0, first: true, name: null };
				order.push(key);
			}
			groups[key].count++;
		});

		var next = names.next;
		order.forEach(function (key) {
			if (groups[key].count > 1) {
				while (names.used[':' + next]) next++;
				groups[key].name = ':' + next;
				names.used[':' + next] = true;
				next++;
			}
		});

		var metaByUrl = {};
		var chain = Promise.resolve();
		var fail = 0;
		order.forEach(function (key) {
			chain = chain.then(function () {
				return fetchOne(groups[key].url).then(function (meta) {
					metaByUrl[key] = meta;
				}).catch(function () {
					fail++;
					metaByUrl[key] = { url: groups[key].url };
				}).then(function () {
					return new Promise(function (res) { setTimeout(res, 350); });
				});
			});
		});

		chain.then(function () {
			var ok = 0;
			bare.forEach(function (item) {
				var key = normUrl(item.url);
				var g = groups[key];
				var meta = metaByUrl[key] || { url: item.url };
				var neu;
				if (!g.name) {
					neu = '<ref>' + templateFromCitoid(meta, item.url) + '</ref>';
				} else if (g.first) {
					g.first = false;
					neu = '<ref name="' + g.name + '">' + templateFromCitoid(meta, item.url) + '</ref>';
				} else {
					neu = '<ref name="' + g.name + '" />';
				}
				var pos = text.indexOf(item.full);
				if (pos !== -1) {
					text = text.slice(0, pos) + neu + text.slice(pos + item.full.length);
					ok++;
				}
			});
			setWikitext(text);
			setSummary(ok, fail);
			if (tab) tab.textContent = 'reFill';
			window.alert('reFill finished: ' + ok + ' filled, ' + fail + ' failed.');
			showDiff();
		});
	}

	function addSection($textarea) {
		if (!$textarea || !$textarea.length || typeof $textarea.wikiEditor !== 'function') return;
		if ($textarea.data('refillAdded')) return;
		try {
			$textarea.wikiEditor('addToToolbar', { sections: { refill: { type: 'toolbar', label: 'reFill' } } });
			$textarea.data('refillAdded', true);
		} catch (e) {}
	}
	function bindTab() {
		var root = document.querySelector('.wikiEditor-ui-toolbar');
		if (!root || root.getAttribute('data-refill-bound')) return;
		root.setAttribute('data-refill-bound', '1');
		root.addEventListener('click', function (e) {
			var t = e.target.closest('.tab-refill, [rel="refill"], .wikiEditor-ui-toolbar [rel="refill"]');
			if (!t) return;
			e.preventDefault();
			e.stopPropagation();
			run();
		}, true);
	}
	function start() {
		addSection($box());
		bindTab();
		if (mw.hook) mw.hook('wikiEditor.toolbarReady').add(function ($t) { addSection($t); bindTab(); });
		setTimeout(function () { addSection($box()); bindTab(); }, 800);
	}
	mw.loader.using(['jquery', 'jquery.textSelection', 'ext.wikiEditor']).then(start).catch(start);
})();