使用 MPV 播放

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

目前为 2023-02-10 提交的版本。查看 最新版本

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