Search posts by user

Search for posts by user in current topic

目前為 2017-12-14 提交的版本,檢視 最新版本

您需要先安裝使用者腳本管理器擴展,如 TampermonkeyGreasemonkeyViolentmonkey 之後才能安裝該腳本。

You will need to install an extension such as Tampermonkey to install this script.

您需要先安裝使用者腳本管理器擴充功能,如 TampermonkeyViolentmonkey 後才能安裝該腳本。

您需要先安裝使用者腳本管理器擴充功能,如 TampermonkeyUserscripts 後才能安裝該腳本。

你需要先安裝一款使用者腳本管理器擴展,比如 Tampermonkey,才能安裝此腳本

您需要先安裝使用者腳本管理器擴充功能後才能安裝該腳本。

(我已經安裝了使用者腳本管理器,讓我安裝!)

你需要先安裝一款使用者樣式管理器擴展,比如 Stylus,才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展,比如 Stylus,才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展,比如 Stylus,才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展後才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展後才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展後才能安裝此樣式

(我已經安裝了使用者樣式管理器,讓我安裝!)

// ==UserScript==
// @name           Search posts by user
// @name:ru        Поиск постов пользователя
// @description    Search for posts by user in current topic
// @description:ru Найти посты пользователя в текущей теме
// @version        1.0.3
// @date           13.12.2017
// @author         Halibut
// @namespace      https://greasyfork.org/en/users/145947-halibut
// @homepageURL    https://greasyfork.org/en/scripts/36319-search-posts-by-user
// @supportURL     https://greasyfork.org/en/scripts/36319-search-posts-by-user/feedback
// @license        HUG-WARE
// @include        http*://forum.ru-board.com/topic.cgi?forum=*&topic=*
// @noframes
// @grant          none
// ==/UserScript==

/******************************************************************************
 * "THE HUG-WARE LICENSE" (Revision 2): As long as you retain this notice you *
 * can do whatever you want with this stuff. If we meet some day, and you     *
 * think this stuff is worth it, you can give me/us a hug.                    *
******************************************************************************/

