YouTube: Add Channel Name to Shorts Thumbnail

To add channel name to Shorts thumbnail

当前为 2024-04-13 提交的版本,查看 最新版本

  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: Add Channel Name to Shorts Thumbnail
  28. // @namespace UserScript
  29. // @match https://www.youtube.com/*
  30. // @grant none
  31. // @version 0.2.2
  32. // @license MIT License
  33. // @author CY Fung
  34. // @description To add channel name to Shorts thumbnail
  35. // @require https://cdn.jsdelivr.net/gh/cyfung1031/userscript-supports@8fac46500c5a916e6ed21149f6c25f8d1c56a6a3/library/ytZara.js
  36. // @run-at document-start
  37. // ==/UserScript==
  38.  
  39.  
  40. ((__CONTEXT__) => {
  41.  
  42. const { Promise, fetch } = __CONTEXT__;
  43.  
  44. class Mutex {
  45.  
  46. constructor() {
  47. this.p = Promise.resolve()
  48. }
  49.  
  50. /**
  51. * @param {(lockResolve: () => void)} f
  52. */
  53. lockWith(f) {
  54. this.p = this.p.then(() => new Promise(f).catch(console.warn))
  55. }
  56.  
  57. }
  58.  
  59.  
  60. const addCSSProcess = () => {
  61.  
  62. if (document.querySelector('style#ePCWh')) return;
  63.  
  64.  
  65. let style = document.createElement('style')
  66. style.id = 'ePCWh'
  67. style.textContent = `
  68.  
  69. #metadata-line[ePCWh]::before {
  70. width: 100%;
  71. content: attr(ePCWh);
  72. display: block;
  73. max-width: 100%;
  74. text-overflow: ellipsis;
  75. overflow: hidden;
  76. white-space: nowrap;
  77. }
  78. `;
  79. document.head.appendChild(style);
  80.  
  81. }
  82.  
  83. const { requestVideoInfoAsync } = (() => {
  84.  
  85. const mutex = new Mutex();
  86.  
  87. const resolvedValues = new Map();
  88.  
  89. const createPromise = (videoId) => {
  90.  
  91. return new Promise(resolve => {
  92.  
  93. mutex.lockWith(lockResolve => {
  94.  
  95. fetch(`/watch?v=${videoId}`, {
  96.  
  97. "method": "GET",
  98. "mode": "same-origin",
  99. "credentials": "omit",
  100. referrerPolicy: "no-referrer",
  101. cache: "default",
  102. redirect: "error", // there shall be no redirection in this API request
  103. integrity: "",
  104. keepalive: false,
  105.  
  106. "headers": {
  107. "Cache-Control": "public, max-age=900, stale-while-revalidate=1800",
  108. // refer "Cache-Control Use Case Examples" in https://www.koyeb.com/blog/using-cache-control-and-cdns-to-improve-performance-and-reduce-latency
  109. // seems YouTube RSS Feeds server insists its own Cache-Control.
  110.  
  111. // "Content-Type": "text/xml; charset=UTF-8",
  112. "Accept-Encoding": "gzip, deflate, br", // YouTube Response - gzip
  113. // X-Youtube-Bootstrap-Logged-In: false,
  114. // X-Youtube-Client-Name: 1, // INNERTUBE_CONTEXT_CLIENT_NAME
  115. // X-Youtube-Client-Version: "2.20230622.06.00" // INNERTUBE_CONTEXT_CLIENT_VERSION
  116.  
  117. "Accept": "text/html",
  118. "Pragma": ""
  119. }
  120.  
  121. }).then(res => {
  122. lockResolve();
  123. return res.text();
  124. }).then(resText => {
  125.  
  126. let wIdx2 = resText.indexOf('itemprop="author"');
  127. let wIdx1 = wIdx2 > 0 ? resText.lastIndexOf('<span', wIdx2) : -1;
  128. let wIdx3 = wIdx1 > 0 ? resText.indexOf('<\/span>', wIdx2) : -1;
  129. if (wIdx3 > 0) {
  130.  
  131. let mText = resText.substring(wIdx1, wIdx3 + '<\/span>'.length);
  132. let template = document.createElement('template');
  133. template.innerHTML = mText;
  134. let span = template.content.firstElementChild;
  135. if (span && span.nodeName === "SPAN") {
  136. let name = span.querySelector('link[itemprop="name"]')
  137. if (name) {
  138. name = name.getAttribute('content');
  139. if (name) {
  140. return name;
  141. }
  142. }
  143. }
  144. template.innerHTML = '';
  145. }
  146.  
  147. return '';
  148.  
  149. }).then(resolve);
  150.  
  151. })
  152.  
  153. });
  154.  
  155. };
  156.  
  157. const requestVideoInfoAsync = (videoId) => {
  158.  
  159. let promise = resolvedValues.get(videoId);
  160. if (!promise) {
  161. promise = createPromise(videoId);
  162. resolvedValues.set(videoId, promise);
  163. }
  164. return promise;
  165. }
  166.  
  167. return { requestVideoInfoAsync };
  168.  
  169. })();
  170.  
  171.  
  172. ytZara.ytProtoAsync("ytd-rich-grid-slim-media").then((cProto) => {
  173.  
  174. Promise.resolve().then(addCSSProcess);
  175.  
  176. const onDataChanged = cProto.onDataChanged;
  177.  
  178. cProto.onDataChanged = function () {
  179. const cnt = this;
  180. const hostElement = this.hostElement || this;
  181. let nameToAdd = '';
  182. const data = cnt.data;
  183. if (data && data.videoType === "REEL_VIDEO_TYPE_VIDEO" && typeof data.videoId === 'string') {
  184. const videoId = data.videoId;
  185. if (data.rsVideoId === videoId && typeof data.rsChannelName === 'string') {
  186. nameToAdd = data.rsChannelName;
  187. } else {
  188. requestVideoInfoAsync(videoId).then(name => {
  189. cnt.data = Object.assign({}, cnt.data, { rsVideoId: videoId, rsChannelName: name });
  190. });
  191. }
  192. }
  193.  
  194. const details = ((this.$ || 0).details || 0);
  195.  
  196. let ml = details ? HTMLElement.prototype.querySelector.call(details, '#details #metadata-line') : HTMLElement.prototype.querySelector.call(hostElement, 'ytd-rich-grid-slim-media #details #metadata-line');
  197. if (ml) {
  198. if (nameToAdd) {
  199. ml.setAttribute('ePCWh', nameToAdd);
  200. } else if (ml.hasAttribute('ePCWh')) {
  201. ml.removeAttribute('ePCWh');
  202. }
  203. }
  204.  
  205. return onDataChanged ? onDataChanged.apply(cnt, arguments) : void 0;
  206. };
  207.  
  208. });
  209.  
  210.  
  211. ytZara.ytProtoAsync("ytd-reel-item-renderer").then((cProto) => {
  212.  
  213. Promise.resolve().then(addCSSProcess);
  214.  
  215. const onVisible = cProto.onVisible;
  216. cProto.onVisible = function () {
  217. const cnt = this;
  218. const hostElement = this.hostElement || this;
  219. let nameToAdd = '';
  220. const data = cnt.data;
  221. if (data && data.videoType === "REEL_VIDEO_TYPE_VIDEO" && typeof data.videoId === 'string') {
  222. const videoId = data.videoId;
  223. if (data.rsVideoId === videoId && typeof data.rsChannelName === 'string') {
  224. nameToAdd = data.rsChannelName;
  225. } else {
  226. requestVideoInfoAsync(videoId).then(name => {
  227. cnt.data = Object.assign({}, cnt.data, { rsVideoId: videoId, rsChannelName: name });
  228. });
  229. }
  230. }
  231.  
  232. const details = ((this.$ || 0).details || 0);
  233.  
  234. let ml = details ? HTMLElement.prototype.querySelector.call(details, '#details #metadata-line') : HTMLElement.prototype.querySelector.call(hostElement, 'ytd-reel-item-renderer #details #metadata-line');
  235. if (ml) {
  236. if (nameToAdd) {
  237. ml.setAttribute('ePCWh', nameToAdd);
  238. } else if (ml.hasAttribute('ePCWh')) {
  239. ml.removeAttribute('ePCWh');
  240. }
  241. }
  242.  
  243. return onVisible ? onVisible.apply(cnt, arguments) : void 0;
  244. };
  245.  
  246. });
  247.  
  248.  
  249. })({ Promise, fetch });