Come Back Tieba Thumbnails

让被设置为页游的贴吧首页能正常显示缩略图

目前為 2017-07-22 提交的版本,檢視 最新版本

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

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

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

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

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

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

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

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

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

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

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

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

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

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

// ==UserScript==
// @name         Come Back Tieba Thumbnails
// @namespace    https://greasyfork.org/zh-CN/users/14997-lrh3321
// @version      0.1
// @description  让被设置为页游的贴吧首页能正常显示缩略图
// @author       LRH3321
// @match        *://tieba.baidu.com/f*
// @icon         http://www.baidu.com/favicon.ico
// @connect      baidu.com
// @grant        GM_xmlhttpRequest
// ==/UserScript==
(function () {
    'use strict';
    if (!document.body.classList.contains("app_forum_body"))
        return;
    var hooked = false;
    BigpipeHook();
    thumbnailsBack();
    function BigpipeHook() {
        if (hooked)
            return;
        var originalBroadcast = Bigpipe.broadcast;
        hooked = true;
        Bigpipe.broadcast = function (e, t) {
            // console.log(e, t);
            var r = originalBroadcast(e, t);
            if (e === "page_change") {
                window.setTimeout(thumbnailsBack, 1000);
                // thumbnailsBack();
            }
            return r;
        };
    }
    function thumbnailsBack() {
        console.log("Getting thumbnails.");
        var MOBILE_USER_AGENT = "Mozilla/5.0 (iPhone; CPU iPhone OS 9_1 like Mac OS X) AppleWebKit/601.1.46 (KHTML, like Gecko) Version/9.0 Mobile/13B143 Safari/601.1";
        function createUList(tid, items) {
            var ul = document.createElement("ul");
            ul.className = "threadlist_media j_threadlist_media clearfix";
            ul.id = "fm" + tid;
            ul.style.cssFloat = "left";
            for (var _i = 0, items_1 = items; _i < items_1.length; _i++) {
                var el = items_1[_i];
                var li = document.createElement("li");
                if (el.hasAttribute("v_href")) {
                    console.log(el);
                    if (el)
                        continue;
                    // 视频帖
                    var div = document.createElement("div");
                    div.className = "threadlist_video";
                    var img = document.createElement("img");
                    img.src = el.src;
                    div.appendChild(img);
                    var a = document.createElement("a");
                    a.href = "#";
                    a.className = "threadlist_btn_play j_m_flash";
                    a.dataset.threadid = tid;
                    a.dataset.forumid = PageData.forum.id.toString();
                    a.dataset.isfive = "0";
                    a.dataset.video = el.getAttribute("v_href");
                    
                    a.dataset.vsrc = el.getAttribute("v_href");
                    
                    a.dataset.type = "movideo";
                    a.dataset.duration = "";
                    div.appendChild(a);
                    li.appendChild(div);
                }
                else {
                    var a = document.createElement("a");
                    a.className = "thumbnail vpic_wrap";
                    var img = document.createElement("img");
                    img.className = "threadlist_pic j_m_pic";
                    img.style.display = "inline";
                    img.style.width = "90px";
                    img.style.height = "90px";
                    var src = el.getAttribute("data-url");
                    var arr = src.split("/");
                    var pid = arr.pop();
                    img.src = src;
                    img.setAttribute("data-original", src);
                    img.setAttribute("bpic", "//imgsrc.baidu.com/forum/pic/item/" + pid);
                    a.appendChild(img);
                    li.appendChild(a);
                }
                ul.appendChild(li);
            }
            return ul;
        }
        function crateSmallWrap(tid, count, items) {
            var wrap = document.createElement("div");
            wrap.className = "small_wrap j_small_wrap";
            wrap.setAttribute("is_handle", "true");
            var anchor = document.createElement("a");
            anchor.onclick = function () { return false; };
            anchor.style.display = "none";
            anchor.className = "small_btn_pre j_small_pic_pre";
            wrap.appendChild(anchor);
            anchor = anchor.cloneNode(true);
            anchor.className = "small_btn_next j_small_pic_next";
            wrap.appendChild(anchor);
            var smallList = document.createElement("div");
            smallList.className = "small_list j_small_list cleafix";
            var smallListGallery = document.createElement("div");
            smallListGallery.className = "small_list_gallery";
            smallListGallery.appendChild(createUList(tid, items));
            if (count) {
                var smallPicNum = document.createElement("div");
                smallPicNum.className = "small_pic_num center_text";
                smallPicNum.textContent = "\u5171 " + count + " \u5F20";
                smallListGallery.appendChild(smallPicNum);
            }
            smallList.appendChild(smallListGallery);
            wrap.appendChild(smallList);
            return wrap;
        }
        GM_xmlhttpRequest({
            method: "GET",
            url: location.href,
            headers: { "User-Agent": MOBILE_USER_AGENT },
            onload: function (resp) {
                if (resp.status < 400) {
                    // console.log(resp);
                    var f = document.createElement("div");
                    f.innerHTML = resp.responseText;
                    var tlist = f.querySelectorAll("#tlist a.j_common[tid]");
                    var tarray = Array.from(tlist);
                    tarray.forEach(function (el) {
                        var mediasItems = el.querySelectorAll("div.medias_item img");
                        if (mediasItems.length === 0) {
                            return;
                        }
                        var s = undefined;
                        var medias_modal = el.querySelector("div.medias_modal");
                        if (medias_modal) {
                            s = medias_modal.textContent.trim();
                            s = s.substring(1, s.length - 1);
                        }
                        var tid = el.getAttribute("tid");
                        var node = document.querySelector("a[href='/p/" + tid + "']");
                        if (node) {
                            node.parentElement
                                .parentElement
                                .parentElement
                                .querySelector("div.threadlist_text")
                                .appendChild(crateSmallWrap(tid, s, mediasItems));
                        }
                    });
                }
            }
        });
        console.log("Getting done.");
    }
})();