User:Koavf/simplife.js

From Wikipedia, the free encyclopedia
Note: After saving, you have to bypass your browser's cache to see the changes. Google Chrome, Firefox, Microsoft Edge and Safari: Hold down the ⇧ Shift key and click the Reload toolbar button. For details and instructions about other browsers, see Wikipedia:Bypass your cache.
var xwLang = 'simple';
$(function() {
    var wgPageName = mw.config.get('wgPageName');
    mw.util.addPortletLink('p-cactions', 'http://' + xwLang + '.wikipedia.org/wiki/' + mw.config.get('wgPageName'), 'simple', 'ca-x' + xwLang)
    wpPageExists(wgPageName);
});

function wpPageExists(title) {
    var url = 'http://' + xwLang + '.wikipedia.org/w/api.php?action=query&format=json&callback=wpPageExistsCb&titles=' + title;
    var scriptElem = document.createElement('script');
    scriptElem.setAttribute('src', url);
    scriptElem.setAttribute('type','text/javascript');
    document.getElementsByTagName('head')[0].appendChild(scriptElem);
}
function wpPageExistsCb(obj) {
    var wgPageName = mw.config.get('wgPageName');
    var exists = obj['query']['pages']['-1'];
    var elm = document.getElementById('ca-x' + xwLang);
    elm.className = exists ? 'new' : '';
    elm.firstChild.href = 'http://' + xwLang + '.wikipedia.org/wiki/' + wgPageName + (exists ? '?action=edit' : '');
}