Advertisement
Guest User

Untitled

a guest
Nov 28th, 2022
111
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 12.41 KB | None | 0 0
  1. // ==UserScript==
  2. // @name Освещение и качество видео ютуб
  3. // @namespace http://tampermonkey.net/
  4. // @version 0.1
  5. // @description try to take over the world!
  6. // @author You
  7. // @include https://www.youtube.com/
  8. // @match https://www.youtube.com/watch*
  9. // @icon https://www.google.com/s2/favicons?sz=64&domain=alternativeto.net
  10. // @run-at document-start
  11. // @grant none
  12. // ==/UserScript==
  13.  
  14. (function() {
  15. 'use strict';
  16. //вырубить освещение на ютубе
  17. var style = document.createElement("style");
  18. style.innerText = "#cinematics{display:none!important;}";
  19. document.head.appendChild(style);
  20. // Your code here...
  21.  
  22. //Убрать окно которое напоминает о фоновой подствке
  23. //var okno = document.body.getElementByClassName(".style-scope.ytd-popup-container").style.display = "none";
  24.  
  25.  
  26.  
  27.  
  28. //автоматическое качество ютуб
  29.  
  30. // --- SETTINGS -------
  31.  
  32. // Target Resolution to always set to. If not available, the next best resolution will be used.
  33. const changeResolution = true;
  34. const targetRes = "hd1080";
  35. // Choices for targetRes are currently:
  36. // "highres" >= ( 8K / 4320p / QUHD )
  37. // "hd2880" = ( 5K / 2880p / UHD+ )
  38. // "hd2160" = ( 4K / 2160p / UHD )
  39. // "hd1440" = ( 1440p / QHD )
  40. // "hd1080" = ( 1080p / FHD )
  41. // "hd720" = ( 720p / HD )
  42. // "large" = ( 480p )
  43. // "medium" = ( 360p )
  44. // "small" = ( 240p )
  45. // "tiny" = ( 144p )
  46.  
  47. // Target Resolution for high framerate (60 fps) videos
  48. // If null, it is the same as targetRes
  49. const highFramerateTargetRes = null;
  50.  
  51. // If changePlayerSize is true, then the video's size will be changed on the page
  52. // instead of using youtube's default (if theater mode is enabled).
  53. // If useCustomSize is false, then the player will be resized to try to match the target resolution.
  54. // If true, then it will use the customHeight variables (theater mode is always full page width).
  55. const changePlayerSize = false;
  56. const useCustomSize = false;
  57. const customHeight = 600;
  58.  
  59. // If autoTheater is true, each video page opened will default to theater mode.
  60. // This means the video will always be resized immediately if you are changing the size.
  61. // NOTE: YouTube will not always allow theater mode immediately, the page must be fully loaded before theater can be set.
  62. const autoTheater = false;
  63.  
  64. // If flushBuffer is false, then the first second or so of the video may not always be the desired resolution.
  65. // If true, then the entire video will be guaranteed to be the target resolution, but there may be
  66. // a very small additional delay before the video starts if the buffer needs to be flushed.
  67. const flushBuffer = true;
  68.  
  69. // Setting cookies can allow some operations to perform faster or without a delay (e.g. theater mode)
  70. // Some people don't like setting cookies, so this is false by default (which is the same as old behavior)
  71. const allowCookies = false;
  72.  
  73. // Tries to set the resolution as early as possible.
  74. // This might cause issues on youtube polymer layout, so disable if videos fail to load.
  75. // If videos load fine, leave as true or resolution may fail to set.
  76. const setResolutionEarly = true;
  77.  
  78. // Enables a temporary work around for an issue where users can get the wrong youtube error screen
  79. // (Youtube has two of them for some reason and changing to theater mode moves the wrong one to the front)
  80. // Try disabling if you can't interact with the video or you think you are missing an error message.
  81. const enableErrorScreenWorkaround = true;
  82.  
  83. // --------------------
  84.  
  85.  
  86.  
  87.  
  88. // --- GLOBALS --------
  89.  
  90.  
  91. const DEBUG = false;
  92.  
  93. // Possible resolution choices (in decreasing order, i.e. highres is the best):
  94. const resolutions = ['highres', 'hd2880', 'hd2160', 'hd1440', 'hd1080', 'hd720', 'large', 'medium', 'small', 'tiny'];
  95. // youtube has to be at least 480x270 for the player UI
  96. const heights = [4320, 2880, 2160, 1440, 1080, 720, 480, 360, 270, 270];
  97.  
  98. let doc = document, win = window;
  99.  
  100. // ID of the most recently played video
  101. let recentVideo = "";
  102.  
  103. let foundHFR = false;
  104.  
  105. let setHeight = 0;
  106.  
  107.  
  108. // --------------------
  109.  
  110.  
  111. function debugLog(message)
  112. {
  113. if (DEBUG)
  114. {
  115. console.log("YTHD | " + message);
  116. }
  117. }
  118.  
  119.  
  120. // --------------------
  121.  
  122.  
  123. // Used only for compatability with webextensions version of greasemonkey
  124. function unwrapElement(el)
  125. {
  126. if (el && el.wrappedJSObject)
  127. {
  128. return el.wrappedJSObject;
  129. }
  130. return el;
  131. }
  132.  
  133.  
  134. // --------------------
  135.  
  136.  
  137. // Get video ID from the currently loaded video (which might be different than currently loaded page)
  138. function getVideoIDFromURL(ytPlayer)
  139. {
  140. const idMatch = /(?:v=)([\w\-]+)/;
  141. let id = "ERROR: idMatch failed; youtube changed something";
  142. let matches = idMatch.exec(ytPlayer.getVideoUrl());
  143. if (matches)
  144. {
  145. id = matches[1];
  146. }
  147.  
  148. return id;
  149. }
  150.  
  151.  
  152. // --------------------
  153.  
  154.  
  155. // Attempt to set the video resolution to desired quality or the next best quality
  156. function setResolution(ytPlayer, resolutionList)
  157. {
  158. debugLog("Setting Resolution...");
  159.  
  160. const currentQuality = ytPlayer.getPlaybackQuality();
  161. let res = targetRes;
  162.  
  163. if (highFramerateTargetRes && foundHFR)
  164. {
  165. res = highFramerateTargetRes;
  166. }
  167.  
  168. // Youtube doesn't return "auto" for auto, so set to make sure that auto is not set by setting
  169. // even when already at target res or above, but do so without removing the buffer for this quality
  170. if (resolutionList.indexOf(res) >= resolutionList.indexOf(currentQuality))
  171. {
  172. if (ytPlayer.setPlaybackQualityRange !== undefined)
  173. {
  174. ytPlayer.setPlaybackQualityRange(res);
  175. }
  176. ytPlayer.setPlaybackQuality(res);
  177. debugLog("Resolution Set To: " + res);
  178. return;
  179. }
  180.  
  181. const end = resolutionList.length - 1;
  182. let nextBestIndex = Math.max(resolutionList.indexOf(res), 0);
  183. let ytResolutions = ytPlayer.getAvailableQualityLevels();
  184. debugLog("Available Resolutions: " + ytResolutions.join(", "));
  185.  
  186. while ( (ytResolutions.indexOf(resolutionList[nextBestIndex]) === -1) && nextBestIndex < end )
  187. {
  188. ++nextBestIndex;
  189. }
  190.  
  191. if (flushBuffer && currentQuality !== resolutionList[nextBestIndex])
  192. {
  193. let id = getVideoIDFromURL(ytPlayer);
  194. if (id.indexOf("ERROR") === -1)
  195. {
  196. let pos = ytPlayer.getCurrentTime();
  197. ytPlayer.loadVideoById(id, pos, resolutionList[nextBestIndex]);
  198. }
  199.  
  200. debugLog("ID: " + id);
  201. }
  202. if (ytPlayer.setPlaybackQualityRange !== undefined)
  203. {
  204. ytPlayer.setPlaybackQualityRange(resolutionList[nextBestIndex]);
  205. }
  206. ytPlayer.setPlaybackQuality(resolutionList[nextBestIndex]);
  207.  
  208. debugLog("Resolution Set To: " + resolutionList[nextBestIndex]);
  209. }
  210.  
  211.  
  212. // --------------------
  213.  
  214.  
  215. // Set resolution, but only when API is ready (it should normally already be ready)
  216. function setResOnReady(ytPlayer, resolutionList)
  217. {
  218. if (ytPlayer.getPlaybackQuality === undefined)
  219. {
  220. win.setTimeout(setResOnReady, 100, ytPlayer, resolutionList);
  221. }
  222. else
  223. {
  224. let framerateUpdate = false;
  225. if (highFramerateTargetRes)
  226. {
  227. let features = ytPlayer.getVideoData().video_quality_features;
  228. if (features)
  229. {
  230. let isHFR = features.includes("hfr");
  231. framerateUpdate = isHFR && !foundHFR;
  232. foundHFR = isHFR;
  233. }
  234. }
  235.  
  236. let curVid = getVideoIDFromURL(ytPlayer);
  237. if ((curVid !== recentVideo) || framerateUpdate)
  238. {
  239. recentVideo = curVid;
  240. setResolution(ytPlayer, resolutionList);
  241.  
  242. let storedQuality = localStorage.getItem("yt-player-quality");
  243. if (!storedQuality || storedQuality.indexOf(targetRes) === -1)
  244. {
  245. let tc = Date.now(), te = tc + 2592000000;
  246. localStorage.setItem("yt-player-quality","{\"data\":\"" + targetRes + "\",\"expiration\":" + te + ",\"creation\":" + tc + "}");
  247. }
  248. }
  249. }
  250. }
  251.  
  252.  
  253. // --------------------
  254.  
  255.  
  256. function setTheaterMode(ytPlayer)
  257. {
  258. debugLog("Setting Theater Mode");
  259.  
  260. if (win.location.href.indexOf("/watch") !== -1)
  261. {
  262. let pageManager = unwrapElement(doc.getElementsByTagName("ytd-watch-flexy")[0]);
  263.  
  264. if (pageManager)
  265. {
  266. if (enableErrorScreenWorkaround)
  267. {
  268. const styleContent = "#error-screen { z-index: 42 !important } .ytp-error { display: none !important }";
  269.  
  270. let errorStyle = doc.getElementById("ythdErrorWorkaroundStyleSheet");
  271. if (!errorStyle)
  272. {
  273. errorStyle = doc.createElement("style");
  274. errorStyle.type = "text/css";
  275. errorStyle.id = "ythdStyleSheet";
  276. errorStyle.innerHTML = styleContent;
  277. doc.head.appendChild(errorStyle);
  278. }
  279. else
  280. {
  281. errorStyle.innerHTML = styleContent;
  282. }
  283. }
  284.  
  285. try
  286. {
  287. pageManager.theaterModeChanged_(true);
  288. }
  289. catch (e)
  290. { /* Ignore internal youtube exceptions. */ }
  291. }
  292. }
  293. }
  294.  
  295.  
  296. // --------------------
  297.  
  298.  
  299. function computeAndSetPlayerSize()
  300. {
  301. let height = customHeight;
  302. if (!useCustomSize)
  303. {
  304. // don't include youtube search bar as part of the space the video can try to fit in
  305. let heightOffsetEl = doc.getElementById("masthead");
  306. let mastheadContainerEl = doc.getElementById("masthead-container");
  307. let mastheadHeight = 50, mastheadPadding = 16;
  308. if (heightOffsetEl && mastheadContainerEl)
  309. {
  310. mastheadHeight = parseInt(win.getComputedStyle(heightOffsetEl).height, 10);
  311. mastheadPadding = parseInt(win.getComputedStyle(mastheadContainerEl).paddingBottom, 10) * 2;
  312. }
  313.  
  314. let i = Math.max(resolutions.indexOf(targetRes), 0);
  315. height = Math.min(heights[i], win.innerHeight - (mastheadHeight + mastheadPadding));
  316. }
  317.  
  318. resizePlayer(height);
  319. }
  320.  
  321.  
  322. // --------------------
  323.  
  324.  
  325. // resize the player
  326. function resizePlayer(height)
  327. {
  328. debugLog("Setting video player size");
  329.  
  330. if (setHeight === height)
  331. {
  332. debugLog("Player size already set");
  333. return;
  334. }
  335.  
  336. let styleContent = "\
  337. ytd-watch-flexy[theater]:not([fullscreen]) #player-theater-container.style-scope { \
  338. min-height: " + height + "px !important; max-height: none !important; height: " + height + "px !important } \
  339. ";
  340.  
  341. let ythdStyle = doc.getElementById("ythdStyleSheet");
  342. if (!ythdStyle)
  343. {
  344. ythdStyle = doc.createElement("style");
  345. ythdStyle.type = "text/css";
  346. ythdStyle.id = "ythdStyleSheet";
  347. ythdStyle.innerHTML = styleContent;
  348. doc.head.appendChild(ythdStyle);
  349. }
  350. else
  351. {
  352. ythdStyle.innerHTML = styleContent;
  353. }
  354.  
  355. setHeight = height;
  356.  
  357. win.dispatchEvent(new Event("resize"));
  358. }
  359.  
  360.  
  361. // --- MAIN -----------
  362.  
  363.  
  364. function main()
  365. {
  366. let ytPlayer = doc.getElementById("movie_player") || doc.getElementsByClassName("html5-video-player")[0];
  367. let ytPlayerUnwrapped = unwrapElement(ytPlayer);
  368.  
  369. if (autoTheater && ytPlayerUnwrapped)
  370. {
  371. if (allowCookies && doc.cookie.indexOf("wide=1") === -1)
  372. {
  373. doc.cookie = "wide=1; domain=.youtube.com";
  374. }
  375.  
  376. setTheaterMode(ytPlayerUnwrapped);
  377. }
  378.  
  379. if (changePlayerSize && win.location.host.indexOf("youtube.com") !== -1 && win.location.host.indexOf("gaming.") === -1)
  380. {
  381. computeAndSetPlayerSize();
  382. window.addEventListener("resize", computeAndSetPlayerSize, true);
  383. }
  384.  
  385. if (changeResolution && setResolutionEarly && ytPlayerUnwrapped)
  386. {
  387. setResOnReady(ytPlayerUnwrapped, resolutions);
  388. }
  389.  
  390. if (changeResolution || autoTheater)
  391. {
  392. win.addEventListener("loadstart", function(e) {
  393. if (!(e.target instanceof win.HTMLMediaElement))
  394. {
  395. return;
  396. }
  397.  
  398. ytPlayer = doc.getElementById("movie_player") || doc.getElementsByClassName("html5-video-player")[0];
  399. ytPlayerUnwrapped = unwrapElement(ytPlayer);
  400. if (ytPlayerUnwrapped)
  401. {
  402. debugLog("Loaded new video");
  403. if (changeResolution)
  404. {
  405. setResOnReady(ytPlayerUnwrapped, resolutions);
  406. }
  407. if (autoTheater)
  408. {
  409. setTheaterMode(ytPlayerUnwrapped);
  410. }
  411. }
  412. }, true );
  413. }
  414.  
  415. // This will eventually be changed to use the "once" option, but I want to keep a large range of browser support.
  416. win.removeEventListener("yt-navigate-finish", main, true);
  417. }
  418.  
  419. main();
  420. // Youtube doesn't load the page immediately in new version so you can watch before waiting for page load
  421. // But we can only set resolution until the page finishes loading
  422. win.addEventListener("yt-navigate-finish", main, true);
  423.  
  424. })();
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement