使用 MPV 播放

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

当前为 2023-02-05 提交的版本,查看 最新版本

  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.01.23
  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. // @grant GM_openInTab
  20. // @run-at document-idle
  21. // @noframes
  22. // @match *://www.youtube.com/*
  23. // @match *://m.youtube.com/*
  24. // @match *://www.twitch.tv/*
  25. // @match *://clips.twitch.tv/*
  26. // @match *://www.crunchyroll.com/*
  27. // @match *://beta.crunchyroll.com/*
  28. // @match *://www.bilibili.com/video/*
  29. // @match *://live.bilibili.com/*
  30. // ==/UserScript==
  31.  
  32. "use strict";
  33.  
  34. const MPV_HANDLER_VERSION = "v0.3.3";
  35.  
  36. const MATCHERS = {
  37. "www.youtube.com": /www.youtube.com\/(watch|playlist|shorts)\?/gi,
  38. "m.youtube.com": /m.youtube.com\/(watch|playlist|shorts)\?/gi,
  39. "www.twitch.tv":
  40. /www.twitch.tv\/(?!(directory|downloads|jobs|p|turbo)\/).+/gi,
  41. "clips.twitch.tv": /clips.twitch.tv/gi,
  42. "www.crunchyroll.com": /www.crunchyroll.com\/.*watch\/([0-9]|[A-Z])+/gi,
  43. "beta.crunchyroll.com": /beta.crunchyroll.com\/.*watch\/([0-9]|[A-Z])+/gi,
  44. "live.bilibili.com": /live.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MCAwIDExLjUwNzgxMiAxMi4yMjg1MTYg\
  76. TCAxMy4xODM1OTQgMTMuMDE3NTc4IEwgMTQuOTMzNTk0IDkuOTg2MzI4MSBMIDEzLjQxMjEwOSA4\
  77. LjkyOTY4NzUgQSA1LjUgNS41IDAgMCAwIDEzLjQ5NjA5NCA4LjAwMTk1MzEgQSA1LjUgNS41IDAg\
  78. MCAwIDEzLjQxNjAxNiA3LjA3MDMxMjUgTCAxNC45MzM1OTQgNi4wMTc1NzgxIEwgMTMuMTgzNTk0\
  79. IDIuOTg2MzI4MSBMIDExLjUxMTcxOSAzLjc3MzQzNzUgQSA1LjUgNS41IDAgMCAwIDkuOTAwMzkw\
  80. NiAyLjg0OTYwOTQgTCA5Ljc1IDEgTCA2LjI1IDEgeiBNIDggNiBBIDIgMiAwIDAgMSAxMCA4IEEg\
  81. MiAyIDAgMCAxIDggMTAgQSAyIDIgMCAwIDEgNiA4IEEgMiAyIDAgMCAxIDggNiB6IiB0cmFuc2Zv\
  82. cm09InRyYW5zbGF0ZSg0IDQpIi8+Cjwvc3ZnPgo=";
  83.  
  84. const MPV_CSS = `
  85. .pwm-play {
  86. width: 48px;
  87. height: 48px;
  88. border: 0;
  89. border-radius: 50%;
  90. background-size: 48px;
  91. background-image: url(data:image/svg+xml;base64,${ICON_MPV});
  92. background-repeat: no-repeat;
  93. }
  94. .pwm-settings {
  95. opacity: 0;
  96. visibility: hidden;
  97. transition: all 0.2s ease-in-out;
  98. display: block;
  99. position: absolute;
  100. top: -32px;
  101. width: 32px;
  102. height: 32px;
  103. margin-left: 8px;
  104. border: 0;
  105. border-radius: 50%;
  106. background-size: 32px;
  107. background-color: #eeeeee;
  108. background-image: url(data:image/svg+xml;base64,${ICON_SETTINGS});
  109. background-repeat: no-repeat;
  110. }
  111. .play-with-mpv {
  112. z-index: 99999;
  113. position: fixed;
  114. left: 8px;
  115. bottom: 8px;
  116. }
  117. .pwm-play:hover + .pwm-settings,
  118. .pwm-settings:hover {
  119. opacity: 1;
  120. visibility: visible;
  121. transition: all 0.2s ease-in-out;
  122. }
  123. `;
  124.  
  125. const CONFIG_ID = "play-with-mpv";
  126.  
  127. const CONFIG_CSS = `
  128. body {
  129. display: flex;
  130. justify-content: center;
  131. }
  132. #${CONFIG_ID}_wrapper {
  133. display: flex;
  134. flex-direction: column;
  135. justify-content: center;
  136. }
  137. #${CONFIG_ID} .config_header {
  138. display: flex;
  139. align-items: center;
  140. padding: 12px;
  141. }
  142. #${CONFIG_ID} .config_var {
  143. margin: 0 0 12px 0;
  144. }
  145. #${CONFIG_ID} .field_label {
  146. display: inline-block;
  147. width: 140px;
  148. font-size: 14px;
  149. }
  150. #${CONFIG_ID}_field_cookies,
  151. #${CONFIG_ID}_field_profile,
  152. #${CONFIG_ID}_field_quality,
  153. #${CONFIG_ID}_field_v_codec {
  154. width: 80px;
  155. height: 24px;
  156. font-size: 14px;
  157. text-align: center;
  158. }
  159. #${CONFIG_ID}_buttons_holder {
  160. display: flex;
  161. flex-direction: column;
  162. }
  163. #${CONFIG_ID} .saveclose_buttons {
  164. margin: 1px;
  165. padding: 4px 0;
  166. }
  167. #${CONFIG_ID} .reset_holder {
  168. padding-top: 4px;
  169. }
  170. `;
  171.  
  172. const CONFIG_IFRAME_CSS = `
  173. position: fixed;
  174. z-index: 99999;
  175. width: 300px;
  176. height: 300px;
  177. border: 1px solid;
  178. border-radius: 10px;
  179. `;
  180.  
  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. function notifyUpdate() {
  230. let version = GM_getValue("mpvHandlerVersion", null);
  231.  
  232. if (version !== MPV_HANDLER_VERSION) {
  233. const UPDATE_NOTIFY = {
  234. title: `${GM_info.script.name}`,
  235. text: `mpv-handler is upgraded to ${MPV_HANDLER_VERSION}\n\nClick to check updates`,
  236. onclick: () => {
  237. GM_openInTab("https://github.com/akiirui/mpv-handler/releases/latest");
  238. },
  239. };
  240.  
  241. GM_notification(UPDATE_NOTIFY);
  242. GM_setValue("mpvHandlerVersion", MPV_HANDLER_VERSION);
  243. }
  244. }
  245.  
  246. function matchUrl(currentUrl) {
  247. if (MATCHERS[location.hostname]) {
  248. return currentUrl.search(MATCHERS[location.hostname]) !== -1;
  249. } else {
  250. return false;
  251. }
  252. }
  253.  
  254. function appendButton() {
  255. let head = document.getElementsByTagName("head")[0];
  256. let style = document.createElement("style");
  257.  
  258. if (head) {
  259. style.innerHTML = MPV_CSS.trim();
  260. head.appendChild(style);
  261. }
  262.  
  263. let body = document.body;
  264. let buttonDiv = document.createElement("div");
  265. let buttonPlay = document.createElement("a");
  266. let buttonSettings = document.createElement("button");
  267.  
  268. if (body) {
  269. buttonPlay.className = "pwm-play";
  270. buttonPlay.style = "display: none";
  271. buttonPlay.target = "_blank";
  272. buttonPlay.addEventListener("click", (e) => {
  273. let videoElement = document.getElementsByTagName("video")[0];
  274. if (videoElement) videoElement.pause();
  275. if (e.stopPropagation) e.stopPropagation();
  276. });
  277.  
  278. buttonSettings.className = "pwm-settings";
  279. buttonSettings.addEventListener("click", () => {
  280. if (!GM_config.isOpen) {
  281. GM_config.open();
  282. GM_config.frame.style = CONFIG_IFRAME_CSS.trim();
  283. }
  284. });
  285.  
  286. buttonDiv.className = "play-with-mpv";
  287. buttonDiv.appendChild(buttonPlay);
  288. buttonDiv.appendChild(buttonSettings);
  289.  
  290. body.appendChild(buttonDiv);
  291.  
  292. document.addEventListener("fullscreenchange", () => {
  293. let button = document.getElementsByClassName("pwm-play")[0];
  294.  
  295. button.style = document.fullscreenElement
  296. ? "display: none"
  297. : "display: block";
  298. });
  299. }
  300. }
  301.  
  302. function updateButton(currentUrl) {
  303. let isMatch = matchUrl(currentUrl);
  304. let button = document.getElementsByClassName("pwm-play")[0];
  305.  
  306. if (button) {
  307. let cookies = GM_config.get("cookies").toLowerCase();
  308. let profile = GM_config.get("profile").trim();
  309. let quality = GM_config.get("quality").toLowerCase();
  310. let v_codec = GM_config.get("v_codec").toLowerCase();
  311. let options = [];
  312.  
  313. let proto =
  314. "mpv://play/" + btoa(currentUrl).replace(/\//g, "_").replace(/\+/g, "-");
  315.  
  316. if (cookies === "yes") {
  317. options.push("cookies=" + document.location.hostname + ".txt");
  318. }
  319. if (profile !== "default" && profile !== "") {
  320. options.push("profile=" + profile);
  321. }
  322. if (quality !== "best") {
  323. options.push("quality=" + quality);
  324. }
  325. if (v_codec !== "any") {
  326. options.push("v_codec=" + v_codec);
  327. }
  328.  
  329. if (options.length !== 0) {
  330. proto += "/?";
  331.  
  332. options.forEach((option, index) => {
  333. proto += option;
  334.  
  335. if (index + 1 !== options.length) {
  336. proto += "&";
  337. }
  338. });
  339. }
  340.  
  341. button.style = isMatch ? "display: block" : "display: none";
  342. button.href = isMatch ? proto : "";
  343. }
  344. }
  345.  
  346. function detectPJAX() {
  347. let previousUrl = null;
  348. let currentUrl = null;
  349.  
  350. setInterval(() => {
  351. currentUrl = location.href;
  352.  
  353. if (previousUrl !== currentUrl) {
  354. updateButton(currentUrl);
  355. previousUrl = currentUrl;
  356. }
  357. }, 500);
  358. }
  359.  
  360. notifyUpdate();
  361. appendButton();
  362. detectPJAX();