Greasy Fork

Greasy Fork is available in English.

Netflix Mouse Control

Volume/Time mouse control for Netflix

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

您需要先安装一款用户脚本管理器扩展,例如 Tampermonkey 篡改猴Greasemonkey 油猴子Violentmonkey 暴力猴,才能安装此脚本。

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

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

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

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

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

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

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

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

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

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

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

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

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

// ==UserScript==
// @name         Netflix Mouse Control
// @namespace    PoKeRGT
// @version      1.02
// @description  Volume/Time mouse control for Netflix
// @author       PoKeRGT
// @match        https://www.netflix.com/watch/*
// @icon         https://www.google.com/s2/favicons?sz=64&domain=netflix.com
// @grant        none
// @run-at       document-ready
// @homepageURL  https://github.com/PoKeRGT/userscripts
// @license      MIT
// ==/UserScript==

setTimeout(() => {
  const getPlayer = () => {
    const nApi = netflix.appContext.state.playerApp.getAPI();
    const videoPlayer = nApi.videoPlayer;
    if (videoPlayer && videoPlayer.getVideoPlayerBySessionId && videoPlayer.getAllPlayerSessionIds) {
      const allSessionIds = videoPlayer.getAllPlayerSessionIds();
      const watchSessionIds = allSessionIds.filter(sid => sid.startsWith('watch-'));
      if (watchSessionIds.length > 0) {
        return videoPlayer.getVideoPlayerBySessionId(watchSessionIds[0]);
      } else if (allSessionIds.length > 0) {
        return videoPlayer.getVideoPlayerBySessionId(allSessionIds[0]);
      }
    }
    return null;
  }

  const player = getPlayer();
  // console.log(player);


  const getVideoTag = () => {
    const videos = document.getElementsByTagName('video');
    return (videos && videos.length === 1 ? videos[0] : null);
  }

  let lastExpectedTimeMillis = null;
  const moveToPosition = (timeMillis) => {
    player.seek(timeMillis);
    lastExpectedTimeMillis = timeMillis;
  }

  const limitRange = (minValue, maxValue, n) => {
    return Math.max(minValue, Math.min(maxValue, n));
  }

  document.body.addEventListener('wheel', (event) => {
    var videoTag = getVideoTag();
    const rect = videoTag.getBoundingClientRect();
    const x = event.clientX - rect.left; //x position within the element.
    const y = event.clientY - rect.top; //y position within the element.
    // console.log("Left? : " + x + " ; Top? : " + y + ".");
    const width = videoTag.offsetWidth;
    const height = videoTag.offsetHeight;
    if ((width / 2) > x) {
      // console.log('isLeft');
      if (event.deltaY < 0) {
        const currVolume = player.getVolume();
        player.setVolume(limitRange(0, 1, currVolume + 0.1))
      } else {
        const currVolume = player.getVolume();
        player.setVolume(limitRange(0, 1, currVolume - 0.1))
      }
    } else {
      // console.log('isRight');
      if (event.deltaY < 0) {
        const newPosition = player.getCurrentTime() + 10000.0;
        moveToPosition(limitRange(0, player.getDuration(), newPosition));
      } else {
        const newPosition = player.getCurrentTime() - 10000.0;
        moveToPosition(limitRange(0, player.getDuration(), newPosition));
      }
    }
    event.preventDefault();
  }, { passive: false });
}, 7000);