Jump to content

User:Omegatron/monobook.js/edittop.js

From Wikipedia, the free encyclopedia
This is an old revision of this page, as edited by Omegatron (talk | contribs) at 02:24, 12 January 2006. 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.
//<pre><nowiki>
// This will add an [edit top] link at the top of all pages except preview pages
// by User:Pile0nades


// Add an [edit top] link to pages
// http://en.wikipedia.org/wiki/Wikipedia:WikiProject_User_scripts/Scripts/Edit_Top

function editTopLink() {
  // if this is preview page or generated page, stop
  if(document.getElementById("wikiPreview") || window.location.href.indexOf("/wiki/Special:") != -1) return;

  // get the page title
  var pageTitle = document.title.split(" - ")[0].replace(" ", "_"); 

  // create div and set innerHTML to link
  var divContainer = document.createElement("div");
  divContainer.innerHTML = '<div class="editsection" style="float:right;margin-left:5px;margin-top:3px;">[<a href="/w/index.php?title='+pageTitle+'&action=edit&section=0" title="'+document.title.split(" - ")[0]+'">edit top</a>]</div>';

  // insert divContainer into the DOM before the h1
  if(window.location.href.indexOf("&action=edit") == -1)
    document.getElementById("content").insertBefore(divContainer, document.getElementsByTagName("h1")[0]);

  if(window.location.href.indexOf("&action=edit&section=0") != -1)
    document.getElementById("wpSummary").value = "/* Intro */ ";
}

addOnloadHook(editTopLink);

addOnloadHook(function () {
    var diffSigns = new Array();
    var fixDiffWidth = function () {
        var tables = document.getElementsByTagName('table');

        for (var i = 0; i < tables.length; i++) {
            if (tables[i].className != 'diff') continue;
            var rows = tables[i].getElementsByTagName('tr');

            var diffDiv = document.createElement('div');
            diffDiv.style.backgroundColor = 'white';
            diffDiv.className = 'xdiff';

            for (var j = 0; j < rows.length; j++) {
                var rowDiv = document.createElement('div');
                rowDiv.style.width = '100%';
                rowDiv.style.margin = '0 0 3px 0';
                rowDiv.style.overflow = 'hidden';  // trick, makes it expand vertically to contain floats
                rowDiv.className = 'xdiff-row';

                var colDiv = null;
                var cols = rows[j].getElementsByTagName('td');
                for (var k = 0; k < cols.length; k++) { 
                    if (!colDiv) {
                        colDiv = document.createElement('div');
                        colDiv.style.cssFloat = 'left';
                        colDiv.style.clear = 'none';
                        colDiv.style.position = 'relative';
                        colDiv.style.width = '49%';
                        colDiv.className = 'xdiff-col';
                        rowDiv.appendChild(colDiv);
                    }
                    var innerDiv = document.createElement('div');

                    if (cols[k].getAttribute('colspan') == 2 || cols[k].className.substring(0,5) == 'diff-') {                
                        innerDiv.style.overflow = 'auto';  // scroll if necessary!
                        innerDiv.style.padding = '1px';
                        if (cols[k].getAttribute('colspan') != 2) {
                            innerDiv.style.margin = '0 0 0 2em';
                            innerDiv.style.fontSize = '85%';  // from MediaWiki:Monobook.css
                            if (cols[k].className == 'diff-addedline')   innerDiv.style.backgroundColor = '#cfc';
                            if (cols[k].className == 'diff-deletedline') innerDiv.style.backgroundColor = '#ffa';
                            if (cols[k].className == 'diff-context')     innerDiv.style.backgroundColor = '#eee';
                        }
                        innerDiv.style.textAlign = cols[k].getAttribute('align');
                        innerDiv.className = (cols[k].className ? "x"+cols[k].className : "");
                    }
                    else if (cols[k].firstChild && (cols[k].firstChild.nextSibling ||
                             cols[k].firstChild.nodeType != 3 || cols[k].firstChild.nodeValue.match(/\S/))) {
                        innerDiv.style.width = '2em';
                        innerDiv.style.textAlign = 'center';
                        innerDiv.style.position = 'absolute';
                        innerDiv.className = 'xdiff-sign';
                        diffSigns.push(innerDiv);
                    }
                    else continue; 

                    for (var node = cols[k].firstChild; node; node = node.nextSibling)
                        innerDiv.appendChild(node.cloneNode(true));
                    innerDiv.appendChild(document.createTextNode(String.fromCharCode(0xa0))); // add nbsp
                    colDiv.appendChild(innerDiv);

                    if (innerDiv.className != 'xdiff-sign')
                        colDiv = null;  // start new columns
                }
                // force rowDiv to expand, just in case position trick fails
                var clearer = document.createElement('span');
                clearer.clear = 'left';
                rowDiv.appendChild(clearer);
                diffDiv.appendChild(rowDiv);
            }
            tables[i].parentNode.replaceChild(diffDiv, tables[i]);
        }
    };
    // finally, a kluge to vertically center the +/- signs
    var centerDiffSigns = function () {
        for (var i = 0; i < diffSigns.length; i++) {
            var parentHeight;
            if (!( parentHeight = diffSigns[i].parentNode )) continue; 
            if (!( parentHeight = parentHeight.clientHeight )) continue; 
            diffSigns[i].style.top = Math.round((parentHeight - diffSigns[i].offsetHeight)/2) + "px";
        }
    };
    fixDiffWidth();
    hookEvent('resize', centerDiffSigns);
    setTimeout(centerDiffSigns, 250); 
});
//</pre></nowiki></pre>