小埋社区

    • 登录
    • 版块
    • 最新
    • 标签
    • 热门
    • 用户
    • 群组

    写了个跳OP脚本

    编程
    2
    4
    299
    正在加载更多帖子
    • 从旧到新
    • 从新到旧
    • 最多赞同
    回复
    • 在新帖中回复
    登录后回复
    此主题已被删除。只有拥有主题管理权限的用户可以查看。
    • Bruce
      Bruce ACG 最后由 编辑

      直接复制进油猴就能用,按住Ctrl+→向后跳转88秒

      // ==UserScript==
      // @name         video forward 88s
      // @namespace    http://tampermonkey.net/
      // @version      0.1
      // @description  Press Ctrl+Right to go ahead 88 s.
      // @author       Bruce Kang
      // @match        *://*
      // @include      *
      // @grant        none
      // ==/UserScript==
      Object.defineProperty(HTMLMediaElement.prototype, 'playing', {
          get: function(){
           return !!(this.currentTime > 0 && !this.paused && !this.ended && this.readyState > 2);
          }
      })
      var video=document.getElementsByTagName("video");
      (function() {
          function keyDown(e){
              if (e.keyCode == 39 && e.ctrlKey){
                  for(var i=0;i<video.length;i++){
                      if(video[i].playing){
                          var vCurrentTime = video[i].currentTime;
                          video[i].currentTime = vCurrentTime +88;
                          }
                      }
                  }
          }
          document.onkeydown = keyDown;
          'use strict';
      
      })();
      

      问题:

      1. 为什么是88s而不是90s?
        给判断OP出现和手放到键盘上操作预留2s时间
      2. 支持flash视频吗?
        不知道,没试过,也没打算支持。目前html5视频是没有问题的
      3. 按→的时候网站视频自身也会触发向后跳转
        各家网站跳转时间不一样,没法很方便的在脚本中减去这段时间。以后有时间的话可能会改进

      解析:
      js里没有直接定义播放状态,StackOverflow上的老哥说可以把同时满足当前时间>0,未暂停等条件的状态定义为playing。

      Object.defineProperty(HTMLMediaElement.prototype, 'playing', {
          get: function(){
           return !!(this.currentTime > 0 && !this.paused && !this.ended && this.readyState > 2);
          }
      })
      

      之后,定义一个视频数组var video=document.getElementsByTagName("video");,定义一个函数function keyDown(e){},当按键时触发。
      keyDown(e)判断当e为→(ASCII=39)和Ctrl的时候,

      if (e.keyCode == 39 && e.ctrlKey)
      

      开始执行以下功能:
      对于视频数组进行遍历,找到其中正在播放的视频,将其当前时间+88s

      for(var i=0;i<video.length;i++){
          if(video[i].playing){
              var vCurrentTime = video[i].currentTime;
              video[i].currentTime = vCurrentTime +88;
              }
          }
      
      1 条回复 最后回复 回复 引用 0
      • 闲淡酱
        闲淡酱 站长 最后由 编辑

        还有个问题就是,跳了op之后快进咋整

        Bruce 1 条回复 最后回复 回复 引用 0
        • Bruce
          Bruce ACG @闲淡酱 最后由 编辑

          @闲淡酱 ???什么原理???

          闲淡酱 1 条回复 最后回复 回复 引用 1
          • 闲淡酱
            闲淡酱 站长 @Bruce 最后由 编辑

            @Bruce 看漏了个Ctrl😅

            1 条回复 最后回复 回复 引用 0
            • First post
              Last post
            © 2017-2023 小埋社区 All Rights Reserved | 皖ICP备17016228号-2