Jump to content

User:Heptite/monobook.js

From Wikipedia, the free encyclopedia
This is an old revision of this page, as edited by Heptite (talk | contribs) at 15:40, 24 May 2006 (using an external editor). The present address (URL) is a permanent link to this revision, which may differ significantly from the current revision.
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.
popupFixRedirs = true;
popupRedlinkRemoval = true;
popupFixDabs = true;
popupMaxWidth = 600;

// [[User:Lupin/popups.js]]

document.write('<script type="text/javascript" src="' 
             + 'http://en.wikipedia.org/w/index.php?title=User:Lupin/popups.js' 
             + '&action=raw&ctype=text/javascript&dontcountme=s"></script>');

// [[User:Quarl/watchlist.js]] - adds buttons to watchlist: "unwatch", "diff
// since"

//  UNWATCH button asynchronously unwatches and crosses the entry off the
//  watchlist.
//
//  DIFF SINCE button shows differences since last edited.

// quarl 2006-01-09 added asynchronous feature.
// quarl 2006-02-03 factored; added diff since.

// requires: wikipage.js, util.js, wikiwatch.js, diffsince.js

// originally based on http://en.wikipedia.org/wiki/User:Omegatron/monobook.js
// see also http://en.wikipedia.org/wiki/User:Matthewmayer/monobook.js

// see also Bug 424 http://bugzilla.wikipedia.org/show_bug.cgi?id=424

var watchlist = new Object();
watchlist.wp = {};

watchlist.unwatchAsync = function(pagename) {
    var wp = watchlist.wp[pagename];
    if (!wp) { alert("## internal error 72192d74-ab57-4a98-917f-8c6ca03b0559"); return; }
    wikiwatch.unwatchAsync(wp, watchlist._unwatchSuccess, wp.unwatchSpan);
    return false;
}

watchlist.watchAsync = function(pagename) {
    var wp = watchlist.wp[pagename];
    if (!wp) { alert("## internal error 72192d74-ab57-4a98-917f-8c6ca03b0559"); return; }
    wikiwatch.watchAsync(wp, watchlist._watchSuccess, wp.unwatchSpan);
    return false;
}

watchlist.diffSince = function(pagename) {
    var wp = watchlist.wp[pagename];
    if (!wp) { alert("## internal error 72192d74-ab57-4a98-917f-8c6ca03b0559"); return; }
    return diffsince.diffPageAsync(wp, wp.diffsinceSpan);
}

watchlist._addStrikeThrough = function(node) {
    // return node && insertNode(node, document.createElement('s'));
    if (!node) return 0;

    addClass(ensureSpan(node), 'history-deleted');
    return 1;
}

watchlist._removeStrikeThrough = function(node) {
    if (!node) return 0;

    removeClass(ensureSpan(node), 'history-deleted');
    return 1;
}

watchlist._unwatchSuccess = function(wp) {
    var wpNT = wp.notalkPage();
    var wpT = wp.talkPage();

    if (0 == (watchlist._addStrikeThrough(findHref(wpNT.url)) +
              watchlist._addStrikeThrough(findHref(wpT.url))))
    {
        alert("Unwatched article '"+wp.page+"', but couldn't annotate current page.");
        return;
    }

    watchlist._updateWuwLink(wpT, 'watch');
    watchlist._updateWuwLink(wpNT, 'watch');
}

watchlist._watchSuccess = function(wp) {
    var wpNT = wp.notalkPage();
    var wpT = wp.talkPage();

    if (0 == (watchlist._removeStrikeThrough(findHref(wpNT.url)) +
              watchlist._removeStrikeThrough(findHref(wpT.url))))
    {
        alert("Watched article '"+wp.page+"', but couldn't annotate current page.");
        return;
    }

    watchlist._updateWuwLink(wpT, 'unwatch');
    watchlist._updateWuwLink(wpNT, 'unwatch');
}

// Update a watch/unwatch link.  Returns 1 on success, 0 if not found.
//
// wuw must be 'watch' or 'unwatch'
watchlist._updateWuwLink = function(wpX, wuw) {
    wp = watchlist.wp[wpX.page];
    if (!wp) return 0;
    var link = "javascript:return watchlist."+wuw+"Async("+string_quote_escape(wp.page)+")";
    var url = wp.qurl+'&action='+wuw;
    wp.unwatchSpan.innerHTML = '<a onclick="'+link+'" href="' + url + '">'+wuw+'</a>';
    return 1;
}


watchlist._load = function()
{
    if (wikiPage.page == "Special:Watchlist") watchlist._annotatePage();
}

