MediaWiki:Common.js: Difference between revisions

From Phuketer
ABC wiki: make missing article redlinks open Wikipedia in new tab
ABC wiki: force missing article redlinks to open Wikipedia in new tab
 
Line 4: Line 4:
/**
/**
  * ABC Wiki:
  * ABC Wiki:
  * Missing imported article links should open Wikipedia in a new tab.
  * Red/missing article links imported from Wikipedia should open Wikipedia,
* not the local missing-page editor.
  *
  *
  * Existing local pages stay local.
  * Local existing pages are untouched.
* Missing article redlinks go to:
* https://wikipedia.org/wiki/Page_Title
  */
  */
(function () {
(function () {
'use strict';
'use strict';
function decodeTitle(title) {
try {
title = decodeURIComponent(title);
} catch (e) {}
return title.replace(/_/g, ' ');
}
function titleToWikipediaUrl(title) {
return 'https://wikipedia.org/wiki/' + encodeURIComponent(title.replace(/ /g, '_'));
}


function getMissingArticleTitle(link) {
function getMissingArticleTitle(link) {
var href = link.getAttribute('href') || '';
var href = link.getAttribute('href') || '';
var title = null;
var match;
var url;
var url;
var title;


if (!href) {
if (!href) {
Line 22: Line 34:
}
}


/* Format: /w/index.php?title=Thai_Airways_Flight_365&action=edit&redlink=1 */
try {
try {
url = new URL(href, window.location.origin);
url = new URL(href, window.location.origin);
} catch (e) {
return null;
}


if (url.searchParams.get('redlink') !== '1') {
if (url.searchParams.get('redlink') === '1') {
return null;
title = url.searchParams.get('title');
}
} catch (e) {}
 
/* Fallback: catch title manually */
if (!title) {
match = href.match(/[?&]title=([^&]+)/);
if (match && href.indexOf('redlink=1') !== -1) {
title = match[1];
}
}
}


title = url.searchParams.get('title');
if (!title) {
if (!title) {
return null;
return null;
}
}


title = title.replace(/_/g, ' ');
title = decodeTitle(title);


// Only normal article pages.
/* Only normal article titles. Do not send Template:, Module:, File:, Category:, etc. */
// Do not redirect Template:, Module:, File:, Category:, User:, Talk:, Special:, etc.
if (title.indexOf(':') !== -1) {
if (title.indexOf(':') !== -1) {
return null;
return null;
Line 48: Line 65:
}
}


function rewriteRedlinksToWikipedia() {
function rewriteOne(link) {
var content = document.querySelector('.mw-parser-output');
var title = getMissingArticleTitle(link);
var wikipediaUrl;


if (!content) {
if (!title) {
return;
return;
}
}


content.querySelectorAll('a.new').forEach(function (link) {
wikipediaUrl = titleToWikipediaUrl(title);
var title = getMissingArticleTitle(link);
var wikiTitle;
var wikipediaUrl;


if (!title) {
link.setAttribute('href', wikipediaUrl);
return;
link.setAttribute('target', '_blank');
}
link.setAttribute('rel', 'noopener noreferrer');
 
link.classList.remove('new');
link.classList.add('abc-wikipedia-missing-link');


wikiTitle = title.replace(/ /g, '_');
link.setAttribute('title', title + ' — opens on Wikipedia in a new tab');
wikipediaUrl = 'https://wikipedia.org/wiki/' + encodeURIComponent(wikiTitle);
link.setAttribute('data-abc-wikipedia-title', title);
}


link.href = wikipediaUrl;
function rewriteAllRedlinks() {
link.target = '_blank';
document.querySelectorAll('a.new, a[href*="redlink=1"], a[href*="action=edit"]').forEach(rewriteOne);
link.rel = 'noopener noreferrer';
link.classList.remove('new');
link.classList.add('abc-wikipedia-missing-link');
link.title = title + ' — opens on Wikipedia in a new tab';
});
}
}


if (document.readyState === 'loading') {
if (document.readyState === 'loading') {
document.addEventListener('DOMContentLoaded', rewriteRedlinksToWikipedia);
document.addEventListener('DOMContentLoaded', rewriteAllRedlinks);
} else {
} else {
rewriteRedlinksToWikipedia();
rewriteAllRedlinks();
}
}
/* Some skins/modules add or touch content after ready. */
window.setTimeout(rewriteAllRedlinks, 500);
window.setTimeout(rewriteAllRedlinks, 1500);
/* Debug marker: check browser console with window.abcWikipediaRedlinksActive */
window.abcWikipediaRedlinksActive = true;
}());
}());
/* ABC-WIKIPEDIA-REDLINKS-END */
/* ABC-WIKIPEDIA-REDLINKS-END */

Latest revision as of 21:13, 15 June 2026

/* ABC-WIKIPEDIA-REDLINKS-START */
/**
 * ABC Wiki:
 * Red/missing article links imported from Wikipedia should open Wikipedia,
 * not the local missing-page editor.
 *
 * Local existing pages are untouched.
 */
(function () {
	'use strict';

	function decodeTitle(title) {
		try {
			title = decodeURIComponent(title);
		} catch (e) {}

		return title.replace(/_/g, ' ');
	}

	function titleToWikipediaUrl(title) {
		return 'https://wikipedia.org/wiki/' + encodeURIComponent(title.replace(/ /g, '_'));
	}

	function getMissingArticleTitle(link) {
		var href = link.getAttribute('href') || '';
		var title = null;
		var match;
		var url;

		if (!href) {
			return null;
		}

		/* Format: /w/index.php?title=Thai_Airways_Flight_365&action=edit&redlink=1 */
		try {
			url = new URL(href, window.location.origin);

			if (url.searchParams.get('redlink') === '1') {
				title = url.searchParams.get('title');
			}
		} catch (e) {}

		/* Fallback: catch title manually */
		if (!title) {
			match = href.match(/[?&]title=([^&]+)/);
			if (match && href.indexOf('redlink=1') !== -1) {
				title = match[1];
			}
		}

		if (!title) {
			return null;
		}

		title = decodeTitle(title);

		/* Only normal article titles. Do not send Template:, Module:, File:, Category:, etc. */
		if (title.indexOf(':') !== -1) {
			return null;
		}

		return title;
	}

	function rewriteOne(link) {
		var title = getMissingArticleTitle(link);
		var wikipediaUrl;

		if (!title) {
			return;
		}

		wikipediaUrl = titleToWikipediaUrl(title);

		link.setAttribute('href', wikipediaUrl);
		link.setAttribute('target', '_blank');
		link.setAttribute('rel', 'noopener noreferrer');

		link.classList.remove('new');
		link.classList.add('abc-wikipedia-missing-link');

		link.setAttribute('title', title + ' — opens on Wikipedia in a new tab');
		link.setAttribute('data-abc-wikipedia-title', title);
	}

	function rewriteAllRedlinks() {
		document.querySelectorAll('a.new, a[href*="redlink=1"], a[href*="action=edit"]').forEach(rewriteOne);
	}

	if (document.readyState === 'loading') {
		document.addEventListener('DOMContentLoaded', rewriteAllRedlinks);
	} else {
		rewriteAllRedlinks();
	}

	/* Some skins/modules add or touch content after ready. */
	window.setTimeout(rewriteAllRedlinks, 500);
	window.setTimeout(rewriteAllRedlinks, 1500);

	/* Debug marker: check browser console with window.abcWikipediaRedlinksActive */
	window.abcWikipediaRedlinksActive = true;
}());
/* ABC-WIKIPEDIA-REDLINKS-END */