YouTube Minimal on PC

Watch YouTube with the least CPU usage

  1. /*
  2.  
  3. MIT License
  4.  
  5. Copyright 2023 CY Fung
  6.  
  7. Permission is hereby granted, free of charge, to any person obtaining a copy
  8. of this software and associated documentation files (the "Software"), to deal
  9. in the Software without restriction, including without limitation the rights
  10. to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  11. copies of the Software, and to permit persons to whom the Software is
  12. furnished to do so, subject to the following conditions:
  13.  
  14. The above copyright notice and this permission notice shall be included in all
  15. copies or substantial portions of the Software.
  16.  
  17. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  18. IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  19. FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  20. AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  21. LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  22. OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
  23. SOFTWARE.
  24.  
  25. */
  26. // ==UserScript==
  27. // @name YouTube Minimal on PC
  28. // @version 0.5
  29. // @description Watch YouTube with the least CPU usage
  30. // @namespace http://tampermonkey.net/
  31. // @author CY Fung
  32. // @license MIT
  33. // @supportURL https://github.com/cyfung1031/userscript-supports
  34. // @run-at document-start
  35. // @match https://www.youtube.com/*
  36. // @match https://m.youtube.com/*
  37. // @icon https://raw.githubusercontent.com/cyfung1031/userscript-supports/main/icons/youtube-minimal.png
  38. // @grant GM_registerMenuCommand
  39. // @require https://cdnjs.cloudflare.com/ajax/libs/js-cookie/3.0.1/js.cookie.min.js#sha512=wT7uPE7tOP6w4o28u1DN775jYjHQApdBnib5Pho4RB0Pgd9y7eSkAV1BTqQydupYDB9GBhTcQQzyNMPMV3cAew==
  40. // ==/UserScript==
  41. "use strict";
  42.  
  43. /* global Cookies */
  44.  
  45. (function () {
  46. "use strict";
  47.  
  48. function addParam(u, s) {
  49. if (typeof u === 'string') {
  50. u += (u.indexOf('?') > 0 ? '&' : '?') + s
  51. }
  52. return u
  53. }
  54.  
  55. let mUrl = Cookies.get("userjs-ym-url", { domain: "youtube.com", secure: true });
  56. if (mUrl) {
  57. Cookies.remove("userjs-ym-url", { domain: "youtube.com", secure: true });
  58. mUrl = addParam(mUrl, 'app=' + (mUrl.charAt(8) === 'w' ? 'desktop' : 'm'))
  59. location.replace(mUrl);
  60. return;
  61. }
  62.  
  63. function getUrl() {
  64. return location.href.replace(/(?<=[&?])(persist_app|app)=\w+(&|$)/g, '').replace(/[?&]$/, '')
  65. }
  66.  
  67. let href = getUrl() || '';
  68. let hrefC8 = href.charAt(8) || '';
  69.  
  70. let iAmDesktop = hrefC8 === 'w';
  71. let iAmMobile = hrefC8 === 'm';
  72.  
  73. let redirection = false
  74.  
  75. if (href.indexOf(".youtube.com/watch") >= 0) {
  76. redirection = true
  77. } else if (href.endsWith('youtube.com/')) {
  78. redirection = true
  79. }
  80.  
  81. function addMenuCommand(s, url, b) {
  82.  
  83. GM_registerMenuCommand(s, function () {
  84. if (redirection) {
  85. let h = getUrl()
  86. if (b) h = h.replace("https://www.youtube.com/", "https://m.youtube.com/");
  87. else h = h.replace("https://m.youtube.com/", "https://www.youtube.com/");
  88. Cookies.set("userjs-ym-url", h, { domain: "youtube.com", secure: true });
  89. }
  90. location.replace(url);
  91. });
  92.  
  93. }
  94. if (iAmDesktop) {
  95. addMenuCommand("Switch to YouTube Mobile persistently", "https://m.youtube.com/?persist_app=1&app=m", true);
  96. addMenuCommand("Switch to YouTube Moble temporarily", "https://m.youtube.com/?persist_app=0&app=m", true);
  97.  
  98. } else if (iAmMobile) {
  99. addMenuCommand("Switch to YouTube Dekstop persistently", "http://www.youtube.com/?persist_app=1&app=desktop", false);
  100. addMenuCommand("Switch to YouTube Dekstop temporarily", "http://www.youtube.com/?persist_app=0&app=desktop", false);
  101.  
  102. }
  103.  
  104. addMenuCommand = null
  105.  
  106. // Your code here...
  107. })();

QingJ © 2025

镜像随时可能失效,请加Q群300939539或关注我们的公众号极客氢云获取最新地址