Region loop

2024/5/15 14:39:50

当前为 2024-05-15 提交的版本,查看 最新版本

您需要先安装一个扩展,例如 篡改猴Greasemonkey暴力猴,之后才能安装此脚本。

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

您需要先安装一个扩展,例如 篡改猴暴力猴,之后才能安装此脚本。

您需要先安装一个扩展,例如 篡改猴Userscripts ,之后才能安装此脚本。

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

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

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

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

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

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

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

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

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

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

// ==UserScript==
// @name        Region loop
// @namespace   Violentmonkey Scripts
// @match       https://www.bilibili.com/video/*
// @grant       unsafeWindow
// @version     1.0.1
// @author      5ec1cff
// @run-at      document-body
// @description 2024/5/15 14:39:50
// @license     MIT
// ==/UserScript==

((window) => {
    let document = window.document;
    let enableCheck;
    let leftBtn;
    let leftResetBtn;
    let rightBtn;
    let rightResetBtn;

    let left = null, right = null, enabled = false;
    function install(dom, video) {
        let root = dom.querySelector('#my_play_control');
        if (root == null) root = document.createElement('div');
        root.id = 'my_play_control';
        dom.appendChild(root);
        root.innerHTML = `
        <input type="checkbox" id="enable_loop">
        <label for="enable_loop">开启循环</label>
        <input type="button" value="[x" id="reset_left" />
        <input type="button" value="[" id="set_left" />
        <input type="button" value="]" id="set_right" />
        <input type="button" value="x]" id="reset_right" />
        `;
        enableCheck = root.querySelector('#enable_loop');
        leftBtn = root.querySelector('#set_left');
        leftResetBtn = root.querySelector('#reset_left');
        rightBtn = root.querySelector('#set_right');
        rightResetBtn = root.querySelector('#reset_right');
        enableCheck.addEventListener('change', () => {
            enabled = enableCheck.checked;
        })
        leftBtn.addEventListener('click', () => {
            let newLeft = video.currentTime;
            if (newLeft >= (right ?? Infinity)) return;
            left = newLeft;
            leftBtn.value='['+left;
        })
        rightBtn.addEventListener('click', () => {
            let newRight = video.currentTime;
            if (newRight <= (left ?? 0)) return;
            right = newRight;
            rightBtn.value=right+']';
        })
        leftResetBtn.addEventListener('click', () => {
            left = null;
            leftBtn.value='[';
        })
        rightResetBtn.addEventListener('click', () => {
            right = null;
            rightBtn.value=']';
        })
        video.addEventListener('timeupdate', () => {
            if (!enabled) return;
            let current = video.currentTime;
            if (current >= (right ?? Infinity) || current < left) video.currentTime = left ?? 0;
        })
        video.addEventListener('ended', () => {
            if (!enabled) return;
            video.currentTime = left ?? 0;
            video.play();
        })
    }
    let intv = 0;
    intv = setInterval(() => {
      let root = document.querySelector('#viewbox_report'), video = document.querySelector('video');
      if (root == null || video == null) return;
      install(root, video);
      console.log('install success on', root, video)
      clearInterval(intv);
    }, 1000)

})(unsafeWindow)