YouTube Better Window Title

Add video length (rounded) and Channel Name to Window Title

当前为 2022-09-02 提交的版本,查看 最新版本

  1. // ==UserScript==
  2. // @name YouTube Better Window Title
  3. // @namespace http://borisjoffe.com
  4. // @version 1.2.5
  5. // @description Add video length (rounded) and Channel Name to Window Title
  6. // @author Boris Joffe
  7. // @match https://*.youtube.com/watch?*
  8. // @grant unsafeWindow
  9. // ==/UserScript==
  10.  
  11. // UPDATES:
  12. // 2022-09-02 - fix date selector - change from #info... to #description
  13. // 2022-01-07 - fix '#date' selector not working anymore
  14. // 2021-05-24 - fix channel name to avoid duplicate text
  15. // 2021-05-07 - fix newlines and extra whitespace in channel name
  16. // 2021-01-13 - add zim wiki link when double clicking date
  17. // 2020-07-21 - fix bug where clicking on new videos doesn't update title (due to using initial player data instead of DOM)
  18.  
  19. /*
  20. The MIT License (MIT)
  21.  
  22. Copyright (c) 2018, 2020, 2021, 2022 Boris Joffe
  23.  
  24. Permission is hereby granted, free of charge, to any person obtaining a copy
  25. of this software and associated documentation files (the "Software"), to deal
  26. in the Software without restriction, including without limitation the rights
  27. to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  28. copies of the Software, and to permit persons to whom the Software is
  29. furnished to do so, subject to the following conditions:
  30.  
  31. The above copyright notice and this permission notice shall be included in
  32. all copies or substantial portions of the Software.
  33.  
  34. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  35. IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  36. FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  37. AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  38. LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  39. OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  40. THE SOFTWARE.
  41. */
  42.  
  43. /* jshint -W097, -W041 */
  44. /* eslint-disable no-console, no-unused-vars */
  45. 'use strict';
  46.  
  47.  
  48. // Util
  49. const DEBUG = false;
  50. function dbg() {
  51. if (DEBUG)
  52. console.log.apply(console, arguments);
  53.  
  54. return arguments[0];
  55. }
  56.  
  57.  
  58. var
  59. qs = document.querySelector.bind(document),
  60. err = console.error.bind(console),
  61. log = console.log.bind(console),
  62. euc = encodeURIComponent;
  63.  
  64. function qsv(elmStr, parent) {
  65. var elm = parent ? parent.querySelector(elmStr) : qs(elmStr);
  66. if (!elm) err('(qs) Could not get element -', elmStr);
  67. return elm;
  68. }
  69.  
  70. function getProp(obj, path, defaultValue) {
  71. path = Array.isArray(path) ? Array.from(path) : path.split('.');
  72. var prop = obj;
  73.  
  74. while (path.length && obj) {
  75. prop = obj[path.shift()];
  76. }
  77.  
  78. return prop != null ? prop : defaultValue;
  79. }
  80.  
  81. function getWindowTitle() { return document.title; }
  82.  
  83. function setWindowTitle(newTitle) {
  84. document.title = newTitle;
  85. log('newTitle =', newTitle);
  86. }
  87.  
  88. function getVideoLengthSeconds() {
  89. return qsv('.ytp-progress-bar').getAttribute('aria-valuemax')
  90. // return unsafeWindow.ytInitialPlayerResponse.videoDetails.lengthSeconds;
  91. }
  92.  
  93. function getVideoLengthFriendly() {
  94. // TODO: update
  95. return Math.round(getVideoLengthSeconds() / 60) + 'm';
  96. }
  97.  
  98. function getChannelName() {
  99. return qsv('#channel-name a').innerText.replaceAll('\n', '').trim()
  100. // return unsafeWindow.ytInitialPlayerResponse.videoDetails.author;
  101. }
  102.  
  103. function getChannelNameShort() {
  104. return getChannelName().substr(0, 20);
  105. }
  106.  
  107. function getVideoTitle() {
  108. return qsv('.title.ytd-video-primary-info-renderer').innerText
  109. // return unsafeWindow.ytInitialPlayerResponse.videoDetails.title;
  110. }
  111.  
  112. function getVideoTitleShort() {
  113. return getVideoTitle()//.substr(0, 30);
  114. }
  115.  
  116. function updateWindowTitle() {
  117. dbg('updateWindowTitle()');
  118. var videoLength = getVideoLengthFriendly();
  119. var channelName = getChannelNameShort();
  120. var videoTitle = getVideoTitleShort();
  121.  
  122. // setWindowTitle([videoLength, channelName, videoTitle].join('—'));
  123. setWindowTitle([videoLength + ',' + channelName, videoTitle].join('—'));
  124. setTimeout(updateWindowTitle, (DEBUG ? 5000 : 5000));
  125. //isTitleUpdated = true;
  126. }
  127.  
  128. function getVideoDate() {
  129. return getProp(qsv('#date'), 'innerText', '').trim() || qsv('meta[itemprop="uploadDate"]').getAttribute('content')
  130. }
  131.  
  132. function getVideoYear() {
  133. const dateArr = getVideoDate().split(',')
  134. return dateArr[dateArr.length - 1].trim()
  135. }
  136.  
  137. function createWikiLink() {
  138. return '[[' + window.location.href + '|' + getVideoTitle() + ']] - '
  139. + getChannelName() + ', ' + getVideoYear() + ', ' + getVideoLengthFriendly()
  140. }
  141.  
  142. function $createWikiLink() {
  143. /*
  144. qsv('#info.ytd-video-primary-info-renderer').lastChild.innerHTML +=
  145. '<div class="style-scope ytd-video-primary-info-renderer" style="color: white">'
  146. + createWikiLink()
  147. + '</div>'
  148. */
  149.  
  150. navigator.clipboard.writeText(createWikiLink())
  151. }
  152.  
  153.  
  154. var isTitleUpdated = false;
  155. function waitForLoad() {
  156. log('waitForLoad');
  157. if (isTitleUpdated) return;
  158.  
  159. //dbg(unsafeWindow.ytInitialPlayerResponse, 'unsafeWindow.ytInitialPlayerResponse')
  160.  
  161. if (! unsafeWindow.ytInitialPlayerResponse) {
  162. log('waiting another 2 sec for ytInitialPlayerResponse');
  163. setTimeout(waitForLoad, 2000);
  164. return;
  165. }
  166.  
  167. //dbg('video details:', unsafeWindow.ytInitialPlayerResponse.videoDetails);
  168.  
  169. //setTimeout(function () {
  170. // TODO: can setTimeout be removed?
  171. // log('e.target =', e.target);
  172. //var innerHtml = e.target.innerHTML;
  173. // log('innerHTML length =', innerHtml.length);
  174. // TODO: delete below check (and replace qsv with qs?) - it's ugly
  175. //if (innerHtml.length > 150 || !innerHtml.includes('Add to calendar'))
  176. // return; // quick return to avoid multiple querySelectors
  177. //if (qsv('.event-description') && qsv('a[href*="google.com/calendar"]')) {
  178. log('video title =', getVideoTitleShort());
  179. updateWindowTitle();
  180. //}
  181. //}, 20);
  182.  
  183. // update selector to #description
  184. qsv('#description').addEventListener('dblclick', $createWikiLink, true)
  185.  
  186. }
  187.  
  188. setTimeout(function () {
  189. waitForLoad();
  190. }, 5000);
  191. // window eventListener doesn't work well for some reason
  192. // window.addEventListener('load', waitForLoad, true);
  193. log('youtube better window title')