YouTube ProgressBar Preserver

让你恒常地显示油管上的进度条(显示播放时间比例的红色条)。

当前为 2020-06-14 提交的版本,查看 最新版本

  1. // ==UserScript==
  2. // @name YouTube ProgressBar Preserver
  3. // @name:ja YouTube ProgressBar Preserver
  4. // @name:zh-CN YouTube ProgressBar Preserver
  5. // @description It preserves YouTube's progress bar always visible even if the controls are hidden.
  6. // @description:ja YouTubeのプログレスバー(再生時刻の割合を示す赤いバー)を、隠さず常に表示させるようにします。
  7. // @description:zh-CN 让你恒常地显示油管上的进度条(显示播放时间比例的红色条)。
  8. // @namespace knoa.jp
  9. // @include https://www.youtube.com/*
  10. // @include https://www.youtube-nocookie.com/embed/*
  11. // @exclude https://www.youtube.com/live_chat*
  12. // @exclude https://www.youtube.com/live_chat_replay*
  13. // @version 1.0.0
  14. // @grant none
  15. // ==/UserScript==
  16.  
  17. (function(){
  18. const SCRIPTID = 'YouTubeProgressBarPreserver';
  19. const SCRIPTNAME = 'YouTube ProgressBar Preserver';
  20. const DEBUG = false;/*
  21. [update] 1.0.0
  22. Added slight shadow for better visibility.
  23.  
  24. [bug]
  25. buffered.end(0) エラー?
  26.  
  27. [todo]
  28.  
  29. [research]
  30. timeupdateの間隔ぶんだけ遅れてしまうのはうまく改善できるかどうか
  31. timeupdateきっかけで250ms(前回との差?)をキープするような仕組みでいける?
  32. もっとも、時間の短い広告時くらいしか知覚できないけど。
  33.  
  34. [memo]
  35. YouTubeによって隠されているときはオリジナルのバーは更新されないので、独自に作るほうがラク。
  36. 0.9完成後、youtube progressbar で検索したところすでに存在していることを発見\(^o^)/
  37. https://addons.mozilla.org/ja/firefox/addon/progress-bar-for-youtube/
  38. カスタマイズできるが、生放送に対応していない。プログレスが最低0.5秒単位でtransitionもない。
  39. */
  40. if(window === top && console.time) console.time(SCRIPTID);
  41. const MS = 1, SECOND = 1000*MS, MINUTE = 60*SECOND, HOUR = 60*MINUTE, DAY = 24*HOUR, WEEK = 7*DAY, MONTH = 30*DAY, YEAR = 365*DAY;
  42. const INTERVAL = 1*SECOND;/*for core.checkUrl*/
  43. const SHORTDURATION = 4*MINUTE / 1000;/*short video should have transition*/
  44. const STARTSWITH = [/*for core.checkUrl*/
  45. 'https://www.youtube.com/watch?',
  46. 'https://www.youtube.com/embed/',
  47. 'https://www.youtube-nocookie.com/embed/',
  48. ];
  49. let site = {
  50. targets: {
  51. player: () => $('.html5-video-player'),
  52. video: () => $('video[src]'),
  53. time: () => $('.ytp-time-display'),
  54. },
  55. is: {
  56. live: (time) => time.classList.contains('ytp-live'),
  57. },
  58. };
  59. let elements = {}, timers = {};
  60. let core = {
  61. initialize: function(){
  62. elements.html = document.documentElement;
  63. elements.html.classList.add(SCRIPTID);
  64. core.checkUrl();
  65. core.addStyle();
  66. },
  67. checkUrl: function(){
  68. let previousUrl = '';
  69. timers.checkUrl = setInterval(function(){
  70. if(document.hidden) return;
  71. /* The page is visible, so... */
  72. if(location.href === previousUrl) return;
  73. else previousUrl = location.href;
  74. /* The URL has changed, so... */
  75. if(STARTSWITH.some(url => location.href.startsWith(url)) === false) return;
  76. /* This page should be modified, so... */
  77. core.ready();
  78. }, INTERVAL);
  79. },
  80. ready: function(){
  81. core.getTargets(site.targets).then(() => {
  82. log("I'm ready.");
  83. core.appendBar();
  84. core.observeTime();
  85. core.observeVideo();
  86. }).catch(e => {
  87. console.error(`${SCRIPTID}:${e.lineNumber} ${e.name}: ${e.message}`);
  88. });
  89. },
  90. appendBar: function(){
  91. if(elements.bar && elements.bar.isConnected) return;
  92. let bar = elements.bar = createElement(html.bar());
  93. let progress = elements.progress = bar.firstElementChild;
  94. let buffer = elements.buffer = bar.lastElementChild;
  95. elements.player.appendChild(bar);
  96. },
  97. observeTime: function(){
  98. /* detect live for hiding the bar */
  99. let time = elements.time, bar = elements.bar;
  100. let detect = function(time, bar){
  101. if(site.is.live(time)) bar.classList.remove('active');
  102. else bar.classList.add('active');
  103. };
  104. detect(time, bar);
  105. if(time.isObservingAttributes) return;
  106. time.isObservingAttributes = true;
  107. let observer = observe(time, function(records){
  108. detect(time, bar);
  109. }, {attributes: true});
  110. },
  111. observeVideo: function(){
  112. let video = elements.video, progress = elements.progress, buffer = elements.buffer;
  113. if(video.isObservingForProgressBar) return;
  114. video.isObservingForProgressBar = true;
  115. if(video.duration < SHORTDURATION) progress.classList.add('transition');
  116. progress.style.transform = 'scaleX(0)';
  117. video.addEventListener('durationchange', function(e){
  118. if(video.duration < SHORTDURATION) progress.classList.add('transition');
  119. else progress.classList.remove('transition');
  120. });
  121. video.addEventListener('timeupdate', function(e){
  122. progress.style.transform = `scaleX(${video.currentTime / video.duration})`;
  123. });
  124. video.addEventListener('progress', function(e){
  125. for(let i = video.buffered.length - 1; 0 <= i; i--){
  126. if(video.currentTime < video.buffered.start(i)) continue;
  127. buffer.style.transform = `scaleX(${video.buffered.end(i) / video.duration})`;
  128. break;
  129. }
  130. });
  131. video.addEventListener('seeking', function(e){
  132. if(video.buffered.end(0) === video.duration) return;
  133. buffer.style.transform = `scaleX(0)`;
  134. });
  135. },
  136. getTarget: function(selector, retry = 10, interval = 1*SECOND){
  137. const key = selector.name;
  138. const get = function(resolve, reject){
  139. let selected = selector();
  140. if(selected && selected.length > 0) selected.forEach((s) => s.dataset.selector = key);/* elements */
  141. else if(selected instanceof HTMLElement) selected.dataset.selector = key;/* element */
  142. else if(--retry) return log(`Not found: ${key}, retrying... (${retry})`), setTimeout(get, interval, resolve, reject);
  143. else return reject(new Error(`Not found: ${selector.name}, I give up.`));
  144. elements[key] = selected;
  145. resolve(selected);
  146. };
  147. return new Promise(function(resolve, reject){
  148. get(resolve, reject);
  149. });
  150. },
  151. getTargets: function(selectors, retry = 10, interval = 1*SECOND){
  152. return Promise.all(Object.values(selectors).map(selector => core.getTarget(selector, retry, interval)));
  153. },
  154. addStyle: function(name = 'style'){
  155. if(html[name] === undefined) return;
  156. let style = createElement(html[name]());
  157. document.head.appendChild(style);
  158. if(elements[name] && elements[name].isConnected) document.head.removeChild(elements[name]);
  159. elements[name] = style;
  160. },
  161. };
  162. const html = {
  163. bar: () => `<div id="${SCRIPTID}-bar"><div id="${SCRIPTID}-progress"></div><div id="${SCRIPTID}-buffer"></div></div>`,
  164. style: () => `
  165. <style type="text/css">
  166. /* preserved bar */
  167. #${SCRIPTID}-bar{
  168. --height: 3px;
  169. --background: rgba(255,255,255,.2);
  170. --filter: drop-shadow(0px 0px var(--height) rgba(0,0,0,.5));
  171. --color: #f00;
  172. --ad-color: #fc0;
  173. --buffer-color: rgba(255,255,255,.4);
  174. --transition-bar: opacity .25s cubic-bezier(0.0,0.0,0.2,1);
  175. --transition-progress: transform .25s linear;
  176. --z-index: 100;
  177. }
  178. #${SCRIPTID}-bar{
  179. width: 100%;
  180. height: var(--height);
  181. background: var(--background);
  182. position: absolute;
  183. bottom: 0;
  184. transition: var(--transition-bar);
  185. opacity: 0;
  186. z-index: var(--z-index);
  187. }
  188. #${SCRIPTID}-progress,
  189. #${SCRIPTID}-buffer{
  190. width: 100%;
  191. height: var(--height);
  192. transform-origin: 0 0;
  193. position: absolute;
  194. }
  195. #${SCRIPTID}-progress.transition,
  196. #${SCRIPTID}-buffer{
  197. transition: var(--transition-progress);
  198. }
  199. #${SCRIPTID}-progress{
  200. background: var(--color);
  201. filter: var(--filter);
  202. z-index: 1;
  203. }
  204. #${SCRIPTID}-buffer{
  205. background: var(--buffer-color);
  206. }
  207. .ad-interrupting/*advertisement*/ #${SCRIPTID}-progress{
  208. background: var(--ad-color);
  209. }
  210. /* replace the original bar */
  211. .ytp-autohide #${SCRIPTID}-bar.active{
  212. opacity: 1;
  213. }
  214. /* replace the bar for an ad */
  215. .ytp-ad-persistent-progress-bar-container/*YouTube offers progress bar only when an ad is showing, but it doesn't have transition animation*/{
  216. display: none
  217. }
  218. </style>
  219. `,
  220. };
  221. const setTimeout = window.setTimeout.bind(window), clearTimeout = window.clearTimeout.bind(window), setInterval = window.setInterval.bind(window), clearInterval = window.clearInterval.bind(window), requestAnimationFrame = window.requestAnimationFrame.bind(window);
  222. const alert = window.alert.bind(window), confirm = window.confirm.bind(window), getComputedStyle = window.getComputedStyle.bind(window), fetch = window.fetch.bind(window);
  223. if(!('isConnected' in Node.prototype)) Object.defineProperty(Node.prototype, 'isConnected', {get: function(){return document.contains(this)}});
  224. const $ = function(s, f){
  225. let target = document.querySelector(s);
  226. if(target === null) return null;
  227. return f ? f(target) : target;
  228. };
  229. const $$ = function(s, f){
  230. let targets = document.querySelectorAll(s);
  231. return f ? Array.from(targets).map(t => f(t)) : targets;
  232. };
  233. const createElement = function(html = '<span></span>'){
  234. let outer = document.createElement('div');
  235. outer.innerHTML = html;
  236. return outer.firstElementChild;
  237. };
  238. const observe = function(element, callback, options = {childList: true, attributes: false, characterData: false, subtree: false}){
  239. let observer = new MutationObserver(callback.bind(element));
  240. observer.observe(element, options);
  241. return observer;
  242. };
  243. const log = function(){
  244. if(!DEBUG) return;
  245. let l = log.last = log.now || new Date(), n = log.now = new Date();
  246. let error = new Error(), line = log.format.getLine(error), callers = log.format.getCallers(error);
  247. //console.log(error.stack);
  248. console.log(
  249. (SCRIPTID || '') + ':',
  250. /* 00:00:00.000 */ n.toLocaleTimeString() + '.' + n.getTime().toString().slice(-3),
  251. /* +0.000s */ '+' + ((n-l)/1000).toFixed(3) + 's',
  252. /* :00 */ ':' + line,
  253. /* caller.caller */ (callers[2] ? callers[2] + '() => ' : '') +
  254. /* caller */ (callers[1] || '') + '()',
  255. ...arguments
  256. );
  257. };
  258. log.formats = [{
  259. name: 'Firefox Scratchpad',
  260. detector: /MARKER@Scratchpad/,
  261. getLine: (e) => e.stack.split('\n')[1].match(/([0-9]+):[0-9]+$/)[1],
  262. getCallers: (e) => e.stack.match(/^[^@]*(?=@)/gm),
  263. }, {
  264. name: 'Firefox Console',
  265. detector: /MARKER@debugger/,
  266. getLine: (e) => e.stack.split('\n')[1].match(/([0-9]+):[0-9]+$/)[1],
  267. getCallers: (e) => e.stack.match(/^[^@]*(?=@)/gm),
  268. }, {
  269. name: 'Firefox Greasemonkey 3',
  270. detector: /\/gm_scripts\//,
  271. getLine: (e) => e.stack.split('\n')[1].match(/([0-9]+):[0-9]+$/)[1],
  272. getCallers: (e) => e.stack.match(/^[^@]*(?=@)/gm),
  273. }, {
  274. name: 'Firefox Greasemonkey 4+',
  275. detector: /MARKER@user-script:/,
  276. getLine: (e) => e.stack.split('\n')[1].match(/([0-9]+):[0-9]+$/)[1] - 500,
  277. getCallers: (e) => e.stack.match(/^[^@]*(?=@)/gm),
  278. }, {
  279. name: 'Firefox Tampermonkey',
  280. detector: /MARKER@moz-extension:/,
  281. getLine: (e) => e.stack.split('\n')[1].match(/([0-9]+):[0-9]+$/)[1] - 6,
  282. getCallers: (e) => e.stack.match(/^[^@]*(?=@)/gm),
  283. }, {
  284. name: 'Chrome Console',
  285. detector: /at MARKER \(<anonymous>/,
  286. getLine: (e) => e.stack.split('\n')[2].match(/([0-9]+):[0-9]+\)?$/)[1],
  287. getCallers: (e) => e.stack.match(/[^ ]+(?= \(<anonymous>)/gm),
  288. }, {
  289. name: 'Chrome Tampermonkey',
  290. detector: /at MARKER \(chrome-extension:.*?\/userscript.html\?id=/,
  291. getLine: (e) => e.stack.split('\n')[2].match(/([0-9]+):[0-9]+\)?$/)[1] - 6,
  292. getCallers: (e) => e.stack.match(/[^ ]+(?= \(chrome-extension:)/gm),
  293. }, {
  294. name: 'Chrome Extension',
  295. detector: /at MARKER \(chrome-extension:/,
  296. getLine: (e) => e.stack.split('\n')[2].match(/([0-9]+):[0-9]+\)?$/)[1],
  297. getCallers: (e) => e.stack.match(/[^ ]+(?= \(chrome-extension:)/gm),
  298. }, {
  299. name: 'Edge Console',
  300. detector: /at MARKER \(eval/,
  301. getLine: (e) => e.stack.split('\n')[2].match(/([0-9]+):[0-9]+\)$/)[1],
  302. getCallers: (e) => e.stack.match(/[^ ]+(?= \(eval)/gm),
  303. }, {
  304. name: 'Edge Tampermonkey',
  305. detector: /at MARKER \(Function/,
  306. getLine: (e) => e.stack.split('\n')[2].match(/([0-9]+):[0-9]+\)$/)[1] - 4,
  307. getCallers: (e) => e.stack.match(/[^ ]+(?= \(Function)/gm),
  308. }, {
  309. name: 'Safari',
  310. detector: /^MARKER$/m,
  311. getLine: (e) => 0,/*e.lineが用意されているが最終呼び出し位置のみ*/
  312. getCallers: (e) => e.stack.split('\n'),
  313. }, {
  314. name: 'Default',
  315. detector: /./,
  316. getLine: (e) => 0,
  317. getCallers: (e) => [],
  318. }];
  319. log.format = log.formats.find(function MARKER(f){
  320. if(!f.detector.test(new Error().stack)) return false;
  321. //console.log('////', f.name, 'wants', 0/*line*/, '\n' + new Error().stack);
  322. return true;
  323. });
  324. core.initialize();
  325. if(window === top && console.timeEnd) console.timeEnd(SCRIPTID);
  326. })();