Jump to content

User:Timeshifter/common.js

From Wikipedia, the free encyclopedia
This is an old revision of this page, as edited by Timeshifter (talk | contribs) at 19:08, 26 September 2012 (auto-numbering of table rows). 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.
// Ctrl-click (PC) and Command-click (Mac) to open search in new tab.
// The Command key is the Apple key on Apple keyboards.
// commons.wikimedia.org/wiki/MediaWiki_talk:Gadget-search-new-tab.js
// On the Commons enable the gadget in your preferences. On other wikis...
// Add line below to Special:MyPage/common.js (User:YOUR-USER-NAME/common.js)

mw.loader.load('//commons.wikimedia.org/w/index.php?title=MediaWiki:Gadget-search-new-tab.js&action=raw&ctype=text/javascript');

/* Reference Tooltips. Version for testing before adding to gadget */
/* See: en.wikipedia.org/wiki/User:Yair_rand/ReferenceTooltips */
/* Discussion: en.wikipedia.org/wiki/User_talk:Yair_rand/ReferenceTooltips */

importScript('User:Yair rand/ReferenceTooltips.js'); importStylesheet('User:Yair rand/ReferenceTooltips.css');

// en.wikipedia.org/wiki/User:Mike_Dillon/Scripts/autonumber.js
// bugzilla.wikimedia.org/show_bug.cgi?id=10433
// Bug 10433 - Client-side auto-numbering of table rows

addOnloadHook(function () {
    function firstCell(row) {
        for (var n in row.childNodes) {
            var c = row.childNodes[n];
            if (/^t[dh]$/i.test(c.tagName)) return c;
        }
    }
 
    var tables = getElementsByClassName(document.getElementById("bodyContent"), "table", "autonumber");
    for (var n in tables) {
        var t = tables[n];
 
        var rownum = 1;
        for (var m = 0; m < t.rows.length; m++) {
            var r = t.rows[m];
 
            var c = firstCell(r);
            if (!c) continue;
 
            var newCell;
            if (c.tagName.toLowerCase() == "th") {
                newCell = document.createElement("th");
                newCell.appendChild(document.createTextNode("#"));
                rownum = 1; // Renumber if a header row is seen
            } else {
                newCell = document.createElement("td");
                newCell.appendChild(document.createTextNode(rownum));
                rownum += 1;
            }
            newCell.setAttribute("class", "autonumber-cell");
            r.insertBefore(newCell, r.firstChild);
        }
    }
});