User:Xiplus/js/enable-discussion-tools.js

From Meta, a Wikimedia project coordination wiki

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)
  • Internet Explorer / Edge: Hold Ctrl while clicking Refresh, or press Ctrl-F5
  • Opera: Press Ctrl-F5.
/* eslint-disable no-console */
// <nowiki>
(function() {

	var updateLinks = function() {
		$('a').each(function(i, e) {
			var shorthref = e.getAttribute('href') || '';
			if (shorthref.match(/^(|#)$/)) {
				return;
			}
			try {
				var url = new URL(e.href);
				if (url.host.match(/\.(wikipedia|wiktionary|wikiquote|wikisource|wikinews|wikivoyage|wikibooks|wikiversity|wikimedia|mediawiki|wikidata)\.org$/)) {
					url.searchParams.set('dtenable', '1');
					e.href = url.href;
				}
			} catch (err) {
				console.error(e, err);
			}
		});
	};


	if (mw.util.getParamValue('dtenable')) {
		setTimeout(updateLinks, 1000);
	} else {
		var url = new URL(window.location.href);
		url.searchParams.set('dtenable', '1');
		mw.util.addPortletLink(
			'p-tb',
			url.href,
			'Enable DiscussionTools',
			't-dtenable'
		);
	}

}
)();
// </nowiki>