Region loop

2024/5/15 14:39:50

θα χρειαστεί να εγκαταστήσετε μια επέκταση όπως το Tampermonkey, το Greasemonkey ή το Violentmonkey για να εγκαταστήσετε αυτόν τον κώδικα.

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

θα χρειαστεί να εγκαταστήσετε μια επέκταση όπως το Tampermonkey ή το Violentmonkey για να εγκαταστήσετε αυτόν τον κώδικα.

θα χρειαστεί να εγκαταστήσετε μια επέκταση όπως το Tampermonkey ή το Userscripts για να εγκαταστήσετε αυτόν τον κώδικα.

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

Θα χρειαστεί να εγκαταστήσετε μια επέκταση διαχείρισης κώδικα χρήστη για να εγκαταστήσετε αυτόν τον κώδικα.

(Έχω ήδη έναν διαχειριστή κώδικα χρήστη, επιτρέψτε μου να τον εγκαταστήσω!)

You will need to install an extension such as Stylus to install this style.

You will need to install an extension such as Stylus to install this style.

You will need to install an extension such as Stylus to install this style.

You will need to install a user style manager extension to install this style.

You will need to install a user style manager extension to install this style.

You will need to install a user style manager extension to install this style.

(Έχω ήδη έναν διαχειριστή στυλ χρήστη, επιτρέψτε μου να τον εγκαταστήσω!)

// ==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)