User:Conrad.Irwin/subsectionpreload.js

From Wiktionary, the free dictionary
Jump to navigation Jump to search

Note – after saving, you may have to bypass your browser’s cache to see the changes.

  • Mozilla / Firefox / Safari: hold Shift while clicking Reload, or press either Ctrl-F5 or Ctrl-R (Command-R on a Macintosh);
  • Konqueror and Chrome: click Reload or press F5;
  • Opera: clear the cache in Tools → Preferences;
  • Internet Explorer: hold Ctrl while clicking Refresh, or press Ctrl-F5.

if (wgPageName == 'Wiktionary:Beer_parlour') addOnloadHook(function ()
{
    // Loads a section from the archive and puts it at the top of the page
    function load_section_from_archive(section_title)
    {
        var page_title = 'Wiktionary:Beer_parlour/' + section_title;
        if (!arguments.callee.div)
        {
            var insert_after = document.getElementById('jump-to-nav');
            arguments.callee.div = newNode('div',{class:'toc'});
            insert_after.parentNode.insertBefore(arguments.callee.div, insert_after.nextSibling);
        }
        var div = arguments.callee.div;

        div.innerHTML = "";
        div.appendChild(newNode('b', "Loading, '" + section_title + "' from Archive. Please wait."))

        JsMwApi().page(page_title).parse(function (section, result)
        {
            if (!result || !result.parse || !result.parse.revid)
            {
                div.innerHTML = "";
                div.appendChild(newNode('b',"Could not find '" + section_title + "' in Archives. Sorry."));
            }
            else
            {
                div.innerHTML = section;
                div.insertBefore(newNode('b', "This is an archived discussion from ", 
                    newNode('a', {href:wgArticlePath.replace("%s", encodeURIComponent(page_title))}, page_title),
                    newNode('br')
                ), div.firstChild);
            }
        });
    }

// __init__

    var location = window.location.toString();

    // If we are at a section title that isn't in the document, try loading it from the archives
    location.replace(/^.*#(.*)$/, function (match, section_title)
    {
        if (! document.getElementById(section_title) )
            load_section_from_archive(decodeURIComponent(section_title.replace('.', '%')));
    });
});