Greasy Fork

Greasy Fork is available in English.

直播吧助手

直播吧使用助手,包括标题栏比分、新闻模块排序调整等

当前为 2020-12-16 提交的版本,查看 最新版本

您需要先安装一款用户脚本管理器扩展,例如 Tampermonkey 篡改猴Greasemonkey 油猴子Violentmonkey 暴力猴,才能安装此脚本。

您需要先安装一款用户脚本管理器扩展,例如 Tampermonkey 篡改猴,才能安装此脚本。

您需要先安装一款用户脚本管理器扩展,例如 Tampermonkey 篡改猴Violentmonkey 暴力猴,才能安装此脚本。

您需要先安装一款用户脚本管理器扩展,例如 Tampermonkey 篡改猴Userscripts ,才能安装此脚本。

您需要先安装一款用户脚本管理器扩展,例如 Tampermonkey 篡改猴,才能安装此脚本。

您需要先安装一款用户脚本管理器扩展后才能安装此脚本。

(我已经安装了用户脚本管理器,让我安装!)

您需要先安装一款用户样式管理器扩展,比如 Stylus,才能安装此样式。

您需要先安装一款用户样式管理器扩展,比如 Stylus,才能安装此样式。

您需要先安装一款用户样式管理器扩展,比如 Stylus,才能安装此样式。

您需要先安装一款用户样式管理器扩展后才能安装此样式。

您需要先安装一款用户样式管理器扩展后才能安装此样式。

您需要先安装一款用户样式管理器扩展后才能安装此样式。

(我已经安装了用户样式管理器,让我安装!)

// ==UserScript==
// @name            直播吧助手
// @namespace       http://tampermonkey.net/
// @version         2.1
// @description     直播吧使用助手,包括标题栏比分、新闻模块排序调整等
// @author          purezhi
// @match           https://*.zhibo8.cc/*
// @require         https://cdn.jsdelivr.net/npm/[email protected]/dist/jquery.min.js
// @grant           none
// ==/UserScript==

let page = '';
let $cntr = null;
let modSltr = '';
let headSltr = '';
let ttlSltr = '';
let localKey = '';

(function() {
    'use strict';

    initSorting();
    initScoreTitle();
})();

function initSorting() {
    if (/^https?:\/\/www\.zhibo8\.cc\/nba\/?$/.test(location.href)) {
        page = 'www_nba';
    }
    else if (/^https?:\/\/www\.zhibo8\.cc\/zuqiu\/?$/.test(location.href)) {
        page = 'www_zuqiu';
    }
    else if (/^https?:\/\/news\.zhibo8\.cc\/nba\/?$/.test(location.href)) {
        page = 'news_nba';
    }
    else if (/^https?:\/\/news\.zhibo8\.cc\/zuqiu\/?$/.test(location.href)) {
        page = 'news_zuqiu';
    }

    if (!page) return;

    $cntr = $('#middle .m_left');
    modSltr = '.video.v_change';
    headSltr = '.v_top';
    ttlSltr = '.v_top>a:first-child';
    localKey = 'sorting_' + page;

    $('.menu .container').append( ' \
        <div style="position: absolute; right: 2.5px; top: 2.5px;"> \
            <button mod-order-save style="height: 24px; line-height: 22px; padding: 0 8px;">保存排序</button> \
            <button mod-order-reset style="height: 24px; line-height: 22px; padding: 0 8px;">重置排序</button> \
        </div> \
    ');

    $(document).on('click', '[mod-order-save]', function (event) {
        saveSort();
    });

    $(document).on('click', '[mod-order-reset]', function (event) {
        if (localStorage.getItem(localKey) != null) {
            localStorage.removeItem(localKey);
        }
        location.reload();
    });

    $(document).on('click', '[mod-top]', function (event) {
        let $el = $(this);
        let $mod = $el.closest(modSltr);
        if ($mod.length <= 0 || $mod.prevAll(modSltr).length <= 0) return;
        $mod.insertBefore($mod.prevAll(modSltr).last());
    });

    $(document).on('click', '[mod-prev]', function (event) {
        let $el = $(this);
        let $mod = $el.closest(modSltr);
        if ($mod.length <= 0 || $mod.prev(modSltr).length <= 0) return;
        $mod.insertBefore($mod.prev(modSltr));
    });

    $(document).on('click', '[mod-next]', function (event) {
        let $el = $(this);
        let $mod = $el.closest(modSltr);
        if ($mod.length <= 0 || $mod.next(modSltr).length <= 0) return;
        $mod.insertAfter($mod.next(modSltr));
    });

    let sortings = loadSort();

    const modCount = $(modSltr).length;
    $(modSltr).each(function (idx, el) {
        let $head = $(el).find(headSltr);
        if (!$head) return;

        let $titleEl = $(el).find(ttlSltr);
        if (!$titleEl) return;

        const title = $titleEl.html();
        if (!title) return;

        let sort = modCount - idx;
        if (sortings && sortings[title] != null) {
            sort = sortings[title];
        }

        $(el).attr('mod-order', sort);

        $head.css('position', 'relative');
        $head.append(
            '<div mod-sort style="position: absolute; top: -20px; left: 0; right: 0; padding: 0; background: rgba(255,255,255,0.7); backdrop-filter: blur(12px); opacity: 0.15;"> \
                <a class="more" mod-next style="cursor: pointer; font-size: 14px !important;">➡️️ 后移</a> \
                <a class="more" mod-prev style="cursor: pointer; font-size: 14px !important; margin-right: 5px;">⬅️ 前移</a> \
                <a class="more" mod-top style="cursor: pointer; font-size: 14px !important; margin-right: 5px;">⬆️置顶</a> \
            </div>'
        );
    });

    doSort();

    $('[mod-sort]').on('mouseover', function (event) {
        $(this).css('opacity', '1');
    })
    $('[mod-sort]').on('mouseout', function (event) {
        $(this).css('opacity', '0.15');
    });
}

