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 | * ABC Wiki: | ||
* | * Missing imported article links should open Wikipedia in a new tab. | ||
* | * | ||
* Existing local pages stay local. | * Existing local pages stay local. | ||
* Missing article | * Missing article redlinks go to: | ||
* https://wikipedia.org/wiki/Page_Title | |||
*/ | */ | ||
(function () { | (function () { | ||
'use strict'; | 'use strict'; | ||
function | function getMissingArticleTitle(link) { | ||
var href | var href = link.getAttribute('href') || ''; | ||
var url; | |||
var title; | |||
if (!href) { | if (!href) { | ||
return null; | return null; | ||
| Line 35: | Line 39: | ||
title = title.replace(/_/g, ' '); | title = title.replace(/_/g, ' '); | ||
// Only | // Only normal article pages. | ||
// | // 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 | 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 = | var title = getMissingArticleTitle(link); | ||
var wikiTitle; | |||
var wikipediaUrl; | var wikipediaUrl; | ||
| Line 58: | Line 64: | ||
} | } | ||
wikipediaUrl = 'https:// | 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 | link.title = title + ' — opens on Wikipedia in a new tab'; | ||
}); | }); | ||
} | } | ||
if (document.readyState === 'loading') { | if (document.readyState === 'loading') { | ||
document.addEventListener('DOMContentLoaded', | document.addEventListener('DOMContentLoaded', rewriteRedlinksToWikipedia); | ||
} else { | } else { | ||
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 */