虎扑网红鬼畜表情图屏蔽缩小

去tm的鬼畜网红图,摸鱼万岁

目前為 2023-02-13 提交的版本,檢視 最新版本

// ==UserScript==
// @name         虎扑网红鬼畜表情图屏蔽缩小
// @namespace    http://tampermonkey.net/
// @version      0.8
// @license      MIT
// @description  去tm的鬼畜网红图,摸鱼万岁
// @author       zwxbest
// @match        https://bbs.hupu.com/*.html
// @match        https://m.hupu.com/bbs/*.html
// @icon         https://www.google.com/s2/favicons?sz=64&domain=hupu.com
// @require      https://cdn.staticfile.org/jquery/3.4.1/jquery.min.js
// @resource     bootstrap https://stackpath.bootstrapcdn.com/bootstrap/3.4.1/css/bootstrap.min.css
// @grant        GM_addStyle
// @grant        GM_getResourceText
// @grant        GM_registerMenuCommand
// @grant        GM_setValue
// @grant        GM_getValue
// @grant        GM_deleteValue

// ==/UserScript==


let imageMap = new Map();


let images = [];
images.push("https://i4.hoopchina.com.cn/hupuapp/bbs/659/26124659/thread_26124659_20220502102437_s_2861733_o_w_576_h_768_69592.gif");
images.push("https://i5.hoopchina.com.cn/hupuapp/bbs/109/37338109/thread_37338109_20210220183736_s_5420718_o_w_550_h_550_29803.gif");
images.push("https://i4.hoopchina.com.cn/hupuapp/bbs/0/0/thread_0_20220716191215_s_89079_o_w_440_h_440_62625.jpg");
images.push("https://i3.hoopchina.com.cn/hupuapp/bbs/0/0/thread_0_20220731165903_s_25909_o_w_810_h_595_79234.jpg");
images.push("https://i5.hoopchina.com.cn/hupuapp/bbs/268/27421268/thread_27421268_20220317112134_s_101479_w_855_h_861_74144.jpg");
images.push("https://i10.hoopchina.com.cn/hupuapp/bbs/762/48837762/thread_48837762_20210602234639_s_209846_w_464_h_483_18974.png?x-oss-process=image/resize,w_225/qulity,Q_60");
images.push("https://i1.hoopchina.com.cn/hupuapp/bbs/79/25409079/thread_25409079_20220528082159_s_77155_w_246_h_246_77938.png?x-oss-process=image/resize,w_225/qulity,Q_60");
images.push("https://i4.hoopchina.com.cn/hupuapp/bbs/111111/thread_111111_20220527134517_s_10151_w_196_h_172_66941.jpg?x-oss-process=image/resize,w_225/qulity,Q_60");
images.push("https://i3.hoopchina.com.cn/newsPost/rc-upload-1675337463596-4/1675337542711-92bcc207.jpg");
images.push("https://i3.hoopchina.com.cn/newsPost/rc-upload-1675337463596-2/1675337537456-80e2c558.jpg");
images.push("https://i2.hoopchina.com.cn/hupuapp/bbs/3/thread_3_20230109235917_s_1876759_o_w_289_h_288_30392.gif?x-oss-process=image/resize,w_800/format,webp");
images.push("https://i11.hoopchina.com.cn/reply/1676087734070-0544231e-8490-4739-af07-b2c8d5f58ea5.gif?x-oss-process=image/resize,w_800/format,webp");


let blacklist = new Set();
let blacklist2 = new Set();
let isPC = true;


let GM_KEY_MANUAL_IMAGE = "manual_image_urls";
let GM_KEY_BLOCK_ID = "block_ids";
let GM_KEY_SMALL_ID = "small_ids";


(function () {
    'use strict';

    var bootCSS = GM_getResourceText("bootstrap");
    GM_addStyle(bootCSS);

    let url = window.location.href;
    isPC = !url.includes("m.hupu.com");
    initImageMap();
    updateBlackList();
    updateBlackList2();
    GM_registerMenuCommand("🚫图片屏蔽", setting, "");
    GM_registerMenuCommand("🐌图片缩小", setting2, "");
    GM_registerMenuCommand("➕添加屏蔽图", setting3, "");
    GM_registerMenuCommand("❗清除已添加的屏蔽图", setting4, "");

    let parentClass = isPC ? ".post-reply-list" : ".hp-m-post-page";

    $(document).ready(function () {
        $(parentClass).each(function (i, e) {
            removeImg(e);
            $(e).bind("DOMNodeInserted", function () {
                removeImg(e);
            })
        })
    });
})();

