Jump to content

User:SD0001/text-reactions.js

From Wikipedia, the free encyclopedia
This is an old revision of this page, as edited by SD0001 (talk | contribs) at 10:38, 23 November 2021 (Created page with 'var $textarea, api; var textReactions = [ { name: 'test', trigger: 'keyup', key: 'e', analyse: function (e) { return e; }, react: function (e) { console.log(e); } }, { test: 'dabNotification', trigger: 'keyup', key: ']', analyse: function (wikitext, cursorPosition) { var matches = /.*(\[\[([^[\]|]+)(?:\|.*]]|]]))$/.exec(wikitext.slice(0, cursorPosition)); if (matches) { var pageTitle = matches[matches.length - 1].tr...'). The present address (URL) is a permanent link to this revision, which may differ significantly from the current revision.
(diff) ← Previous revision | Latest revision (diff) | Newer revision → (diff)
Note: After saving, you have to bypass your browser's cache to see the changes. Google Chrome, Firefox, Microsoft Edge and Safari: Hold down the ⇧ Shift key and click the Reload toolbar button. For details and instructions about other browsers, see Wikipedia:Bypass your cache.
var $textarea, api;

var textReactions = [
	{
		name: 'test',
		trigger: 'keyup',
		key: 'e',
		analyse: function (e) {
			return e;
		},
		react: function (e) {
			console.log(e);
		}
	},
	{
		test: 'dabNotification',
		trigger: 'keyup',
		key: ']',
		analyse: function (wikitext, cursorPosition) {
			var matches = /.*(\[\[([^[\]|]+)(?:\|.*]]|]]))$/.exec(wikitext.slice(0, cursorPosition));
			if (matches) {
				var pageTitle = matches[matches.length - 1].trim();
				var linkWikitext = matches[matches.length - 2];
				return api.get({
					action: 'query',
					titles: pageTitle,
					prop: 'pageprops',
					ppprop: 'disambiguation',
				}).then(function (json) {
					if (json.query.pages && json.query.pages[0].pageprops &&
						Object.prototype.hasOwnProperty.call(json.query.pages[0].pageprops, 'disambiguation')
					) {
						return [pageTitle, linkWikitext, cursorPosition];
					}
				} );
			}
		},
		react: function (pageTitle, linkWikitext, cursorPosition) {

		}
	},
	{
		name: 'unreliableSourceWarning',
		trigger: 'interval',
		interval: 5000,
		analyse: function () {

		},
		react: function (wikitext) {

		}
	}
];

$.when(
	$.ready,
	mw.loader.using([ 'mediawiki.util', 'mediawiki.api', 'mediawiki.user', 'jquery.textSelection', 'es6-polyfills' ])
).then(function () {
	if (['edit', 'submit'].indexOf(mw.util.getParamValue('action')) === -1) return;

	$textarea = $('#wpTextbox1');
	if (!$textarea.length) return; // probably VE

	api = new mw.Api({
		parameters: {
			formatversion: 2
		}
	});

	var reaction = function (tr) {
		var args = Array.prototype.slice.call(arguments, 1);
		Promise.resolve(tr.analyse.apply(tr, args)).then(function (returnVals) {
			tr.reaction.apply(tr, returnVals);
		});
	}

	var keyupReactions = textReactions.filter(function (tr) {
		return tr.trigger === 'keyup'
	});
	var keyupHandler = function (e) {
		keyupReactions.forEach(function (tr) {
			if (e.key === tr.key) {
				reaction(tr, $textarea.textSelection('getContents'), $textarea.textSelection('getCaretPosition'));
			}
		});
	};
	mw.hook('ext.CodeMirror.switch').add(function (usingCodeMirror, $codeMirror) {
		$textarea = usingCodeMirror ? $codeMirror : $('#wpTextbox1');
		$textarea.on('keyup', keyupHandler);
	});

	textReactions.filter(function (tr) {
		return tr.trigger === 'interval';
	}).forEach(function (tr) {
		setInterval(function () {
			reaction(tr);
		}, tr.interval);
	});
});