自动设置B站的自动连播(自动切集)

B站的多数页面中,自动切集功能在播放器里,修改起来很麻烦,所以按照我的习惯做了在不同页面自动切换的功能(脚本运行后可在存储中修改配置),具体包括:1.【自己的收藏等列表页面】自动连播;2.【普通视频/稍后再看/番剧】不自动连播;3.其余情况默认也不自动连播。【吐槽】视频或番剧看完可能会想看评论区,或者点赞等,所以不想自动连播,尤其是番剧最新一集播完可能连播到奇怪的视频。

// ==UserScript==
// @name         自动设置B站的自动连播(自动切集)
// @namespace    https://github.com/SineObama/bilibili-player-auto-set-playtype
// @homepage     https://github.com/SineObama/bilibili-player-auto-set-playtype
// @version      0.2.0
// @description  B站的多数页面中,自动切集功能在播放器里,修改起来很麻烦,所以按照我的习惯做了在不同页面自动切换的功能(脚本运行后可在存储中修改配置),具体包括:1.【自己的收藏等列表页面】自动连播;2.【普通视频/稍后再看/番剧】不自动连播;3.其余情况默认也不自动连播。【吐槽】视频或番剧看完可能会想看评论区,或者点赞等,所以不想自动连播,尤其是番剧最新一集播完可能连播到奇怪的视频。
// @author       SineObama
// @match        https://www.bilibili.com/*
// @grant        GM_setValue
// @grant        GM_getValue
// @grant        GM_registerMenuCommand
// @grant        GM_unregisterMenuCommand
// @license      MIT
// @icon         data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAACgAAAAuCAMAAABteatCAAADAFBMVEX///////n4+fzuz7Wpuc7q///x2L+zwdTx///89e3x8uDd4u3uy6yUn7XV9f///+3IqaCeweDqxamUpsH/5cizuLmsqaaboaaboaOXm6CUmaCUm6Obrsju/////ePBp6CUp8Hn/ebBpqD/5c6+vrKisb/P087d17+epL/d9PbEtbnZ8v//9dGwn6Cbu9fx9O3x8uPIrKCUr8jq8u3x8u3x9Pb/7cipoaabnKCboa/I7f//6cGim6a+4/z/6sGipsHq//znxann/f//////6d3j+v+imaO23fz/27ybobzZ////8tS+uLmwp6OUnqmwuL/V493S087Itqysw8vSybKbobLI08vBr6yzy87g8f////PSs6OUq8j/17WUmbXV//+Unqa62/+Um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65qXL0GXeFaBrt18fOVkBSu37Bx0+YtW7dt3wG0PnDnrt176vbKzlfaJ7r/wMFDh5EU6hwBRlP50WPHxUHgxMlTp6XPMMxXmgbSNmMmihuxgJyz5yDUebJT74WLly5LoYDLly5ewFB25eo1KanrqAqB3GtXr6Aou3HzltTtO3fv3bt75zaqYqlbN28g1N2/LHX7LoxzF13p5ftw8y5LPUA2/wG6SpiZN1HVYaq8CfXHrdvoXkOz/RbER1el7qIrvItm5FVw+F27jRmoaEZeA4XnRak7mArvoBl5ESh2CdNmTLsvAcUuX7+HqfAeWhxdBopJSRFWKCUFNpGw1WATLxHjmUtgXxMOHrCvL1wjHODgcATGDKEohMQMMK4JJQpoXANTD/5kBks9oPSIL+Ei0iMohUvhygrIKRySZ3AAlDxDfC4kIV+TA5yxAgBl3Dfmj9YMtwAAAABJRU5ErkJggg==
// ==/UserScript==

'use strict';

// 方便自己随时修改代码,固定使用代码中的最新配置内容
var reset = loadStorage('__ALWAYS_RESET_CONFIG__', false);
// 实验性功能:阻止番剧最后一集播完后自动跳转
var experimentalFeature = loadStorage('__EXPERIMENTAL_FEATURE__', false);
// 配置需要开启功能的页面
var urlsToOpen = loadStorage('urlsToOpen', [
    'bilibili.com/list',
], reset);
// 配置需要关闭功能的页面
var urlsToClose = loadStorage('urlsToClose', [
    'bilibili.com/video',
    'bilibili.com/bangumi',
    'bilibili.com/list/watchlater',
], reset);
// 配置默认行为(没有匹配以上规则时):true/false 设置为开启/关闭; null 不修改
var unmatchBehavior = loadStorage('unmatchBehavior', false, reset);

