User:Sherurcij/monobook.js

From Wikisource
Jump to navigation Jump to search
Note: After saving, changes may not occur immediately. Click here to learn how to bypass your browser's cache.
  • Firefox / Safari: Hold Shift while clicking Reload, or press either Ctrl-F5 or Ctrl-R (Cmd-R on a Mac)
  • Google Chrome: Press Ctrl-Shift-R (Cmd-Shift-R on a Mac)
  • Internet Explorer: Hold Ctrl while clicking Refresh, or press Ctrl-F5
  • Opera: Clear the cache in Tools → Preferences

For details and instructions about other browsers, see Wikipedia:Bypass your cache.

//<pre>
/* Preload Template:Header2 when adding a new text */
function preloadHeaderTemplate()
{
    var wpTextbox1 = document.getElementById("wpTextbox1")
    if (wpTextbox1.value == "")
    {
        //fill in header template
        var isSubpage = wgTitle.indexOf("/") != -1
        var subpageDepth = 0
        var subpageTitle
        var subpageTitleRegExp
        var rawBasePageRequest
        
        //title
        var wpTextbox1value = "{{header2\n | title      = "
        if (isSubpage)
        {
            wpTextbox1value += "[["
            for (var i = wgTitle.indexOf("/"); i != -1; i = wgTitle.indexOf("/", i + 1))
            {
                wpTextbox1value += "../"
                subpageDepth += 1
            }
            wpTextbox1value += "]]"
        }
        else
        {
            wpTextbox1value += wgTitle
        }
        
        //author
        wpTextbox1value += "\n | author     = "
        if (isSubpage)
        {
            try
            {
                rawBasePageRequest = new XMLHttpRequest()
                rawBasePageRequest.open("GET", mw.config.get('wgServer') + mw.config.get('wgScript') + "?title=" + wgPageName.substr(0, wgPageName.indexOf("/")) + "&action=raw", false)
                rawBasePageRequest.send(null)
                
                wpTextbox1value += /\{\{[\s]*[Hh]eader[2]?[\s]*[\s\S]*\|[\s]*author[\s]*\=[\s]*(.*)[\s]*[\||\}\}]/.exec(rawBasePageRequest.responseText)[1].replace(/\s+$/,"")
            }
            catch (e)
            {
                //if there was an error, just leave the field blank
            }
        }
        
        //translator
        wpTextbox1value += "\n | translator = "
        if (isSubpage)
        {
            try
            {
                wpTextbox1value += /\{\{[\s]*[Hh]eader2[\s]*[\s\S]*\|[\s]*translator[\s]*\=[\s]*(.*)[\s]*[\||\}\}]/.exec(rawBasePageRequest.responseText)[1].replace(/\s+$/,"")
            }
            catch (e)
            {
                //if there was an error, just leave the field blank
            }
        }
        
        //section
        wpTextbox1value += "\n | section    = "
        if (subpageDepth == 1) //more than 1 level deep and it really depends on the text
        {
            try
            {
                subpageTitle = wgTitle.substr(wgTitle.lastIndexOf("/") + 1) //will be used for the "next" field
                subpageTitleRegExp = subpageTitle.replace(/[ _]/, "[ _]")  //subpageTitleRegExp will also be used later
                subpageTitleRegExp = "[" + subpageTitleRegExp[0].toLowerCase() + subpageTitleRegExp[0].toUpperCase() + "]" + subpageTitleRegExp.substr(1) //MediaWiki is not case-sensitive on the first letter
                
                wpTextbox1value += (new RegExp("\\[\\[/" + subpageTitleRegExp + "\\|(.*)\]\]")).exec(rawBasePageRequest.responseText)[1]
            }
            catch (e)
            {
                //if there was an error, fall back on just adding the section name
                wpTextbox1value += subpageTitle
            }
        }
        
        //used for both previous page and next page determination
        var indexOfCurrentPageInTableOfContents
        
        //previous
        wpTextbox1value += "\n | previous   = "
        if (subpageDepth == 1) //more than one subpage deep it gets inconsistent, so restrict to the simple cases
        {
            try
            {
                indexOfCurrentPageInTableOfContents = rawBasePageRequest.responseText.search(new RegExp("\\[\\[/" + subpageTitleRegExp + "[\\||\\]\\]]"))
                var indexOfTargetLink = rawBasePageRequest.responseText.lastIndexOf("[[/", indexOfCurrentPageInTableOfContents - 3)
                var previousPageTitle = /\[\[\/([\w\s]*)[\||\]\]]/.exec(rawBasePageRequest.responseText.substring(indexOfTargetLink, indexOfCurrentPageInTableOfContents))[1]
                wpTextbox1value += "[[../" + previousPageTitle + "|" + previousPageTitle + "]]"
            }
            catch (e)
            {
                //if there was an error, just leave the field blank
            }
        }
        
        //next
        wpTextbox1value += "\n | next       = "
        if (subpageDepth == 1)
        {
            try
            {
                var indexOfTargetLink = rawBasePageRequest.responseText.indexOf("[[/", indexOfCurrentPageInTableOfContents + subpageTitle.length + 1)
                if (indexOfTargetLink == -1) //if we're at the end of the list, do not wrap back to the beginning
                {
                    throw 0
                }
                var nextPageTitle = /\[\[\/([\w\s]*)[\||\]\]]/.exec(rawBasePageRequest.responseText.substring(indexOfTargetLink))[1]
                wpTextbox1value += "[[../" + nextPageTitle + "|" + nextPageTitle + "]]"
            }
            catch (e)
            {
                //if there was an error, just leave the field blank
            }
        }
        
        wpTextbox1.value = wpTextbox1value + "\n | notes      = \n}}\n\n"
    }
}

/* Preload Template:Textinfo when starting a discussion page */
function preloadTextinfoTemplate()
{
    var wpTextbox1 = document.getElementById("wpTextbox1")
    if (wpTextbox1.value == "")
    {
        wpTextbox1.value = "{{textinfo\n | edition      = \n | source       = \n | contributors = \n | progress     = \n | notes        = \n | proofreaders = \n}}\n"
    }
}

/* Preload Template:Author when starting an author page */
function preloadAuthorTemplate()
{
    var wpTextbox1 = document.getElementById("wpTextbox1")
    if (wpTextbox1.value == "")
    {
        wpTextbox1.value = "{{author\n | firstname      = \n | lastname       = \n | last_initial   = \n | birthyear      = \n | deathyear      = \n | description    = \n | image          = \n | wikipedia_link = \n | wikiquote_link = \n | commons_link   = \n}}\n\n==Works==\n\n"
    }
}

if (wgAction == "edit")
{
    switch (wgNamespaceNumber)
    {
        case 0: //main
            addOnloadHook(preloadHeaderTemplate)
            break
        case 1: //Talk
            addOnloadHook(preloadTextinfoTemplate)
            break
        case 102: //Author
            addOnloadHook(preloadAuthorTemplate)
    }
}
//</pre>