watchlist._annotatePage = function() {
    var links = copyArray(document.getElementById('bodyContent').getElementsByTagName('a'));
    for (i in links) {
        var link = links[i];
        if (link.href && link.href.match(/action=history$/)) {
            var wp = new WikiPage(link.href);
            watchlist.wp[wp.page] = wp;

            wp.unwatchSpan = document.createElement('span');
            var unwatchLink = "javascript:return watchlist.unwatchAsync("+string_quote_escape(wp.page)+")";
            var unwatchUrl = wp.qurl+'&action=unwatch';
            wp.unwatchSpan.innerHTML = '<a onclick="'+unwatchLink+'" href="' + unwatchUrl + '">unwatch</a>';
            add_after(link, wp.unwatchSpan);
            add_after(link, document.createTextNode('; '));

            wp.diffsinceSpan = document.createElement('span');
            diffsinceLink = "javascript:return watchlist.diffSince("+string_quote_escape(wp.page)+")";
            var diffsinceUrl = diffsince.makeUrl(wp);
            wp.diffsinceSpan.innerHTML = '<a onclick="'+diffsinceLink+'" href="' + diffsinceUrl + '">since</a>';
            add_before(link, wp.diffsinceSpan);
            add_before(link, document.createTextNode('; '));
        }
    }
}

addOnloadHook(watchlist._load);

function addlilink(tabs, url, name, id, title, key){
    var na = document.createElement('a');
    na.href = url;
    na.appendChild(document.createTextNode(name));
    var li = document.createElement('li');
    if(id) li.id = id;
    li.appendChild(na);
    tabs.appendChild(li);
    if(id)
    {
        if(key && title)
        {
            ta[id] = [key, title];
        }
        else if(key)
        {
            ta[id] = [key, ''];
        }
        else if(title)
        {
            ta[id] = ['', title];
        }
    }
    // re-render the title and accesskeys from existing code in wikibits.js
    akeytt();
    return li;
}

function addTab(url, name, id, title, key){
    var tabs = document.getElementById('p-cactions').getElementsByTagName('ul')[0];
    return addlilink(tabs, url, name, id, title, key);
}

function addLink(where, url, name, id, title, key, after){
    //* where is the id of the toolbar where the button should be added;
    //   i.e. one of "p-cactions", "p-personal", "p-navigation", or "p-tb".
    //
    //* url is the URL which will be called when the button is clicked.
    //   javascript: urls can be used to do more complex things.
    //
    //* name is what will appear as the name of the button.
    //
    //* id is the id of the button; it's best to define one.  
    //   Use a prefix to make sure its unique. Optional.
    //
    //* title is the tooltip title that gives a longer description 
    //   of the button; if you define a accesskey, mention it here. Optional.
    //
    //* key is the char you want for the accesskey. Optional.
    //
    //* after is the id of the button you want to follow this one. Optional.
    //
    var na = document.createElement('a');
    na.href = url;
    na.appendChild(document.createTextNode(name));
    var li = document.createElement('li');
    if(id) li.id = id;
    li.appendChild(na);
    var tabs = document.getElementById(where).getElementsByTagName('ul')[0];
    if(after) {
        tabs.insertBefore(li,document.getElementById(after));
    } else {
        tabs.appendChild(li);
    }
    if(id) {
        if(key && title) { ta[id] = [key, title]; }
        else if(key) { ta[id] = [key, '']; }
        else if(title) { ta[id] = ['', title];} 
    }
    // re-render the title and accesskeys from existing code in wikibits.js
    akeytt();
    return li;
}

// external editor
addOnloadHook(function(){
 if (document.URL.search("=") == -1 && !document.getElementById("ca-viewsource") && document.URL.search("/Special:") == -1) { //viewing a page that isn't protected or special
   addTab("javascript:location = document.URL + '?action=edit&externaledit=true';", "ee", "ca-exted", "external editor", "");
 }
 else if (document.title.indexOf("Editing ") == 0) { //this line from techniques subpage of WP:US
   addTab("javascript:location = document.URL + '&externaledit=true';", "ee", "ca-exted", "external editor", "");
 }
});

// addPurge
addOnloadHook(function (){
    var x = document.getElementById('ca-history');
    if(!x) return;
    if(x.children) x = x.children[0].href;
    else x = x.childNodes[0].href;
    addLink("p-cactions", x.replace(/=history/, "=purge"), 'purge', 'ca-purge', 'Purge the internal cache for this page', 0);
});