User:Zackmann08/scripts/sandboxlinks.js
Appearance
Code that you insert on this page could contain malicious content capable of compromising your account. If you import a script from another page with "importScript", "mw.loader.load", "iusc", or "lusc", take note that this causes you to dynamically load a remote script, which could be changed by others. Editors are responsible for all edits and actions they perform, including by scripts. User scripts are not centrally supported and may malfunction or become inoperable due to software changes. A guide to help you find broken scripts is available. If you are unsure whether code you are adding to this page is safe, you can ask at the appropriate village pump.
This code will be executed when previewing this page.
This code will be executed when previewing this page.
Documentation for this user script can be added at User:Zackmann08/scripts/sandboxlinks.
// Adds [sandbox] and [testcases] to the documentation links
// Clicking on [sandbox] and [testcases] will view those respective pages
async function pageExistsForSandbox(title) {
const api = new mw.Api();
const response = await api.get({
action: 'query',
titles: title,
format: 'json'
});
// Check if the 'missing' property is present in the page object
const pages = response.query.pages;
const pageId = Object.keys(pages)[0];
return !pages[pageId].hasOwnProperty('missing');
}
// TODO: turn links red if the relevant page does not exist
jQuery(document).ready(function($) {
if((mw.config.get('wgNamespaceNumber') == 10 || mw.config.get('wgNamespaceNumber') == 828) && document.getElementsByClassName('mw-editsection-like plainlinks')) {
var sandboxExists, testcasesExists;
var pagename = mw.config.get('wgPageName');
(async () => {
sandboxExists = await pageExistsForSandbox(pagename+'/sandbox');
testcasesExists = await pageExistsForSandbox(pagename.replace(/\/sandbox/g, '')+'/testcases');
var links = document.getElementsByClassName('mw-editsection-like plainlinks')[0];
var sandbox = /\/sandbox$/;
var sandboxText = ''
+ (sandbox.test(pagename) ? '<span rel="mw:WikiLink" style="font-weight:bold">sandbox</span> | ' : '<a rel="mw:WikiLink" href="/wiki/'
+ pagename.replace(/\/sandbox/g, '')
+ '/sandbox"'
+ (sandboxExists ? '' : ' class="new"')
+ '>sandbox</a>');
var testcasesText = ''
+ '<a rel="mw:WikiLink" href="/wiki/'
+ pagename.replace(/\/sandbox/g, '')
+ '/testcases"'
+ (testcasesExists ? '' : ' class="new"')
+ '>testcases</a>';
var newHTML = sandboxText + testcasesText;
links.insertAdjacentHTML("beforeend", newHTML);
})();
}
});