skip audio intro

a helper for skiping audio intro

您需要先安裝使用者腳本管理器擴展,如 TampermonkeyGreasemonkeyViolentmonkey 之後才能安裝該腳本。

您需要先安裝使用者腳本管理器擴充功能,如 TampermonkeyViolentmonkey 後才能安裝該腳本。

您需要先安裝使用者腳本管理器擴充功能,如 TampermonkeyViolentmonkey 後才能安裝該腳本。

您需要先安裝使用者腳本管理器擴充功能,如 TampermonkeyUserscripts 後才能安裝該腳本。

你需要先安裝一款使用者腳本管理器擴展,比如 Tampermonkey,才能安裝此腳本

您需要先安裝使用者腳本管理器擴充功能後才能安裝該腳本。

(我已經安裝了使用者腳本管理器,讓我安裝!)

你需要先安裝一款使用者樣式管理器擴展,比如 Stylus,才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展,比如 Stylus,才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展,比如 Stylus,才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展後才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展後才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展後才能安裝此樣式

(我已經安裝了使用者樣式管理器,讓我安裝!)

// ==UserScript==
// @name        skip audio intro
// @name:zh-CN  音频片头和片尾跳过
// @namespace   https://github.com/22earth
// @description a helper for skiping audio intro
// @description:zh-cn 跳过音频片头和片尾
// @author      22earth
// @include     https://www.ximalaya.com/*
// @version     0.0.1
// @grant       GM_registerMenuCommand
// @grant       GM_setValue
// @grant       GM_getValue
// @run-at      document-start
// @license     MIT
// ==/UserScript==


const SKIP_START_CONFIG = 'e_user_skip_start_config';
const SKIP_END_CONFIG = 'e_user_skip_end_config';
function setStart() {
    var start = +prompt('设置跳过片头秒数', '20');
    skipStartSec = start;
    GM_setValue(SKIP_START_CONFIG, start);
}
function setEnd() {
    var sec = +prompt('设置跳过片尾秒数', '10');
    skipEndSec = +sec;
    GM_setValue(SKIP_END_CONFIG, sec);
}
if (GM_registerMenuCommand) {
    GM_registerMenuCommand('设置跳过片头秒数', setStart);
    GM_registerMenuCommand('设置跳过片尾秒数', setEnd);
}
let skipStartSec = GM_getValue(SKIP_START_CONFIG, 20);
let skipEndSec = GM_getValue(SKIP_END_CONFIG, 10);
function setAudioEvents(audio) {
    audio.addEventListener('canplaythrough', function () {
        if (this.currentTime < skipStartSec) {
            this.currentTime = skipStartSec;
        }
    });
    audio.addEventListener('timeupdate', function () {
        if (this.currentTime >= this.duration) {
            return;
        }
        if (this.currentTime + skipEndSec > this.duration) {
            this.currentTime = this.duration;
        }
    });
}
(function (window) {
    function hookAudioContructor() {
        var fakeObj = new Proxy(window.Audio, {
            get: function (target, p) {
                return Reflect.get(target, p);
            },
            construct(target, args) {
                let inst = new target(...args);
                setAudioEvents(inst);
                return inst;
            },
        });
        return fakeObj;
    }
    function setDescriptor(fakeObj, prop) {
        Object.defineProperty(window, prop, {
            get: function () {
                return fakeObj;
            },
            set: function (v) {
                console.log(`检测到修改 ${prop}`);
            },
        });
    }
    setDescriptor(hookAudioContructor(), 'Audio');
})(unsafeWindow);