MediaWiki:Common.js: Difference between revisions

From Phuketer
ABC wiki: send missing imported article links to English Wikipedia in new tab
 
ABC wiki: make missing article redlinks open Wikipedia in new tab
Line 1: Line 1:


/* ABC-WIKIPEDIA-REDLINKS-START */
/**
/**
  * ABC Wiki compatibility:
  * ABC Wiki:
  * Imported Wikipedia articles should work as entrance pages.
  * Missing imported article links should open Wikipedia in a new tab.
  *
  *
  * Existing local pages stay local.
  * Existing local pages stay local.
  * Missing article links (redlinks) open the matching English Wikipedia page in a new tab.
  * Missing article redlinks go to:
* https://wikipedia.org/wiki/Page_Title
  */
  */
(function () {
(function () {
'use strict';
'use strict';


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


href = link.getAttribute('href') || '';
if (!href) {
if (!href) {
return null;
return null;
Line 35: Line 39:
title = title.replace(/_/g, ' ');
title = title.replace(/_/g, ' ');


// Only auto-send normal article links to Wikipedia.
// Only normal article pages.
// Avoid Template:, Module:, File:, Category:, User:, Talk:, Special:, 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 44: Line 48:
}
}


function rewriteMissingWikipediaLinks() {
function rewriteRedlinksToWikipedia() {
var content = document.querySelector('.mw-parser-output');
var content = document.querySelector('.mw-parser-output');
if (!content) {
if (!content) {
return;
return;
Line 51: Line 56:


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


Line 58: Line 64:
}
}


wikipediaUrl = 'https://en.wikipedia.org/wiki/' + encodeURIComponent(title.replace(/ /g, '_'));
wikiTitle = title.replace(/ /g, '_');
wikipediaUrl = 'https://wikipedia.org/wiki/' + encodeURIComponent(wikiTitle);


link.href = wikipediaUrl;
link.href = wikipediaUrl;
Line 65: Line 72:
link.classList.remove('new');
link.classList.remove('new');
link.classList.add('abc-wikipedia-missing-link');
link.classList.add('abc-wikipedia-missing-link');
link.title = title + ' — opens on English Wikipedia in a new tab';
link.title = title + ' — opens on Wikipedia in a new tab';
});
});
}
}


if (document.readyState === 'loading') {
if (document.readyState === 'loading') {
document.addEventListener('DOMContentLoaded', rewriteMissingWikipediaLinks);
document.addEventListener('DOMContentLoaded', rewriteRedlinksToWikipedia);
} else {
} else {
rewriteMissingWikipediaLinks();
rewriteRedlinksToWikipedia();
}
}
}());
}());
/* ABC-WIKIPEDIA-REDLINKS-END */

Revision as of 21:01, 15 June 2026

/* ABC-WIKIPEDIA-REDLINKS-START */
/**
 * ABC Wiki:
 * Missing imported article links should open Wikipedia in a new tab.
 *
 * Existing local pages stay local.
 * Missing article redlinks go to:
 * https://wikipedia.org/wiki/Page_Title
 */
(function () {
	'use strict';

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

		if (!href) {
			return null;
		}

		try {
			url = new URL(href, window.location.origin);
		} catch (e) {
			return null;
		}

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

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

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

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

		return title;
	}

	function rewriteRedlinksToWikipedia() {
		var content = document.querySelector('.mw-parser-output');

		if (!content) {
			return;
		}

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

			if (!title) {
				return;
			}

			wikiTitle = title.replace(/ /g, '_');
			wikipediaUrl = 'https://wikipedia.org/wiki/' + encodeURIComponent(wikiTitle);

			link.href = wikipediaUrl;
			link.target = '_blank';
			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') {
		document.addEventListener('DOMContentLoaded', rewriteRedlinksToWikipedia);
	} else {
		rewriteRedlinksToWikipedia();
	}
}());
/* ABC-WIKIPEDIA-REDLINKS-END */