使用 mpv 播放

通过 mpv 和 youtube-dl 播放网页上的视频和歌曲

当前为 2020-11-19 提交的版本,查看 最新版本

  1. // ==UserScript==
  2. // @name Play with mpv
  3. // @name:en-US Play with mpv
  4. // @name:zh-CN 使用 mpv 播放
  5. // @name:zh-TW 使用 mpv 播放
  6. // @description Play website videos and songs with mpv & youtube-dl
  7. // @description:en-US Play website videos and songs with mpv & youtube-dl
  8. // @description:zh-CN 通过 mpv 和 youtube-dl 播放网页上的视频和歌曲
  9. // @description:zh-TW 通過 mpv 和 youtube-dl 播放網頁上的視頻和歌曲
  10. // @namespace play-with-mpv-handler
  11. // @version 2020.11.19
  12. // @author Akatsuki Rui
  13. // @license MIT License
  14. // @grant GM_info
  15. // @run-at document-idle
  16. // @noframes
  17. // @match *://www.youtube.com/*
  18. // @match *://www.bilibili.com/video/*
  19. // ==/UserScript==
  20.  
  21. "use strict";
  22.  
  23. const MATCH_URLS = ["www.youtube.com/watch", "www.bilibili.com/video/"];
  24.  
  25. function appendButton() {
  26. let head = document.getElementsByTagName("head")[0];
  27. let body = document.getElementsByTagName("body")[0];
  28. let style = document.createElement("style");
  29. let buttonIframe = document.createElement("iframe");
  30. let button = document.createElement("a");
  31.  
  32. if (head) {
  33. style.innerHTML =
  34. ".play-with-mpv{position:fixed;left:12px;bottom:12px;width:48px;height:48px;border:0;border-radius:50%;background-size:100%;background-image:url(data:image/svg+xml;base64,PHN2ZyB4bWxucz0iaHR0cDovL3d3dy53My5vcmcvMjAwMC9zdmciIHdpZHRoPSI2NCIgaGVpZ2h0PSI2NCIgdmVyc2lvbj0iMSI+CiA8Y2lyY2xlIHN0eWxlPSJvcGFjaXR5Oi4yIiBjeD0iMzIiIGN5PSIzMyIgcj0iMjgiLz4KIDxjaXJjbGUgc3R5bGU9ImZpbGw6IzhkMzQ4ZSIgY3g9IjMyIiBjeT0iMzIiIHI9IjI4Ii8+CiA8Y2lyY2xlIHN0eWxlPSJvcGFjaXR5Oi4zIiBjeD0iMzQuNSIgY3k9IjI5LjUiIHI9IjIwLjUiLz4KIDxjaXJjbGUgc3R5bGU9Im9wYWNpdHk6LjIiIGN4PSIzMiIgY3k9IjMzIiByPSIxNCIvPgogPGNpcmNsZSBzdHlsZT0iZmlsbDojZmZmZmZmIiBjeD0iMzIiIGN5PSIzMiIgcj0iMTQiLz4KIDxwYXRoIHN0eWxlPSJmaWxsOiM2OTFmNjkiIHRyYW5zZm9ybT0ibWF0cml4KDEuNTE1NTQ0NSwwLDAsMS41LC0zLjY1Mzg3OSwtNC45ODczODQ4KSIgZD0ibTI3LjE1NDUxNyAyNC42NTgyNTctMy40NjQxMDEgMi0zLjQ2NDEwMiAxLjk5OTk5OXYtNC0zLjk5OTk5OWwzLjQ2NDEwMiAyeiIvPgogPHBhdGggc3R5bGU9ImZpbGw6I2ZmZmZmZjtvcGFjaXR5Oi4xIiBkPSJNIDMyIDQgQSAyOCAyOCAwIDAgMCA0IDMyIEEgMjggMjggMCAwIDAgNC4wMjE0ODQ0IDMyLjU4NTkzOCBBIDI4IDI4IDAgMCAxIDMyIDUgQSAyOCAyOCAwIDAgMSA1OS45Nzg1MTYgMzIuNDE0MDYyIEEgMjggMjggMCAwIDAgNjAgMzIgQSAyOCAyOCAwIDAgMCAzMiA0IHoiLz4KPC9zdmc+Cgo=);background-repeat:no-repeat}";
  35. head.appendChild(style);
  36. }
  37.  
  38. if (body) {
  39. buttonIframe.name = "play-with-mpv";
  40. buttonIframe.style = "display: none";
  41. body.appendChild(buttonIframe);
  42.  
  43. button.className = "play-with-mpv";
  44. button.style = "display: none";
  45. button.target = "play-with-mpv";
  46. button.addEventListener("click", () => {
  47. let videoElement = document.getElementsByTagName("video")[0];
  48.  
  49. videoElement.paused ? null : videoElement.pause();
  50. });
  51. body.appendChild(button);
  52.  
  53. changeButton(location.href);
  54. }
  55. }
  56.  
  57. function changeButton(currentUrl) {
  58. let isMatch = false;
  59. let button = document.getElementsByClassName("play-with-mpv")[0];
  60.  
  61. for (const element of MATCH_URLS) {
  62. if ((isMatch = currentUrl.includes(element))) break;
  63. }
  64.  
  65. if (button) {
  66. button.style = isMatch ? "display: inline-block" : "display: none";
  67. button.href = isMatch ? "mpv://" + btoa(currentUrl) : "";
  68. }
  69. }
  70.  
  71. function detectPJAX() {
  72. let previousUrl = null;
  73.  
  74. setInterval(() => {
  75. let currentUrl = location.href;
  76.  
  77. if (currentUrl && previousUrl !== currentUrl) {
  78. changeButton(currentUrl);
  79. previousUrl = currentUrl;
  80. }
  81. }, 500);
  82. }
  83.  
  84. appendButton();
  85. detectPJAX();

QingJ © 2025

镜像随时可能失效,请加Q群300939539或关注我们的公众号极客氢云获取最新地址