function initScoreTitle() {
    if (!/https?:\/\/(www\.)?zhibo8\.cc\/zhibo\/nba\/([^\.]+?)\.htm/.test(location.href)) return;

    var ttlIntrvl = setInterval(function() {
        showScoreTitle();
    }, 3000);
    showScoreTitle();
}

function doSort() {
    const modArr = []
    let max = 0;
    $(modSltr).each(function (idx, el) {
        let sort = $(el).attr('mod-order');
        if (!sort) return;

        sort = parseInt(sort)
        modArr[sort] = $(el);
        if (max < sort) max = parseInt(sort);
    });

    for (let i = 0; i <= max; i++) {
        if (!modArr[i]) continue;
        modArr[i].insertBefore(modSltr + ':first-child');
    }
}

function loadSort() {
    if (localStorage.getItem(localKey)) {
        return JSON.parse(localStorage.getItem(localKey));
    }
    return null;
}

function saveSort() {
    const sortings = {};
    const modCount = $(modSltr).length;
    $(modSltr).each(function (idx, el) {
        let $head = $(el).find(headSltr);
        if (!$head) return;

        let $titleEl = $(el).find(ttlSltr);
        if (!$titleEl) return;

        const title = $titleEl.html();
        if (!title) return;

        sortings[title] = modCount - idx;
    });

    localStorage.setItem(localKey, JSON.stringify(sortings));
    console.log(localStorage.getItem(localKey));
}

function showScoreTitle() {
    const vtm = document.querySelectorAll('.bf_table .visit_team_name a');
    if (vtm.length < 1) {
        console.warn("no visit team");
        return;
    }
    const vsc = document.querySelectorAll('.bf_top .bf_box.tmtop .time_score .visit_score');
    if (vtm.length < 1) {
        console.warn("no visit score");
        return;
    }
    const htm = document.querySelectorAll('.bf_table .home_team_name a');
    if (vtm.length < 1) {
        console.warn("no host team");
        return;
    }
    const hsc = document.querySelectorAll('.bf_top .bf_box.tmtop .time_score .host_score');
    if (vtm.length < 1) {
        console.warn("no host score");
        return;
    }

    document.title = `${vtm[0].innerText} ${vsc[0].innerText} ➲ ${htm[0].innerText} ${hsc[0].innerText}`;
}