((body, img, listener) => {
    'use strict';
    if (listener.isFF) {
        const context = body.getAttribute("contextmenu"),
              menu = context
                        ? document.getElementById(context)
                        : body.appendChild(document.createElement("menu")),
              mitem = menu.appendChild(document.createElement("menuitem"));
        if (!context) {
            menu.id = "GM_page-actions";
            menu.type = "context";
            body.setAttribute("contextmenu", "GM_page-actions");
        };
        mitem.label = "Найти все собщения пользователя в теме";
        mitem.addEventListener("click", listener)
    };
    const button = listener.actnBox.getElementsByTagName("td")[0].appendChild(document.createTextNode("   "))
                         && listener.actnBox.getElementsByTagName("td")[0].appendChild(document.createElement("a")),
          imgNode = button.appendChild(document.createElement("img"));
    imgNode.src = img;
    button.addEventListener("click", listener)
})(
    document.body
    , "data:image/gif;base64,R0lGODlhXgAPAJEAAAAAAP///////wAAACH5BAEAAAIALAAAAABeAA8AAAKUhI+py+0PIwq02ouz3rz7HxjgSJamJ57qym5plcAvAMs0dYfuhL/hzPvlgrLLEFfT/ZDMWy5zVCqjzKozWbREt7qrsQpNcsNYr7T3bYq74N6TPF3Dpc4Dxke3ImlmuWYcNwf4hjeIFhiIp+Vn9uTIaERYNjNJ9bZo5WOHaWNTeNkSKgqiOGp6Gomquuom4foKGwtRAAA7"
    , {
        get win() {
            delete this.win;
            return this.win = window.top
        }
        , get actnBox() {
            delete this.actnBox;
            return this.actnBox = [...document.getElementsByTagName("table")].find(el => el.querySelector("a[href^='post.cgi?action=new']"))
        }
        , get isFF() {
            delete this.isFF;
            return this.isFF = this.win.navigator.product == "Gecko"
        }
        , get spHd() {
            delete this.spHd;
            return this.spHd = this.getSpHd()
        }
        , get spBd() {
            delete this.spBd;
            return this.spBd = this.getSpBd()
        }
        , getPosts(url) {
            return new Promise((res, rej) => {
                const xhr = new XMLHttpRequest();
                xhr.open("GET", url, true);
                xhr.onload = re => {
                    if (!(xhr.readyState == 4 && xhr.status == 200)) return rej("error");
                    const posts = xhr.responseXML.getElementsByClassName('tb');
                    if (posts && !!posts.length)
                        res([...posts]);
                };
                xhr.onerror = xhr.onabort = xhr.ontimeout = () => rej("error");
                xhr.responseType = "document";
                xhr.send();
            })
        }
        , getUsrName() {
            const prompt = this.win.prompt('Задайте имя для поиска', "")
            return prompt && prompt.toLowerCase().replace(/\s/g, '_');
        }
        , getPages() {
            const paginator = [...document.getElementsByTagName("p")].find(el => el.textContent && el.textContent.startsWith("Страницы: "));
            if (paginator)
                return [...paginator.getElementsByTagName("td")[0].children].map(el => el.href || this.win.location.href);
            else
                return [this.win.location.href]
        }
        , getSpHd() {
            let spoilerHead = document.getElementById("spu-spoiler-head");
            if (!spoilerHead) {
                const dummyNode = this.actnBox.parentNode.insertBefore(document.createElement('div'), this.actnBox.nextElementSibling),
                      show = decodeURIComponent("%E2%96%BA") + ' Показать результаты поиска',
                      hide = decodeURIComponent("%E2%96%BC") + ' Скрыть результаты поиска';
                dummyNode.outerHTML = '<table width="95%" cellspacing="1" cellpadding="3" bgcolor="#999999" align="center" border="0"><tbody><tr><td valign="middle" bgcolor="#dddddd" align="left"></td></tr></tbody></table>';
                spoilerHead = this.actnBox.nextElementSibling;
                spoilerHead.id = "spu-spoiler-head";
                spoilerHead.hidden = true;
                const spTitle = spoilerHead.getElementsByTagName('td')[0];
                spoilerHead.style.cssText = '-moz-user-select: none !important;-webkit-user-select: none !important; -ms-user-select: none !important; user-select: none !important; cursor: pointer !important';
                spTitle.textContent = show;
                spoilerHead.onclick = e => {
                    if (e.button != 0 || !this.spBd) return;
                    e.preventDefault(); e.stopPropagation();
                    this.spBd.hidden = !this.spBd.hidden;
                    spTitle.textContent = this.spBd.hidden ? show : hide;
                }
            }
            return spoilerHead;
        }
        , getSpBd() {
            let spoilerBody = document.getElementById("spu-spoiler-body");
            if (!spoilerBody) {
                spoilerBody = this.spHd.parentNode.insertBefore(document.createElement("table"), this.spHd.nextElementSibling);
                spoilerBody.id = "spu-spoiler-body";
                spoilerBody.align = "center";
                spoilerBody.width = "95%";
                spoilerBody.style.border = "1px solid black";
                spoilerBody.style.padding = "1em .5em"
                spoilerBody.hidden = true;
            }
            return spoilerBody;
        }
        , endNotify(suc,err) {
            if (!!suc)
                this.win.alert("Поиск закончен!\nНайдено: " + suc + (!!err ? "\nОшибок при запросе страниц: " + err : ""));
            else
                this.win.alert("Постов не найдено!" + (!!err ? "\nОшибок при запросе страниц: " + err : ""));
        }
        , handleEvent(e) {
            e.preventDefault(); e.stopPropagation();
            const usrName = this.getUsrName();
            if (!usrName) return;
            const pageLinks = this.getPages();
            if (!(pageLinks && pageLinks.length)) return;
            const postsByUser = [],
                  errors = [];
            if (this.spBd.children && !!this.spBd.children.length)
                for (const node of [...this.spBd.children])
                    node.remove();
            pageLinks.map((link, indx) => {
                this.getPosts(link).then(
                    re => {
                        const finded = re.filter(post => !post.querySelector('a.tpc[href$="&postno=1"]') && post.querySelector("td.dats > a.m > b").textContent.toLowerCase().replace(/\s/g, '_') == usrName);
                        if (finded && !!finded.length) {
                            this.spHd.hidden = false;
                            for (const post of finded) {
                                postsByUser.push("!");
                                this.spBd.appendChild(post);
                            }
                        }
                    }
                    , err => postsByUser.push("?")
                ).then(end => {
                    if (indx == pageLinks.length - 1)
                        setTimeout(() => this.endNotify(postsByUser.length,errors.length) && (postsByUser.length = errors.length = 0), 1000);
                })
            })
        }
    }
);