User:Odie5533/WAjaxInterface.user.js

// ==UserScript== // @name          Wikipedia AJAX Interface // @namespace     http://en.wikipedia.org/wiki/ // @include       http://en.wikipedia.org/wiki/ // ==/UserScript==

window.WAjaxInterface = function {

window.showTalkPage = function { document.getElementById("articleContent").style.display = "none"; document.getElementById("talkContent").style.display = ""; var editTab = document.getElementById('ca-edit').childNodes[0]; editTab.href = mw.config.get('wgServer') + mw.config.get('wgScript') + '?title=' + talkPageLink + '&action=edit'; var histTab = document.getElementById('ca-history').childNodes[0]; histTab.href = mw.config.get('wgServer') + mw.config.get('wgScript') + '?title=' + talkPageLink + '&action=history'; } window.showArticlePage = function { document.getElementById("articleContent").style.display = ""; document.getElementById("talkContent").style.display = "none"; var editTab = document.getElementById('ca-edit').childNodes[0]; editTab.href = mw.config.get('wgServer') + mw.config.get('wgScript') + '?title=' + encodeURIComponent(mw.config.get('wgPageName')) + '&action=edit'; var histTab = document.getElementById('ca-history').childNodes[0]; histTab.href = mw.config.get('wgServer') + mw.config.get('wgScript') + '?title=' + encodeURIComponent(mw.config.get('wgPageName')) + '&action=history'; }

var wajaxint = { /**    * Starts the script object running. The main function of the script. If the * getMainType function can find the assessment, it uses that assessment * for the page, parses it, and displays it in the header. Otherwise, it runs * ajaxMain. */   init: function init  { var talkTab = document.getElementById('ca-talk').childNodes[0]; var cutLen = (mw.config.get('wgArticlePath') + mw.config.get('wgServer')).length - 2; window.talkPageLink = talkTab.href.substring(cutLen); var editTab = document.getElementById('ca-edit'); editTab.childNodes[0].id = 'editArticlePage'; var talkEditTab = document.createElement('a'); talkEditTab.href = mw.config.get('wgServer') + mw.config.get('wgScript') + '?title=' + talkPageLink + '&action=edit'; talkEditTab.innerHTML = 'edit'; talkEditTab.style.display = 'none'; talkEditTab.id = 'editTalkPage'; editTab.appendChild(talkEditTab); talkTab.href = 'javascript:ajaxMain("'+ talkTab.href + '");'; window.ajaxMain = this.ajaxMain; window.ajStateChange = this.ajStateChange; },   /**     * The main function when an AJAX request is needed to find the assessment. * Creates an AJAX request for the contents of a URL (defaults to the    * first section of the article's talk page), then sends the request. After * getting the requested data back, it finds the assessment information in    * the data, then uses and displays that assessment in the header. * @param {String} arguments[0] - Optional: override the default URL for the *       request. */   ajaxMain: function ajaxMain  { if (arguments[0] && arguments[0].match(/^https?:\/\//i)) // optional url override this.url = arguments[0]; else this.url = mw.config.get('wgServer') + mw.config.get('wgScript') + '?title=Talk:' + encodeURIComponent(mw.config.get('wgPageName')); this.request = sajax_init_object; if (this.request) { var that = this; // store value of 'this' this.request.onreadystatechange = function { that.ajStateChange.call(that); }           this.request.open('GET', this.url, true); this.request.send(null); }   },    /**     * This function is passed as a parameter to ajaxMain. It is called each time * this.request updates, and the code inside the conditional runs when the * data is available. */   ajStateChange: function ajStateChange  { if (this.request.readyState == 4 && this.request.status == 200) { var text = this.request.responseText; var bodyContent = document.getElementById('bodyContent'); var newCon = text.match(/ ([\n\w\W]*) /g)[0]; newCon = newCon.substring(22, newCon.length - 40); bodyContent.innerHTML = ' ' + bodyContent.innerHTML + ' ' + newCon + ' '; showTalkPage; var talkTab = document.getElementById('ca-talk').childNodes[0]; talkTab.href = "javascript:showTalkPage;"; var mainTab = document.getElementById('ca-nstab-main'); if (mainTab) mainTab.childNodes[0].href = "javascript:showArticlePage;"; var projTab = document.getElementById('ca-nstab-project'); if (projTab) projTab.childNodes[0].href = "javascript:showArticlePage;";

var userTab = document.getElementById('ca-nstab-user'); if (userTab) userTab.childNodes[0].href = "javascript:showArticlePage;"; }   } }

wajaxint.init;

}

// append wrapper to head if run under Greasemonkey if (window.parent == window) { if (typeof(GM_getValue) == 'function') { var script = document.createElement('script'); script.id = 'AjaxInterface'; script.type = 'text/javascript'; script.text = 'WAjaxInterface = ' + WAjaxInterface.toString + '; WAjaxInterface;'; document.getElementsByTagName('head')[0].appendChild(script); }

// otherwise run installation directly else { WAjaxInterface; } }