Greasy Fork is available in English.

NGA Excel UI optimization

原PiGGy修改风昔代码的基础上,又修改了下表格的宽度适配。切换快捷键为E。

// ==UserScript==
// @name			NGA Excel UI optimization
// @author			风昔,CinGing,PiGGy, Conrad
// @description		原PiGGy修改风昔代码的基础上,又修改了下表格的宽度适配。切换快捷键为E。
// @version			1.0.0.0
// @match			https ://bbs.ngacn.cc/*
// @include			https://bbs.ngacn.cc/*
// @match			https://nga.178.com/*
// @include			https://nga.178.com/*
// @match			https://bbs.nga.cn/*
// @include			https://bbs.nga.cn/*
// @match			bbs.ngacn.cc/*
// @include			bbs.ngacn.cc/*
// @match			nga.178.com/*
// @include			nga.178.com/*
// @match			bbs.nga.cn/*
// @include			bbs.nga.cn/*
// @run-at			document-start
// @namespace https://greasyfork.org/zh-CN/users/113353
// ==/UserScript==


(function() {
    'use strict';
    var body0, body1, parent0, title0, status0, style,

        statusMap = {
            none: 'normal',
            normal: 'none',
            clean: 'none'
        },
        createDom = function(html) {
            var tmp = document.createElement("div");
            tmp.innerHTML = html;
            return tmp.childNodes[0];
        },
        setCookie = function(c_name, value, expire) {
            var expiredays = expire || 1;
            var exdate = new Date();
            exdate.setDate(exdate.getDate() + expiredays);
            document.cookie = c_name + "=" + escape(value) + ((expiredays == null) ? "" : ";expires=" + exdate.toGMTString());
        },
        getCookie = function(c_name) {
            var cookies = document.cookie.split(';');
            var cookie = '';
            for (var i = 0; i < cookies.length; i++) {
                cookie = cookies[i].split('=');
                if (cookie[0].replace(/^\s+|\s+$/g, '') == c_name) {
                    return (cookie.length <= 1) ? "" : unescape(cookie[1].replace(
                        /^\s+|\s+$/g, ''));
                }
            }
            return "";
        },
        generate = function(to) {
            var rowCount = -1,
                frame = createDom('<div><div style="position: fixed; top: 0; width:100%; background: url(http://pic3.178.com/2126/21268720/month_1310/77cf4384624dcdc285a52ced7f9d6a2e.PNG) left no-repeat; height: 142px;"></div></div>'),
                threadsTable = createDom('<table style="margin-top: 142px; margin-bottom: 42px; background-color : #fff; font-size: 13px;table-layout: fixed;border-collapse: separate;border-style: none;border-spacing: 0;border-color: gray;width: 100%;"></table>'),
                rootNav = createDom('<a class="uitxt1" href="/">NGA</a>'),
                newRow = function(ctx) {
                    var rowDom = document.createElement("tr"),
                        i = 0,
                        rowHTML = '<td width="25px;" style="vertical-align:top; padding: 5px; border: 1px #ccc solid; border-width: 0 1px 1px 0; text-align: center;">' + (rowCount < 1 ? '' : rowCount) + '</td>';

                    rowCount++;
                    if (window.location.pathname == "/thread.php"){
                        rowHTML += '<td width="650px;" style="vertical-align:top; padding: 5px; overflow: hidden; border: 1px #ccc solid; border-width: 0 1px 1px 0;"></td>';
                        for (i = 1; i < ctx.length; i++) {
                            rowHTML += '<td width="100px;" style="vertical-align:top; padding: 5px; overflow: hidden; border: 1px #ccc solid; border-width: 0 1px 1px 0;"></td>';
                        }
                    }else if (window.location.pathname == "/read.php"){
                        for (i = 0; i < ctx.length; i++) {
                            rowHTML += '<td width="100px;" style="vertical-align:top; padding: 5px; overflow: hidden; border: 1px #ccc solid; border-width: 0 1px 1px 0;"></td>';
                        }
                    }
                    rowDom.innerHTML = rowHTML;
                    rowDom.children[rowDom.children.length - 1].removeAttribute("width");
                    for (var i1 = 0; i1 < ctx.length; i1++) {
                        if (typeof ctx[i1] == "string") {
                            rowDom.children[i1 + 1].textContent = rowDom.children[i1 + 1].innerText = ctx[i1];
                        } else {
                            rowDom.children[i1 + 1].appendChild(ctx[i1].cloneNode(true));
                        }
                    }
                    threadsTable.appendChild(rowDom);
                },
                getAnchor = function(tb, type) {
                    if (tb instanceof Array) {
                        tb = tb[0];
                    }
                    var args, scripts = tb.getElementsByTagName("script"),
                        anchor = createDom("<a style='float:right;' href=''>REPLY</a>");
                    try {
                        for (var i2 = 0; i2 < scripts.length; i2++) {
                            if (/commonui\.postArg\.proc/.test(scripts[i2].innerHTML)) {
                                args = scripts[i2].innerHTML.replace(/^[^(]*\(|\s*/g, "").split(",");
                                break;
                            }
                        }
                        if (type == 1) {
                            anchor.setAttribute("href", "/post.php?action=reply&_newui&fid=" + args[8] + "&tid=" + args[9]);
                        } else {
                            anchor.setAttribute("href", "/post.php?action=quote&_newui&fid=" + args[8] + "&tid=" + args[9] + "&pid=" + args[10] + "&article=" + args[0]);
                        }
                    } catch (e) {
                        return tb.nextElementSibling ? getAnchor(tb.nextElementSibling, type) : anchor;
                    }
                    return anchor;
                };
            frame.appendChild(threadsTable);
            if (window.location.pathname == "/thread.php") {
                var dom = document.getElementById("topicrows"),
                    nav0 = document.getElementById("m_pbtntop").getElementsByTagName("a"),
                    nav1 = document.createElement("div"),
                    topics = dom.children[1].children,
                    ctx;
                for (var i3 = 0; i3 < nav0.length; i3++) {
                    if (!isNaN(Number(nav0[i3].textContent))) {
                        nav1.appendChild(nav0[i3].cloneNode(true));
                    }
                }
                newRow(["", "", rootNav, nav1]);
                newRow(["REPLIES", "STARTER", "LAST POST", "POST NUM", ""]);
                for (var i4 = 0; i4 < dom.children.length; i4 += 2) {
                    ctx = [];
                    var str = dom.children[i4].children[0].children[1].children[1].textContent;
                    var item = dom.children[i4].children[0].children[1].children[0];
                    if (str.charAt(str.length - 1) != "•") {
                        item = dom.children[i4].children[0].children[1].children[1];
                    }
                    ctx.push(item);
                    ctx.push(dom.children[i4].children[0].children[2].children[0].textContent);
                    /*ctx.push(topics[i].children[0].textContent);
				ctx.push(topics[i].children[2].children[0].textContent);
				ctx.push(topics[i].children[3].children[1].textContent);
				var threadCtx = topics[i].children[1].children,
				threadNew = createDom('<div></div>');
				for(var j= 0; j < threadCtx.length; j++){
					threadNew.appendChild(removeClass(threadCtx[j].cloneNode(true)));
				}
				ctx.push(threadNew);*/
                    ctx.push(dom.children[i4].children[0].children[3].children[0].textContent);
                    ctx.push(dom.children[i4].children[0].children[0].children[0].textContent)
                    ctx.push("")
                    newRow(ctx);
                }

                newRow(["", "", rootNav, nav1]);
            } else if (window.location.pathname == "/read.php") {
                var nav00 = [],
                    nav11 = document.createElement("div"),
                    navAnchors0 = document.getElementById("m_pbtntop").getElementsByTagName("a"),
                    navAnchors1 = document.getElementById("m_nav").getElementsByTagName("a"),
                    postTables = (function() {
                        var tables = document.getElementsByTagName("table"),
                            res = [],
                            i5 = 0;
                        for (; i5 < tables.length; i5++) {
                            if (tables[i5].className == "forumbox postbox") {
                                res.push(tables[i5]);
                            }
                        }
                        return res;
                    })(),
                    startIndex = Number(postTables[0].getElementsByTagName("tr")[0].getAttribute("id").substr(10));
                for (var i6 = 1; i6 < navAnchors1.length; i6++) {
                    if (navAnchors1[i6].className == "nav_link") {
                        navAnchors1[i6].className = "uitxt1";
                        nav00.push(navAnchors1[i6].cloneNode(true));
                    }
                }
                nav11.appendChild(nav00[nav00.length - 1]);
                for (var i7 = 0; i7 < navAnchors0.length; i7++) {
                    if (!isNaN(Number(navAnchors0[i7].textContent))) {
                        nav11.appendChild(navAnchors0[i7].cloneNode(true));
                    }
                }
                nav11.appendChild(getAnchor(postTables, 1));
                newRow([rootNav, nav00[0], nav11]);
                newRow(["AUTHOR", "TIME", "CONTENT"]);
                for (var i8 = startIndex; i8 < postTables.length + startIndex; i8++) {
                    try {
                        var ctx1 = [];
                        ctx1.push(document.getElementById("postauthor" + i8).textContent);
                        ctx1.push(document.getElementById("postdate" + i8).textContent);
                        ctx1.push(document.getElementById("postcontent" + i8).cloneNode(true));
                        ctx1[2].appendChild(getAnchor(postTables[i8 - startIndex]));
                        newRow(ctx1);
                    } catch (e) {}
                }
                newRow([rootNav, nav00[0], nav11]);
            } else {
                rowCount = 1;
                var rowCtx = [],
                    hrefs = document.getElementsByTagName("a");
                for (var i9 = 0; i9 < hrefs.length; i9++) {
                    if (hrefs[i9].getAttribute("href").substr(0, 7) == "/thread") {
                        rowCtx.push(hrefs[i9].cloneNode(true));
                    }
                    if (rowCtx.length == 5) {
                        newRow(rowCtx);
                        rowCtx = [];
                    }
                }
                if (rowCtx.length > 0) {
                    newRow(rowCtx);
                }
            }
            frame.appendChild(createDom('<div style="position: fixed; bottom: 0; width:100%; background: url(http://pic3.178.com/2126/21268720/month_1310/8f0711665fac9e200f742934cf92069b.PNG) left no-repeat; height: 42px;"></div>'));
            to.appendChild(frame);
        },
        onStatus = function(status) {

            try {
                if (!parent0) {
                    body0 = document.getElementById("mmc");
                    parent0 = body0.parentNode;
                    body1 = document.createElement("div");
                    title0 = document.title;
                    generate(body1);

                    parent0.insertBefore(body1, body0);

                }
            } finally {
                if (style) {
                    style.parentNode.removeChild(style);
                    style = null;
                }
            }
            status = status || "none";
            if (status != "none") {
                body0.style.display = "none";
                body1.style.display = "inherit";
                parent0.style.backgroundColor = "#fff";
                document.title = "Untitled spreadsheet";
            } else {
                body1.style.display = "none";
                body0.style.display = "inherit";
                parent0.style.backgroundColor = "#ffe";
                document.title = title0;
            }

        };

    //initial
    if (window.location.pathname != "/thread.php" &&
        window.location.pathname != "/read.php" &&
        window.location.pathname != "/") {
        return;
    }

    document.onkeypress = function(event) {
        if (document.readyState != "interactive" &&
            document.readyState != "complete") {
            return;
        }
        if (/textarea|select|input/i.test(event.target.nodeName) ||
            /text|password|number|email|url|range|date|month/i.test(event.target.type)) {
            return;
        }
        var keycode = event.which || event.keyCode;
        if (keycode != 69 && keycode != 101) {
            return;
        }
        status0 = statusMap[getCookie("NgaExcelStatus")] || "none";
        setCookie("NgaExcelStatus", status0, 1000);
        onStatus(status0);
    };

    status0 = getCookie("NgaExcelStatus") || 'none';
    if (status0 == "none") {
        return;
    }

    if (document.readyState != "interactive" &&
        document.readyState != "complete") {

        document.onreadystatechange = function() {
            if (document.readyState == "interactive" ||
                document.readyState == "complete") {
                setTimeout(function() {
                    onStatus(status0);
                }, 55);
            }
        };
        setTimeout(function() {
            style = document.createElement("style");
            style.type = "text/css";
            style.innerHTML = "body{display:none;}";

            document.getElementsByTagName("head")[0].appendChild(style);
        }, 50);
    } else {
        onStatus(status0);
    }
})();