function initImageMap() {
    let manualImageUrls = GM_getValue(GM_KEY_MANUAL_IMAGE, []);
    let allIamges = new Set([...manualImageUrls, ...images]);
    for (let image of allIamges) {
        let idImage = getImageAndId(image);
        imageMap.set(idImage[0], idImage[1]);
    }

}

function getImageAndId(image) {
    let pIndex = image.indexOf("?");
    if (pIndex > -1) {
        image = image.substring(0, pIndex);
    }
    let id = "";//如果url中没有找到标识字符串,用乱码代替,这样不会匹配任何图片
    let index = image.indexOf("thread_");
    if (index > -1) {
        for (let i = index + 7; i < image.length; i++) {
            if (image[i] >= 'a' && image[i] <= 'z') {
                break;
            }
            id += image[i];
        }
    } else {
        let splits = image.split("/");
        let name = splits[splits.length - 1];
        let split2 = name.split(".");
        id = split2[0];
    }
    id = id === "" ? "@1pgCMxYJk*t_" : id;
    return [id, image];
}

function updateBlackList() {
    let checkIds = GM_getValue(GM_KEY_BLOCK_ID, new Set());
    blacklist = new Set();
    for (let id of checkIds) {
        if (imageMap.has(id)) {
            blacklist.add(id);
        }
    }
    console.log(blacklist);
}

function updateBlackList2() {
    let checkIds = GM_getValue(GM_KEY_SMALL_ID, new Set());
    blacklist2 = new Set();
    for (let id of checkIds) {
        if (imageMap.has(id)) {
            blacklist2.add(id);
        }
    }
}

//e是.image-wrapper
function removeImg(e) {
    let imgClass = isPC ? ".thread-img" : "img.hupu-fufu-lazy-img";
    let imgParent = isPC ? ".image-wrapper" : ".discuss-card__images";

    $(e).find(imgClass).each(function (i2, e2) {
        let src = $(e2).attr("src");
        for (let black of blacklist) {
            if (src.includes(black)) {
                $(e2).parents(imgParent).first().remove();
                break;
            }
        }
        for (let black of blacklist2) {
            if (src.includes(black)) {
                let style = $(e2).attr("style");
                $(e2).attr("style", style + "max-height:100px;")
                break;
            }
        }
    });
}

function selectCheckbox(e) {
    let checked = e.checked;
    let id = $(e).attr("id");
    let checkIds = new Set(GM_getValue(GM_KEY_BLOCK_ID, []));
    if (checked) {
        checkIds.add(id);
    } else {
        checkIds.delete(id);
    }
    GM_setValue(GM_KEY_BLOCK_ID, Array.from(checkIds));
    updateBlackList();
}

function selectCheckbox2(e) {
    let checked = e.checked;
    let id = $(e).attr("id");
    let checkIds = new Set(GM_getValue(GM_KEY_SMALL_ID, []));
    if (checked) {
        checkIds.add(id);
    } else {
        checkIds.delete(id);
    }
    GM_setValue(GM_KEY_SMALL_ID, Array.from(checkIds));
    updateBlackList2();
}


function setting() {
    // 初始化打开开关
    addUI();
    let checkIds = new Set(GM_getValue(GM_KEY_BLOCK_ID, []));
    let allChecked = true;
    $(".hp-cbx").each(function (i, e) {
        $(e).click(function () {
            selectCheckbox(this)
        })
        let id = $(e).attr("id");
        if (checkIds.has(id)) {
            $(e).prop("checked", true);
        } else {
            allChecked = false;
        }
    })
    if (allChecked) {
        $("#z_all").prop("checked", true);
    }

}