// ==== 配置结束 ====

var myBlockJump = experimentalFeature;

doCheck(unmatchBehavior);

function doCheck(isOpen) {

    isOpen = travelList(urlsToOpen, true, isOpen);
    isOpen = travelList(urlsToClose, false, isOpen);

    if (isOpen === null) {
        return;
    }

    initScriptMenu();

    doBlockJump();

    var radioIdx = isOpen ? 0 : 1;
    // 需要多次检查结果,避免被B站设置覆盖
    var keepCount = 0;
    var id = setInterval(function () {
        var el = document.getElementsByClassName('bpx-player-ctrl-setting-handoff')[0];
        if (el) {
            console.debug('video_status.playtype radioIdx', radioIdx, 'keep', keepCount);
            if (el.getElementsByClassName('bui-radio-input')[radioIdx].checked) {
                keepCount++;
                if (keepCount > 2) {
                    clearInterval(id);
                }
            } else {
                el.getElementsByClassName('bui-radio-item')[radioIdx].click();
                keepCount = 0;
            }
        }
    }, 1000);
}

function loadStorage(key, defaultValue, isReset) {
    if (!isReset) {
        var value = GM_getValue(key);
        if (value !== undefined) {
            return value;
        }
    }
    GM_setValue(key, defaultValue);
    return defaultValue;
}

function travelList(list, target, defaultResult) {
    var result = defaultResult;
    for (var i = 0; i < list.length; i++) {
        if (_matchLocation(list[i])) {
            result = target;
            break;
        }
    }
    return result;
}

function _matchLocation(matcher) {
    if (typeof matcher === 'string') {
        if (location.href.indexOf(matcher) > -1) {
            return true;
        }
    } else {
        if (matcher.test(location.href)) {
            return true;
        }
    }
    return false;
}

// 失败方法:

// var settings = JSON.parse(localStorage.getItem('bilibili_player_settings'));
// if (/bilibili.com\/list/.test(location.href) && location.href.indexOf('bilibili.com/list/watchlater') < 0) {
//     if (settings.video_status.playtype === 2) {
//         settings.video_status.playtype = 1;
//         localStorage.setItem('bilibili_player_settings', JSON.stringify(settings));
//     }
// } else {
//     if (settings.video_status.playtype === 1) {
//         settings.video_status.playtype = 2;
//         localStorage.setItem('bilibili_player_settings', JSON.stringify(settings));
//     }
// }

function initScriptMenu() {
    var menuId = GM_registerMenuCommand(`[ ${experimentalFeature ? '✓' : '✗'} ] 实验性功能:阻止番剧最后一集播完跳转`, () => {
        myBlockJump = experimentalFeature = loadStorage('__EXPERIMENTAL_FEATURE__', !experimentalFeature, true)
        clearMenu()
        initScriptMenu()
    });

    function clearMenu() {
        GM_unregisterMenuCommand(menuId)
    }
}

function doBlockJump() {

    // 阻止 History.pushState 方法的调用
    var pushStateAssign = window.History.prototype.pushState;

    Object.defineProperty(window.History.prototype, 'pushState', {
        get: function () {
            debugger
            try {
                // 启用阻止时,通过异常阻止B站页面跳转!这是偶然发现的方法,单纯throw不行,不清楚原理
                return shouldPrevent() ? pushStateAssign.get.call(this) : pushStateAssign;
            } catch (e) {
                // 根据B站源码添加属性,使其走相应逻辑,不再进行跳转
                e.cancelled = true;
                throw e;
            }
        }
    });

    // 如果是正常的人为点击操作,则允许页面跳转
    window.addEventListener('click', releaseJump, true);

    function shouldPrevent() {
        return experimentalFeature && myBlockJump && _matchLocation('bilibili.com/bangumi');
    }

    var reBlockNum;

    function releaseJump(e) {
        if (!experimentalFeature || !myBlockJump) {
            return;
        }

        // 不管点击的是什么,实际可能有很多情况,
        // 通过暂时关闭阻止功能来允许页面跳转
        myBlockJump = false;
        clearTimeout(reBlockNum);
        reBlockNum = setTimeout(function () {
            myBlockJump = true;
        }, 500);
    }

    // 其他不可能实现的方法(常识):location location.href 和 location.replace 都是不可以被修改的
}