User:Phe/Interwiki.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.

// add_interwiki() perform one request per entry in this array (-1 if the native
// wiki has its own entry here). So don't add too much entry, pywikipedia interwiki
// bots can handle the missing interwikis. No entry has been added for any wiki
// w/o an Author: dedicated namespace, biggest missing are de: and es:.
var author_ns_prefix = {
   'en' : 'Author',
   'fr' : 'Auteur',
   'it' : 'Autore',
   'pt' : 'Autor',
}

function get_interwiki(data)
{
    var wpTextbox1 = document.getElementById("wpTextbox1");
    if (wpTextbox1 && !data.query.pages["-1"]) {
        for (var ids in data.query.pages) {
            wpTextbox1.value += '[[' + data.query.general.lang + ':' + data.query.pages[ids].title + ']]\n'
            break;
        }
    }
}

function create_script_obj(url)
{
    var scriptObj = document.createElement("script");
    scriptObj.setAttribute("type", "text/javascript");
    scriptObj.setAttribute("src", url);
    document.body.appendChild(scriptObj);
}

function add_interwiki()
{
    for (var lang in author_ns_prefix) {
        if (lang == mw.config.get('wgContentLanguage'))
            continue;

        // well, hard coded... domain name would be extracted from mw.config.get('wgServer')...
        var base_url = "http://" + lang + ".wikisource.org"
            + mw.config.get('wgScriptPath')
            + "/api.php?format=json&redirects";

        var url = base_url
            //+ '&callback=get_interwiki&action=query&prop=langlinks&lllimit=32&titles='
            + '&callback=get_interwiki&action=query&meta=siteinfo&titles='
            + encodeURIComponent(author_ns_prefix[lang] + ':' + mw.config.get('wgTitle'));

        create_script_obj(url);
    }
}