MediaWiki:Gadget-UnsupportedTitles.js
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.
/*
To get template for titleTag.textContent, process MediaWiki:pagetitle,
open your browser's JavaScript console on a Wiktionary page
and run this code:
((msg) => new mw.Api().getMessages(msg).then((messages) => {
mw.messages.set(messages);
const titleTemplate = new mw.Message(mw.messages, msg, ["$1"]).text();
const {
before,
after
} = /^(?<before>.*)\$1(?<after>.*)/.exec(titleTemplate).groups;
console.log(before, after)
})
.fail(console.error))("pagetitle")
We can do this for action=edit and action=submit by replacing
"pagetitle" with "editing" in this snippet.
*/
function setTitle(newTitle) {
// Set title tag, which determines the title shown in the browser.
var titleTag = document.getElementsByTagName('title')[0];
titleTag.textContent = newTitle + " - Wiktionary";
// Set the text of the first heading above the page content.
document.getElementById('firstHeading').textContent = newTitle;
}
var UnsupportedTitlesJson = require('./UnsupportedTitles.json');
var wgPageName = mw.config.get('wgPageName');
/*
* On subpages of Unsupported titles and Talk:Unsupported titles, show the
* correct title in the header at the top of the page. For instance, on the page
* [[Unsupported titles/Left curly bracket]], show the title {.
*
* This is not enabled in the mobile version of the site.
*
* For all such pages, see [[Special:PrefixIndex/Unsupported titles]].
*/
if (mw.config.get('wgAction') === 'view'
&& (/^(?:Talk:)?Unsupported_titles\//.test(wgPageName)
|| UnsupportedTitlesJson.TitleDisplay.hasOwnProperty(wgPageName))) {
$(function() {
try {
var match = /^(?:Talk:)?Unsupported_titles\/(.+)$/.exec(wgPageName);
var newTitle;
if (match) {
var subpage = match && match[1];
newTitle = UnsupportedTitlesJson.UnsupportedTitles[subpage] || subpage;
} else {
newTitle = UnsupportedTitlesJson.TitleDisplay[wgPageName];
}
if (mw.config.get('wgCanonicalNamespace') == 'Talk')
newTitle = 'Talk:' + newTitle;
setTitle(newTitle);
} catch (e) {
console.log('Error while changing title: ' + e.message + '.');
}
});
}
/*
* This redirects a user to the correct Unsupported titles page if they attempt
* to access an invalid title. For instance, if you attempt to access the page
* for <
* https://en.wiktionary.org/wiki/%3C
* you will be redirected to
* https://en.wiktionary.org/wiki/Unsupported_titles/Less_than
*/
if (mw.config.get('wgCanonicalSpecialPageName') == 'Badtitle') {
/*
* Get the name of the page that the user attempted to access using either
* the portion of the URL after "/wiki/" or the value of the "title"
* parameter in the query. That is, the user is accessing the page using
* either the path /wiki/<invalid title> or a path similar to
* /w/index.php?title=<invalid title>.
*
* The actual page displayed if a user attempts to access an invalid title
* is the special page with the canonical title [[Special:Badtitle]].
*/
var rxArticlePath = new RegExp('^' + mw.config.get('wgArticlePath').replace('$1', '(.*)') + '$');
var m = rxArticlePath.exec(location.pathname);
var title = m ? decodeURIComponent(m[1]) : mw.util.getParamValue('title');
var badTitleSubpage = UnsupportedTitlesJson.BadTitles[title];
if (badTitleSubpage) {
location.href = mw.util.getUrl('Unsupported titles/' + badTitleSubpage);
}
}