b站 搜索次级排序 bilibili 哔哩哔哩

b站搜索的次级排序功能,能对当前搜索页面的视频进行二次排序

目前为 2021-02-10 提交的版本。查看 最新版本

// ==UserScript==
// @name         b站 搜索次级排序 bilibili 哔哩哔哩
// @namespace    http://tampermonkey.net/
// @version      0.2
// @description  b站搜索的次级排序功能,能对当前搜索页面的视频进行二次排序
// @author       You
// @match        *://search.bilibili.com/*
// @grant        none
// ==/UserScript==

(function () {
    "use strict";
    const container = document.getElementsByClassName("filter-wrap")[0];
    const insertAnchor = container.getElementsByClassName("up")[0];
    const line = document.createElement("ul");
    line.innerHTML =
        '<li class="filter-item active"><a href="javascript:;">次级排序</a></li><li class="filter-item"><a href="javascript:;">最多播放</a></li><li class="filter-item"><a href="javascript:;">最新发布</a></li><li class="filter-item"><a href="javascript:;">最多弹幕</a></li><li class="filter-item"><a href="javascript:;">最多收藏</a></li><li class="filter-item"><a href="javascript:;">最长时长</a></li>';
    line.className = "filter-type clearfix secondly-order";
    container.insertBefore(line, insertAnchor);
    container.getElementsByClassName("up")[0].onclick = function () {
        document.getElementsByClassName("secondly-order")[0].style.display = "None";
    };
    container.getElementsByClassName("down")[0].onclick = function () {
        document.getElementsByClassName("secondly-order")[0].style.display = "";
    };
    function active(element) {
        const brothers = element.parentNode.children;
        for (let brother of brothers) {
            brother.classList.remove("active");
        }
        element.classList.add("active");
        brothers[0].innerHTML = '<a href="javascript:;">恢复默认</a>';
    }
    function activeFirst(){
        active(document.getElementsByClassName('secondly-order')[0].children[0])
    }
    //原始排序
    line.getElementsByTagName("li")[0].onclick = function () {
        active(this);
        this.innerHTML = '<a href="javascript:;">次级排序</a>';
        const videoList = document.getElementsByClassName("video-list")[0];
        const list_v = videoList.__vue__;
        list_v.list.sort(function (a, b) {
            return -(a.rank_score - b.rank_score);
        });
    };
    // 最多播放
    line.getElementsByTagName("li")[1].onclick = function () {
        active(this);
        const videoList = document.getElementsByClassName("video-list")[0];
        const list_v = videoList.__vue__;
        list_v.list.sort(function (a, b) {
            return -(a.play - b.play);
        });
    };
    // 最新发布
    line.getElementsByTagName("li")[2].onclick = function () {
        active(this);
        const videoList = document.getElementsByClassName("video-list")[0];
        const list_v = videoList.__vue__;
        list_v.list.sort(function (a, b) {
            return -(a.pubdate - b.pubdate);
        });
    };
    // 最多弹幕
    line.getElementsByTagName("li")[3].onclick = function () {
        active(this);
        const videoList = document.getElementsByClassName("video-list")[0];
        const list_v = videoList.__vue__;
        list_v.list.sort(function (a, b) {
            return -(a.video_review - b.video_review);
        });
    };
    // 最多收藏
    line.getElementsByTagName("li")[4].onclick = function () {
        active(this);
        const videoList = document.getElementsByClassName("video-list")[0];
        const list_v = videoList.__vue__;
        list_v.list.sort(function (a, b) {
            return -(a.favorites - b.favorites);
        });
    };
    // 最长时长
    line.getElementsByTagName("li")[5].onclick = function () {
        active(this);
        const videoList = document.getElementsByClassName("video-list")[0];
        const list_v = videoList.__vue__;
        list_v.list.sort(function (a, b) {
            const timeA = a.duration.split(":");
            const timeB = b.duration.split(":");
            return -(Number(timeA[0]) * 60 + Number(timeA[1]) - Number(timeB[0]) * 60 - Number(timeB[1]));
        });
    };
    function setFilterClicked() {
        document.getElementsByClassName("filter-item").forEach(function (v, i, a) {
            if(v.parentNode.className.indexOf('secondly-order') < 0){
                            v.onclick = activeFirst
            }
        });
    }
    function setPageClicked() {
        document.getElementsByClassName("page-item").forEach(function (v, i, a) {
            v.onclick = activeFirst
        });
    }

    function videoWrapObservation(mutations, observer) {
        for(let mutation of mutations){
            if(mutation.target.className == 'flow-loader' && mutation.addedNodes.length > 0){
                if(mutation.addedNodes[0].className  == 'page-wrap'){
                    setPageClicked();
                }

            }
        }
    }
    setPageClicked()
    setFilterClicked()
    const videoWrapMutationConfig = { childList: true, subtree: true };
    const observer = new MutationObserver(videoWrapObservation);
    observer.observe(document.getElementsByClassName("flow-loader")[0], videoWrapMutationConfig);
})();

QingJ © 2025

镜像随时可能失效,请加Q群300939539或关注我们的公众号极客氢云获取最新地址