使用 MPV 播放

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

目前为 2022-11-11 提交的版本,查看 最新版本

  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 videos and songs on the website via mpv-handler
  7. // @description:en-US Play videos and songs on the website via mpv-handler
  8. // @description:zh-CN 通过 mpv-handler 播放网页上的视频和歌曲
  9. // @description:zh-TW 通過 mpv-handler 播放網頁上的視頻和歌曲
  10. // @namespace play-with-mpv-handler
  11. // @version 2022.11.11.2
  12. // @author Akatsuki Rui
  13. // @license MIT License
  14. // @require https://cdn.jsdelivr.net/gh/sizzlemctwizzle/GM_config@a4a49b47ecfb1d8fcd27049cc0e8114d05522a0f/gm_config.js
  15. // @grant GM_info
  16. // @grant GM_getValue
  17. // @grant GM_setValue
  18. // @grant GM_notification
  19. // @grant GM_openInTab
  20. // @run-at document-idle
  21. // @noframes
  22. // @match *://clips.twitch.tv/*
  23. // @match *://live.bilibili.com/*
  24. // @match *://m.youtube.com/*
  25. // @match *://www.bilibili.com/video/*
  26. // @match *://www.crunchyroll.com/*
  27. // @match *://www.twitch.tv/*
  28. // @match *://www.youtube.com/*
  29. // ==/UserScript==
  30.  
  31. "use strict";
  32.  
  33. const MPV_HANDLER_VERSION = "v0.3.0";
  34.  
  35. const MATCHERS = {
  36. "clips.twitch.tv": /clips.twitch.tv/gi,
  37. "live.bilibili.com": /live.bilibili.com\/[0-9]+/gi,
  38. "m.youtube.com": /m.youtube.com\/(watch|playlist|shorts)\?/gi,
  39. "www.bilibili.com": /www.bilibili.com\/video\/(av|bv)/gi,
  40. "www.crunchyroll.com": /www.crunchyroll.com\/watch\/([0-9]|[A-Z])+\//gi,
  41. "www.twitch.tv":
  42. /www.twitch.tv\/(?!(directory|downloads|jobs|p|turbo)\/).+/gi,
  43. "www.youtube.com": /www.youtube.com\/(watch|playlist|shorts)\?/gi,
  44. };
  45.  
  46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QuOTMzNTk0IDkuOTg2MzI4MSBMIDEzLjQxMjEwOSA4\
  75. LjkyOTY4NzUgQSA1LjUgNS41IDAgMCAwIDEzLjQ5NjA5NCA4LjAwMTk1MzEgQSA1LjUgNS41IDAg\
  76. MCAwIDEzLjQxNjAxNiA3LjA3MDMxMjUgTCAxNC45MzM1OTQgNi4wMTc1NzgxIEwgMTMuMTgzNTk0\
  77. IDIuOTg2MzI4MSBMIDExLjUxMTcxOSAzLjc3MzQzNzUgQSA1LjUgNS41IDAgMCAwIDkuOTAwMzkw\
  78. NiAyLjg0OTYwOTQgTCA5Ljc1IDEgTCA2LjI1IDEgeiBNIDggNiBBIDIgMiAwIDAgMSAxMCA4IEEg\
  79. MiAyIDAgMCAxIDggMTAgQSAyIDIgMCAwIDEgNiA4IEEgMiAyIDAgMCAxIDggNiB6IiB0cmFuc2Zv\
  80. cm09InRyYW5zbGF0ZSg0IDQpIi8+Cjwvc3ZnPgo=";
  81.  
  82. const MPV_CSS = `
  83. .pwm-play {
  84. width: 48px;
  85. height: 48px;
  86. border: 0;
  87. border-radius: 50%;
  88. background-size: 48px;
  89. background-image: url(data:image/svg+xml;base64,${ICON_MPV});
  90. background-repeat: no-repeat;
  91. }
  92. .pwm-settings {
  93. opacity: 0;
  94. visibility: hidden;
  95. transition: all 0.2s ease-in-out;
  96. display: block;
  97. position: absolute;
  98. top: -32px;
  99. width: 32px;
  100. height: 32px;
  101. margin-left: 8px;
  102. border: 0;
  103. border-radius: 50%;
  104. background-size: 32px;
  105. background-color: #eeeeee;
  106. background-image: url(data:image/svg+xml;base64,${ICON_SETTINGS});
  107. background-repeat: no-repeat;
  108. }
  109. .play-with-mpv {
  110. z-index: 99999;
  111. position: fixed;
  112. left: 8px;
  113. bottom: 8px;
  114. }
  115. .pwm-play:hover + .pwm-settings,
  116. .pwm-settings:hover {
  117. opacity: 1;
  118. visibility: visible;
  119. transition: all 0.2s ease-in-out;
  120. }
  121. `;
  122.  
  123. const CONFIG_ID = "play-with-mpv";
  124.  
  125. const CONFIG_CSS = `
  126. body {
  127. display: flex;
  128. }
  129. #${CONFIG_ID}_wrapper {
  130. margin: auto;
  131. }
  132. #${CONFIG_ID} .config_header {
  133. padding-bottom: 8px;
  134. }
  135. #${CONFIG_ID}_field_perferQuality {
  136. padding-top: 4px;
  137. padding-bottom: 8px;
  138. }
  139. #${CONFIG_ID} .saveclose_buttons {
  140. margin: 1px;
  141. padding: 4px 16px;
  142. }
  143. #${CONFIG_ID} .reset_holder {
  144. padding-top: 4px;
  145. }
  146. `;
  147.  
  148. const CONFIG_IFRAME_CSS = `
  149. position: fixed;
  150. z-index: 999;
  151. width: 440px;
  152. height: 240px;
  153. border: 1px solid;
  154. border-radius: 2px;
  155. `;
  156.  
  157. GM_config.init({
  158. id: `${CONFIG_ID}`,
  159. title: `${GM_info.script.name}`,
  160. fields: {
  161. perferQuality: {
  162. label: "Prefer Quality",
  163. type: "radio",
  164. options: ["Best", "2160p", "1440p", "1080p", "720p", "480p", "360p"],
  165. default: "Best",
  166. },
  167. useCookies: {
  168. label: "Try Pass Cookies",
  169. type: "radio",
  170. options: ["Yes", "No"],
  171. default: "No",
  172. },
  173. },
  174. events: {
  175. save: () => {
  176. updateButton(location.href);
  177. GM_config.close();
  178. },
  179. reset: () => {
  180. updateButton(location.href);
  181. GM_config.save();
  182. GM_config.close();
  183. },
  184. },
  185. css: CONFIG_CSS.trim(),
  186. });
  187.  
  188. function notifyUpdate() {
  189. let version = GM_getValue("mpvHandlerVersion", null);
  190.  
  191. if (version !== MPV_HANDLER_VERSION) {
  192. const UPDATE_NOTIFY = {
  193. title: `${GM_info.script.name}`,
  194. text: `mpv-handler is upgraded to ${MPV_HANDLER_VERSION}\n\nClick to check updates`,
  195. onclick: () => {
  196. GM_openInTab("https://github.com/akiirui/mpv-handler/releases/latest");
  197. },
  198. };
  199.  
  200. GM_notification(UPDATE_NOTIFY);
  201. GM_setValue("mpvHandlerVersion", MPV_HANDLER_VERSION);
  202. }
  203. }
  204.  
  205. function matchUrl(currentUrl) {
  206. if (MATCHERS[location.hostname]) {
  207. return currentUrl.search(MATCHERS[location.hostname]) !== -1;
  208. } else {
  209. return false;
  210. }
  211. }
  212.  
  213. function appendButton() {
  214. let head = document.getElementsByTagName("head")[0];
  215. let style = document.createElement("style");
  216.  
  217. if (head) {
  218. style.innerHTML = MPV_CSS.trim();
  219. head.appendChild(style);
  220. }
  221.  
  222. let body = document.body;
  223. let buttonDiv = document.createElement("div");
  224. let buttonPlay = document.createElement("a");
  225. let buttonSettings = document.createElement("button");
  226.  
  227. if (body) {
  228. buttonPlay.className = "pwm-play";
  229. buttonPlay.style = "display: none";
  230. buttonPlay.target = "_blank";
  231. buttonPlay.addEventListener("click", (e) => {
  232. let videoElement = document.getElementsByTagName("video")[0];
  233. if (videoElement) videoElement.pause();
  234. if (e.stopPropagation) e.stopPropagation();
  235. });
  236.  
  237. buttonSettings.className = "pwm-settings";
  238. buttonSettings.addEventListener("click", () => {
  239. if (!GM_config.isOpen) {
  240. GM_config.open();
  241. GM_config.frame.style = CONFIG_IFRAME_CSS.trim();
  242. }
  243. });
  244.  
  245. buttonDiv.className = "play-with-mpv";
  246. buttonDiv.appendChild(buttonPlay);
  247. buttonDiv.appendChild(buttonSettings);
  248.  
  249. body.appendChild(buttonDiv);
  250.  
  251. document.addEventListener("fullscreenchange", () => {
  252. let button = document.getElementsByClassName("pwm-play")[0];
  253.  
  254. button.style = document.fullscreenElement
  255. ? "display: none"
  256. : "display: block";
  257. });
  258. }
  259. }
  260.  
  261. function updateButton(currentUrl) {
  262. let isMatch = matchUrl(currentUrl);
  263. let button = document.getElementsByClassName("pwm-play")[0];
  264.  
  265. if (button) {
  266. let quality = GM_config.get("perferQuality").toLowerCase();
  267. let cookies = GM_config.get("useCookies").toLowerCase();
  268. let options = [];
  269.  
  270. let proto =
  271. "mpv://play/" + btoa(currentUrl).replace(/\//g, "_").replace(/\+/g, "-");
  272.  
  273. if (cookies === "yes") {
  274. options.push("cookies=" + document.location.hostname + ".txt");
  275. }
  276. if (quality !== "best") {
  277. options.push("quality=" + quality);
  278. }
  279.  
  280. if (options.length !== 0) {
  281. proto += "/?";
  282.  
  283. options.forEach((option, index) => {
  284. proto += option;
  285.  
  286. if (index + 1 !== options.length) {
  287. proto += "&";
  288. }
  289. });
  290. }
  291.  
  292. button.style = isMatch ? "display: block" : "display: none";
  293. button.href = isMatch ? proto : "";
  294. }
  295. }
  296.  
  297. function detectPJAX() {
  298. let previousUrl = null;
  299. let currentUrl = null;
  300.  
  301. setInterval(() => {
  302. currentUrl = location.href;
  303.  
  304. if (previousUrl !== currentUrl) {
  305. updateButton(currentUrl);
  306. previousUrl = currentUrl;
  307. }
  308. }, 500);
  309. }
  310.  
  311. notifyUpdate();
  312. appendButton();
  313. detectPJAX();