skip audio intro

a helper for skiping audio intro

  1. // ==UserScript==
  2. // @name skip audio intro
  3. // @name:zh-CN 音频片头和片尾跳过
  4. // @namespace https://github.com/22earth
  5. // @description a helper for skiping audio intro
  6. // @description:zh-cn 跳过音频片头和片尾
  7. // @author 22earth
  8. // @include https://www.ximalaya.com/*
  9. // @version 0.0.1
  10. // @grant GM_registerMenuCommand
  11. // @grant GM_setValue
  12. // @grant GM_getValue
  13. // @run-at document-start
  14. // @license MIT
  15. // ==/UserScript==
  16.  
  17.  
  18. const SKIP_START_CONFIG = 'e_user_skip_start_config';
  19. const SKIP_END_CONFIG = 'e_user_skip_end_config';
  20. function setStart() {
  21. var start = +prompt('设置跳过片头秒数', '20');
  22. skipStartSec = start;
  23. GM_setValue(SKIP_START_CONFIG, start);
  24. }
  25. function setEnd() {
  26. var sec = +prompt('设置跳过片尾秒数', '10');
  27. skipEndSec = +sec;
  28. GM_setValue(SKIP_END_CONFIG, sec);
  29. }
  30. if (GM_registerMenuCommand) {
  31. GM_registerMenuCommand('设置跳过片头秒数', setStart);
  32. GM_registerMenuCommand('设置跳过片尾秒数', setEnd);
  33. }
  34. let skipStartSec = GM_getValue(SKIP_START_CONFIG, 20);
  35. let skipEndSec = GM_getValue(SKIP_END_CONFIG, 10);
  36. function setAudioEvents(audio) {
  37. audio.addEventListener('canplaythrough', function () {
  38. if (this.currentTime < skipStartSec) {
  39. this.currentTime = skipStartSec;
  40. }
  41. });
  42. audio.addEventListener('timeupdate', function () {
  43. if (this.currentTime >= this.duration) {
  44. return;
  45. }
  46. if (this.currentTime + skipEndSec > this.duration) {
  47. this.currentTime = this.duration;
  48. }
  49. });
  50. }
  51. (function (window) {
  52. function hookAudioContructor() {
  53. var fakeObj = new Proxy(window.Audio, {
  54. get: function (target, p) {
  55. return Reflect.get(target, p);
  56. },
  57. construct(target, args) {
  58. let inst = new target(...args);
  59. setAudioEvents(inst);
  60. return inst;
  61. },
  62. });
  63. return fakeObj;
  64. }
  65. function setDescriptor(fakeObj, prop) {
  66. Object.defineProperty(window, prop, {
  67. get: function () {
  68. return fakeObj;
  69. },
  70. set: function (v) {
  71. console.log(`检测到修改 ${prop}`);
  72. },
  73. });
  74. }
  75. setDescriptor(hookAudioContructor(), 'Audio');
  76. })(unsafeWindow);