使用 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
  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.6";
  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\/(?!(categ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1OTQgMTMuMDE3NTc4IEwgMTQuOTMzNTk0IDkuOTg2MzI4MSBMIDEzLjQxMjEwOSA4\
  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. width: 80px;
  152. height: 24px;
  153. font-size: 14px;
  154. text-align: center;
  155. }
  156. #${CONFIG_ID}_buttons_holder {
  157. display: flex;
  158. flex-direction: column;
  159. }
  160. #${CONFIG_ID} .saveclose_buttons {
  161. margin: 1px;
  162. padding: 4px 0;
  163. }
  164. #${CONFIG_ID} .reset_holder {
  165. padding-top: 4px;
  166. }
  167. `;
  168.  
  169. const CONFIG_IFRAME_CSS = `
  170. position: fixed;
  171. z-index: 99999;
  172. width: 300px;
  173. height: 300px;
  174. border: 1px solid;
  175. border-radius: 10px;
  176. `;
  177.  
  178. const CONFIG_FIELDS = {
  179. cookies: {
  180. label: "Try Pass Cookies",
  181. type: "select",
  182. options: ["yes", "no"],
  183. default: "no",
  184. },
  185. profile: {
  186. label: "MPV Profile",
  187. type: "text",
  188. default: "default",
  189. },
  190. quality: {
  191. label: "Prefer Video Quality",
  192. type: "select",
  193. options: ["default", "2160p", "1440p", "1080p", "720p", "480p", "360p"],
  194. default: "default",
  195. },
  196. v_codec: {
  197. label: "Prefer Video Codec",
  198. type: "select",
  199. options: ["default", "av01", "vp9", "h265", "h264"],
  200. default: "default",
  201. },
  202. console: {
  203. label: "Run With Console",
  204. type: "select",
  205. options: ["yes", "no"],
  206. default: "yes",
  207. },
  208. };
  209.  
  210. // GM_config init
  211. GM_config.init({
  212. id: CONFIG_ID,
  213. title: GM_info.script.name,
  214. fields: CONFIG_FIELDS,
  215. events: {
  216. init: () => {
  217. let quality = GM_config.get("quality").toLowerCase();
  218. let v_codec = GM_config.get("v_codec").toLowerCase();
  219.  
  220. if (!CONFIG_FIELDS.quality.options.includes(quality)) {
  221. GM_config.set("quality", "default");
  222. }
  223. if (!CONFIG_FIELDS.v_codec.options.includes(v_codec)) {
  224. GM_config.set("v_codec", "default");
  225. }
  226. },
  227. save: () => {
  228. let profile = GM_config.get("profile").trim();
  229.  
  230. if (profile === "") {
  231. GM_config.set("profile", "default");
  232. } else {
  233. GM_config.set("profile", profile);
  234. }
  235.  
  236. updateButton(location.href);
  237. GM_config.close();
  238. },
  239. reset: () => {
  240. GM_config.save();
  241. },
  242. },
  243. css: CONFIG_CSS.trim(),
  244. });
  245.  
  246. // URL-safe base64 encode
  247. function btoaUrl(url) {
  248. return btoa(url).replace(/\//g, "_").replace(/\+/g, "-").replace(/\=/g, "");
  249. }
  250.  
  251. // Generate protocol
  252. function generateProto(url) {
  253. let cookies = GM_config.get("cookies").toLowerCase();
  254. let profile = GM_config.get("profile").trim();
  255. let quality = GM_config.get("quality").toLowerCase();
  256. let v_codec = GM_config.get("v_codec").toLowerCase();
  257. let console = GM_config.get("console").toLowerCase();
  258. let options = [];
  259.  
  260. let proto;
  261.  
  262. if (console === "yes") {
  263. proto = "mpv-debug://play/" + btoaUrl(url);
  264. } else {
  265. proto = "mpv://play/" + btoaUrl(url);
  266. }
  267. if (cookies === "yes") {
  268. options.push("cookies=" + document.location.hostname + ".txt");
  269. }
  270. if (profile !== "default" && profile !== "") {
  271. options.push("profile=" + profile);
  272. }
  273. if (quality !== "default") {
  274. options.push("quality=" + quality);
  275. }
  276. if (v_codec !== "default") {
  277. options.push("v_codec=" + v_codec);
  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. return proto;
  293. }
  294.  
  295. // Check the URL is matched or not
  296. function matchUrl(url) {
  297. if (MATCHERS[location.hostname]) {
  298. return url.search(MATCHERS[location.hostname]) !== -1;
  299. } else {
  300. return false;
  301. }
  302. }
  303.  
  304. // Update button display status and URL
  305. function updateButton(url) {
  306. let isMatch = matchUrl(url);
  307. let button = document.getElementsByClassName("pwm-play")[0];
  308.  
  309. if (button) {
  310. button.style =
  311. isMatch && !document.fullscreenElement
  312. ? "display: block"
  313. : "display: none";
  314. button.href = isMatch ? generateProto(url) : "";
  315. }
  316. }
  317.  
  318. // Notify update about mpv-handler
  319. function notifyUpdate() {
  320. let version = GM_getValue("mpvHandlerVersion", null);
  321.  
  322. if (version !== MPV_HANDLER_VERSION) {
  323. const UPDATE_NOTIFY = {
  324. title: `${GM_info.script.name}`,
  325. text: `mpv-handler is upgraded to ${MPV_HANDLER_VERSION}\n\nClick to check updates`,
  326. onclick: () => {
  327. window.open("https://github.com/akiirui/mpv-handler/releases/latest");
  328. },
  329. };
  330.  
  331. GM_notification(UPDATE_NOTIFY);
  332. GM_setValue("mpvHandlerVersion", MPV_HANDLER_VERSION);
  333. }
  334. }
  335.  
  336. // Add play and settings buttons to page
  337. function createButton() {
  338. let head = document.getElementsByTagName("head")[0];
  339. let style = document.createElement("style");
  340.  
  341. if (head) {
  342. style.innerHTML = MPV_CSS.trim();
  343. head.appendChild(style);
  344. }
  345.  
  346. let body = document.body;
  347. let buttonDiv = document.createElement("div");
  348. let buttonPlay = document.createElement("a");
  349. let buttonSettings = document.createElement("button");
  350.  
  351. if (body) {
  352. buttonPlay.className = "pwm-play";
  353. buttonPlay.style = "display: none";
  354. buttonPlay.target = "_blank";
  355. buttonPlay.addEventListener("click", (e) => {
  356. let videoElement = document.getElementsByTagName("video")[0];
  357. if (videoElement) videoElement.pause();
  358. if (e.stopPropagation) e.stopPropagation();
  359. });
  360.  
  361. buttonSettings.className = "pwm-settings";
  362. buttonSettings.addEventListener("click", () => {
  363. if (!GM_config.isOpen) {
  364. GM_config.open();
  365. GM_config.frame.style = CONFIG_IFRAME_CSS.trim();
  366. }
  367. });
  368.  
  369. buttonDiv.className = "play-with-mpv";
  370. buttonDiv.appendChild(buttonPlay);
  371. buttonDiv.appendChild(buttonSettings);
  372.  
  373. body.appendChild(buttonDiv);
  374.  
  375. document.addEventListener("fullscreenchange", () => {
  376. let button = document.getElementsByClassName("pwm-play")[0];
  377.  
  378. button.style = document.fullscreenElement
  379. ? "display: none"
  380. : "display: block";
  381. });
  382. }
  383. }
  384.  
  385. // Detect PJAX changes
  386. function detectPJAX() {
  387. let previousUrl = null;
  388. let currentUrl = null;
  389.  
  390. setInterval(() => {
  391. currentUrl = location.href;
  392.  
  393. if (previousUrl !== currentUrl) {
  394. updateButton(currentUrl);
  395. previousUrl = currentUrl;
  396. }
  397. }, 500);
  398. }
  399.  
  400. notifyUpdate();
  401. createButton();
  402. detectPJAX();