使用 MPV 播放

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

目前为 2024-04-22 提交的版本,查看 最新版本

  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 2024.04.22.3
  12. // @author Akatsuki Rui
  13. // @license MIT License
  14. // @require https://cdn.jsdelivr.net/gh/sizzlemctwizzle/GM_config@06f2015c04db3aaab9717298394ca4f025802873/gm_config.js
  15. // @grant GM_info
  16. // @grant GM_getValue
  17. // @grant GM_setValue
  18. // @grant GM_notification
  19. // @run-at document-idle
  20. // @noframes
  21. // @match *://*.youtube.com/*
  22. // @match *://*.twitch.tv/*
  23. // @match *://*.crunchyroll.com/*
  24. // @match *://*.bilibili.com/*
  25. // @match *://*.kick.com/*
  26. // ==/UserScript==
  27.  
  28. "use strict";
  29.  
  30. const MPV_HANDLER_VERSION = "v0.3.8";
  31.  
  32. const MATCHERS = {
  33. "www.youtube.com": /www.youtube.com\/(watch|playlist|shorts)\?/gi,
  34. "m.youtube.com": /m.youtube.com\/(watch|playlist|shorts)\?/gi,
  35. "www.twitch.tv":
  36. /www.twitch.tv\/(?!(directory|downloads|jobs|p|turbo)\/).+/gi,
  37. "clips.twitch.tv": /clips.twitch.tv/gi,
  38. "www.crunchyroll.com": /www.crunchyroll.com\/.*watch\/([0-9]|[A-Z])+/gi,
  39. "beta.crunchyroll.com": /beta.crunchyroll.com\/.*watch\/([0-9]|[A-Z])+/gi,
  40. "live.bilibili.com": /live.bilibili.com\/[0-9]+/gi,
  41. "www.bilibili.com": /www.bilibili.com\/video\/(av|bv)/gi,
  42. "kick.com": /kick.com\/(?!(categories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OTQgMTMuMDE3NTc4IEwgMTQuOTMzNTk0IDkuOTg2MzI4MSBMIDEzLjQxMjEwOSA4\
  74. LjkyOTY4NzUgQSA1LjUgNS41IDAgMCAwIDEzLjQ5NjA5NCA4LjAwMTk1MzEgQSA1LjUgNS41IDAg\
  75. MCAwIDEzLjQxNjAxNiA3LjA3MDMxMjUgTCAxNC45MzM1OTQgNi4wMTc1NzgxIEwgMTMuMTgzNTk0\
  76. IDIuOTg2MzI4MSBMIDExLjUxMTcxOSAzLjc3MzQzNzUgQSA1LjUgNS41IDAgMCAwIDkuOTAwMzkw\
  77. NiAyLjg0OTYwOTQgTCA5Ljc1IDEgTCA2LjI1IDEgeiBNIDggNiBBIDIgMiAwIDAgMSAxMCA4IEEg\
  78. MiAyIDAgMCAxIDggMTAgQSAyIDIgMCAwIDEgNiA4IEEgMiAyIDAgMCAxIDggNiB6IiB0cmFuc2Zv\
  79. cm09InRyYW5zbGF0ZSg0IDQpIi8+Cjwvc3ZnPgo=";
  80.  
  81. const MPV_CSS = `
  82. .pwm-play {
  83. width: 48px;
  84. height: 48px;
  85. border: 0;
  86. border-radius: 50%;
  87. background-size: 48px;
  88. background-image: url(data:image/svg+xml;base64,${ICON_MPV});
  89. background-repeat: no-repeat;
  90. }
  91. .pwm-settings {
  92. opacity: 0;
  93. visibility: hidden;
  94. transition: all 0.2s ease-in-out;
  95. display: block;
  96. position: absolute;
  97. top: -32px;
  98. width: 32px;
  99. height: 32px;
  100. margin-left: 8px;
  101. border: 0;
  102. border-radius: 50%;
  103. background-size: 32px;
  104. background-color: #eeeeee;
  105. background-image: url(data:image/svg+xml;base64,${ICON_SETTINGS});
  106. background-repeat: no-repeat;
  107. }
  108. .play-with-mpv {
  109. z-index: 99999;
  110. position: fixed;
  111. left: 8px;
  112. bottom: 8px;
  113. }
  114. .pwm-play:hover + .pwm-settings,
  115. .pwm-settings:hover {
  116. opacity: 1;
  117. visibility: visible;
  118. transition: all 0.2s ease-in-out;
  119. }
  120. `;
  121.  
  122. const CONFIG_ID = "play-with-mpv";
  123.  
  124. const CONFIG_CSS = `
  125. body {
  126. display: flex;
  127. justify-content: center;
  128. }
  129. #${CONFIG_ID}_wrapper {
  130. display: flex;
  131. flex-direction: column;
  132. justify-content: center;
  133. }
  134. #${CONFIG_ID} .config_header {
  135. display: flex;
  136. align-items: center;
  137. padding: 12px;
  138. }
  139. #${CONFIG_ID} .config_var {
  140. margin: 0 0 12px 0;
  141. }
  142. #${CONFIG_ID} .field_label {
  143. display: inline-block;
  144. width: 140px;
  145. font-size: 14px;
  146. }
  147. #${CONFIG_ID}_field_cookies,
  148. #${CONFIG_ID}_field_profile,
  149. #${CONFIG_ID}_field_quality,
  150. #${CONFIG_ID}_field_v_codec,
  151. #${CONFIG_ID}_field_console {
  152. width: 80px;
  153. height: 24px;
  154. font-size: 14px;
  155. text-align: center;
  156. }
  157. #${CONFIG_ID}_buttons_holder {
  158. display: flex;
  159. flex-direction: column;
  160. }
  161. #${CONFIG_ID} .saveclose_buttons {
  162. margin: 1px;
  163. padding: 4px 0;
  164. }
  165. #${CONFIG_ID} .reset_holder {
  166. padding-top: 4px;
  167. }
  168. `;
  169.  
  170. const CONFIG_IFRAME_CSS = `
  171. position: fixed;
  172. z-index: 99999;
  173. width: 300px;
  174. height: 400px;
  175. border: 1px solid;
  176. border-radius: 10px;
  177. `;
  178.  
  179. const CONFIG_FIELDS = {
  180. cookies: {
  181. label: "Try Pass Cookies",
  182. type: "select",
  183. options: ["yes", "no"],
  184. default: "no",
  185. },
  186. profile: {
  187. label: "MPV Profile",
  188. type: "text",
  189. default: "default",
  190. },
  191. quality: {
  192. label: "Prefer Video Quality",
  193. type: "select",
  194. options: ["default", "2160p", "1440p", "1080p", "720p", "480p", "360p"],
  195. default: "default",
  196. },
  197. v_codec: {
  198. label: "Prefer Video Codec",
  199. type: "select",
  200. options: ["default", "av01", "vp9", "h265", "h264"],
  201. default: "default",
  202. },
  203. console: {
  204. label: "Run With Console",
  205. type: "select",
  206. options: ["yes", "no"],
  207. default: "yes",
  208. },
  209. };
  210.  
  211. // GM_config init
  212. GM_config.init({
  213. id: CONFIG_ID,
  214. title: GM_info.script.name,
  215. fields: CONFIG_FIELDS,
  216. events: {
  217. init: () => {
  218. let quality = GM_config.get("quality").toLowerCase();
  219. let v_codec = GM_config.get("v_codec").toLowerCase();
  220.  
  221. if (!CONFIG_FIELDS.quality.options.includes(quality)) {
  222. GM_config.set("quality", "default");
  223. }
  224. if (!CONFIG_FIELDS.v_codec.options.includes(v_codec)) {
  225. GM_config.set("v_codec", "default");
  226. }
  227. },
  228. save: () => {
  229. let profile = GM_config.get("profile").trim();
  230.  
  231. if (profile === "") {
  232. GM_config.set("profile", "default");
  233. } else {
  234. GM_config.set("profile", profile);
  235. }
  236.  
  237. updateButton(location.href);
  238. GM_config.close();
  239. },
  240. reset: () => {
  241. GM_config.save();
  242. },
  243. },
  244. css: CONFIG_CSS.trim(),
  245. });
  246.  
  247. // URL-safe base64 encode
  248. function btoaUrl(url) {
  249. return btoa(url).replace(/\//g, "_").replace(/\+/g, "-").replace(/\=/g, "");
  250. }
  251.  
  252. // Generate protocol
  253. function generateProto(url) {
  254. let cookies = GM_config.get("cookies").toLowerCase();
  255. let profile = GM_config.get("profile").trim();
  256. let quality = GM_config.get("quality").toLowerCase();
  257. let v_codec = GM_config.get("v_codec").toLowerCase();
  258. let console = GM_config.get("console").toLowerCase();
  259. let options = [];
  260.  
  261. let proto;
  262.  
  263. if (console === "yes") {
  264. proto = "mpv-debug://play/" + btoaUrl(url);
  265. } else {
  266. proto = "mpv://play/" + btoaUrl(url);
  267. }
  268. if (cookies === "yes") {
  269. options.push("cookies=" + document.location.hostname + ".txt");
  270. }
  271. if (profile !== "default" && profile !== "") {
  272. options.push("profile=" + profile);
  273. }
  274. if (quality !== "default") {
  275. options.push("quality=" + quality);
  276. }
  277. if (v_codec !== "default") {
  278. options.push("v_codec=" + v_codec);
  279. }
  280.  
  281. if (options.length !== 0) {
  282. proto += "/?";
  283.  
  284. options.forEach((option, index) => {
  285. proto += option;
  286.  
  287. if (index + 1 !== options.length) {
  288. proto += "&";
  289. }
  290. });
  291. }
  292.  
  293. return proto;
  294. }
  295.  
  296. // Check the URL is matched or not
  297. function matchUrl(url) {
  298. if (MATCHERS[location.hostname]) {
  299. return url.search(MATCHERS[location.hostname]) !== -1;
  300. } else {
  301. return false;
  302. }
  303. }
  304.  
  305. // Update button display status and URL
  306. function updateButton(url) {
  307. let isMatch = matchUrl(url);
  308. let button = document.getElementsByClassName("pwm-play")[0];
  309.  
  310. if (button) {
  311. button.style =
  312. isMatch && !document.fullscreenElement
  313. ? "display: block"
  314. : "display: none";
  315. button.href = isMatch ? generateProto(url) : "";
  316. }
  317. }
  318.  
  319. // Notify update about mpv-handler
  320. function notifyUpdate() {
  321. let version = GM_getValue("mpvHandlerVersion", null);
  322.  
  323. if (version !== MPV_HANDLER_VERSION) {
  324. const UPDATE_NOTIFY = {
  325. title: `${GM_info.script.name}`,
  326. text: `mpv-handler is upgraded to ${MPV_HANDLER_VERSION}\n\nClick to check updates`,
  327. onclick: () => {
  328. window.open("https://github.com/akiirui/mpv-handler/releases/latest");
  329. },
  330. };
  331.  
  332. GM_notification(UPDATE_NOTIFY);
  333. GM_setValue("mpvHandlerVersion", MPV_HANDLER_VERSION);
  334. }
  335. }
  336.  
  337. // Add play and settings buttons to page
  338. function createButton() {
  339. let head = document.getElementsByTagName("head")[0];
  340. let style = document.createElement("style");
  341.  
  342. if (head) {
  343. style.innerHTML = MPV_CSS.trim();
  344. head.appendChild(style);
  345. }
  346.  
  347. let body = document.body;
  348. let buttonDiv = document.createElement("div");
  349. let buttonPlay = document.createElement("a");
  350. let buttonSettings = document.createElement("button");
  351.  
  352. let pauseVideo = (e) => {
  353. let videoElement = document.getElementsByTagName("video")[0];
  354. if (videoElement) {
  355. videoElement.pause();
  356. } else {
  357. setTimeout(pauseVideo, 500, e);
  358. }
  359. if (e.stopPropagation) e.stopPropagation();
  360. };
  361.  
  362. if (body) {
  363. buttonPlay.className = "pwm-play";
  364. buttonPlay.style = "display: none";
  365. buttonPlay.target = "_blank";
  366. buttonPlay.addEventListener("click", pauseVideo);
  367.  
  368. buttonSettings.className = "pwm-settings";
  369. buttonSettings.addEventListener("click", () => {
  370. if (!GM_config.isOpen) {
  371. GM_config.open();
  372. GM_config.frame.style = CONFIG_IFRAME_CSS.trim();
  373. }
  374. });
  375.  
  376. buttonDiv.className = "play-with-mpv";
  377. buttonDiv.appendChild(buttonPlay);
  378. buttonDiv.appendChild(buttonSettings);
  379.  
  380. body.appendChild(buttonDiv);
  381.  
  382. document.addEventListener("fullscreenchange", () => {
  383. let button = document.getElementsByClassName("pwm-play")[0];
  384.  
  385. button.style = document.fullscreenElement
  386. ? "display: none"
  387. : "display: block";
  388. });
  389. }
  390. }
  391.  
  392. // Detect PJAX changes
  393. function detectPJAX() {
  394. let previousUrl = null;
  395. let currentUrl = null;
  396.  
  397. setInterval(() => {
  398. currentUrl = location.href;
  399.  
  400. if (previousUrl !== currentUrl) {
  401. updateButton(currentUrl);
  402. previousUrl = currentUrl;
  403. }
  404. }, 500);
  405. }
  406.  
  407. notifyUpdate();
  408. createButton();
  409. detectPJAX();