function addUI() {
    clearUI();
    $("body").append("<div id=\"setting1\" style='right: 10px;top: 100px;background: #f8f8f8;color:#ffffff;overflow: auto;overflow-x:hidden;z-index: 9999;position: fixed;padding:5px;text-align:center;width: 175px;border-radius: 4px;border-style:solid;\n" +
        " border-width:1px; border-color:black;'>\n" +
        "    <div style=\"margin-bottom: 20px\"><span id=\"z_title\" style=\"color:black;\">选择要屏蔽的图</span></div>\n" +
        "    <table id=\"z_table\" border=\"0\" style=\"width: 100%;border-collapse:collapse;\">\n" +
        "    <!--<table id=\"z_table\" class=\"table\" border=\"0\" >-->\n" +
        "        <tr>\n" +
        "            <td style=\"width: 200px;\"><div style=\"color:black;\">全选</div>\n" +
        "                <input type=\"checkbox\" id=\"z_all\"/></td>\n" +
        "            <td style=\"width: 200px;\"></td>\n" +
        "        </tr>\n" +
        "    </table>\n" +
        "</div>");

    for (let key of imageMap.keys()) {
        let value = imageMap.get(key);
        let tr = `<tr>
             <td style="width: 200px;"><input type="checkbox" id="${key}" class="hp-cbx"/></td>
             <td><img src="${value}" height="50px"/></td>
            </tr>`
        $("#z_table").append(tr)
    }

    let divStyle = $("#setting1").attr("style");
    if (isPC) {
        divStyle = divStyle + ";max-height: 800px;"
    } else {
        divStyle = divStyle + ";max-height: 600px;"
    }
    $("#setting1").attr("style", divStyle);
    $("#z_all").each(function (i, e) {
        $(e).click(function () {
            if (e.checked) {
                $(".hp-cbx").prop("checked", false);
            } else {
                $(".hp-cbx").prop("checked", true);
            }
            $(".hp-cbx").trigger("click");
        })
    })

    $("td").each(function (i, e) {
        let style = $(e).attr("style") ? $(e).attr("style") : "";
        $(e).attr("style", style + "border-bottom :1px solid black;")
    })
}

function clearUI() {
    $("#setting1").remove();
    $("#setting2").remove();
    $("#setting3").remove();
}

function setting2() {
    // 初始化打开开关
    addUI();
    $("#setting1").attr("id", "setting2");
    $("#z_title").text("选择要缩小的图");
    let checkIds2 = new Set(GM_getValue(GM_KEY_SMALL_ID, []));

    let allChecked = true;

    $(".hp-cbx").each(function (i, e) {
        $(e).click(function () {
            selectCheckbox2(this)
        })
        let id = $(e).attr("id");
        if (checkIds2.has(id)) {
            $(e).prop("checked", true);
        } else {
            allChecked = false;
        }
    })
    if (allChecked) {
        $("#z_all").prop("checked", true);
    }

}

function setting3() {
    clearUI();
    $("body").append("<div id=\"setting3\" style='right: 10px;top: 100px;background: #f8f8f8;overflow: auto;overflow-x:hidden;z-index: 9999;position: fixed;padding:5px;text-align:center;width: 300px;border-radius: 4px;border-style:solid;\n" +
        " border-width:1px; border-color:black;'>\n" +
        "    <form>\n" +
        "        <div class=\"form-group\">\n" +
        "            <label for=\"z_input_img\">图片链接</label>\n" +
        "            <div><img alt = \"img\" id=\"z_img\" src=\"\" style=\"max-height: 100px\" hidden/></div>\n" +
        "            <input type=\"text\" class=\"form-control\" id=\"z_input_img\" placeholder=\"图片链接\">\n" +
        "            <label id=\"z_input_img_err\" style=\"color:red\" hidden>图片链接无效</label>\n" +
        "        </div>\n" +
        "        <button type=\"submit\" id=\"z_submit\" class=\"btn btn-default\">添加</button>\n" +
        "    </form>\n" +
        "</div>");

    $("#z_input_img").blur(function () {
        checkValid()
    })

    $("#z_submit").click(function () {
        let valid = checkValid();
        if (valid) {
            //保存url到配置中
            let imgUrls = new Set(GM_getValue(GM_KEY_MANUAL_IMAGE, []));
            let value = $("#z_input_img").val();
            imgUrls.add(value);

            GM_setValue(GM_KEY_MANUAL_IMAGE, Array.from(imgUrls));

            //更新屏蔽id
            let idImage = getImageAndId(value)
            let checkIds = new Set(GM_getValue(GM_KEY_BLOCK_ID, []));
            checkIds.add(idImage[0]);
            GM_setValue(GM_KEY_BLOCK_ID, Array.from(checkIds));

            console.log(imgUrls)
            console.log(checkIds)
            $("#setting3").remove();
            location.reload();
        }
    })

    function checkValid() {
        let value = $("#z_input_img").val();
        if (!value.includes("http")) {
            $("#z_img").attr("hidden", true);
            $("#z_input_img_err").attr("hidden", false);
            return false;
        } else if (!value.includes(".jpg") && !value.includes(".png") && !value.includes(".gif")) {
            $("#z_img").attr("hidden", true);
            $("#z_input_img_err").attr("hidden", false);
            return false;
        } else {
            $("#z_input_img_err").attr("hidden", true);
            $("#z_img").attr("src", value);
            $("#z_img").attr("hidden", false);
            return true;
        }
    }
}

function setting4() {
    let r = window.confirm("此操作将清除已添加的所有屏蔽图片!");
    if (r) {
        GM_deleteValue(GM_KEY_MANUAL_IMAGE);
        location.reload();

    }

}