YouTube: Add Channel Name to Shorts Thumbnail

To add channel name to Shorts thumbnail

目前為 2023-07-04 提交的版本,檢視 最新版本

  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.1.7
  32. // @license MIT License
  33. // @author CY Fung
  34. // @description To add channel name to Shorts thumbnail
  35. // @require https://gf.qytechs.cn/scripts/465819-api-for-customelements-in-youtube/code/API%20for%20CustomElements%20in%20YouTube.js?version=1215161
  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. customYtElements.whenRegistered('ytd-rich-grid-slim-media', (proto) => {
  61.  
  62. const mutex = new Mutex();
  63.  
  64. const resolvedValues = new Map();
  65.  
  66. Promise.resolve().then(() => {
  67.  
  68. if (document.querySelector('style#ePCWh')) return;
  69.  
  70.  
  71. let style = document.createElement('style')
  72. style.id = 'ePCWh'
  73. style.textContent = `
  74.  
  75. #metadata-line[ePCWh]::before {
  76. width: 100%;
  77. content: attr(ePCWh);
  78. display: block;
  79. max-width: 100%;
  80. text-overflow: ellipsis;
  81. overflow: hidden;
  82. white-space: nowrap;
  83. }
  84. `;
  85. document.head.appendChild(style);
  86.  
  87. });
  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. proto.onDataChanged = ((onDataChanged) => {
  158.  
  159. return function () {
  160. let nameToAdd = '';
  161. const data = this.data;
  162. if (data && data.videoType === "REEL_VIDEO_TYPE_VIDEO" && typeof data.videoId === 'string') {
  163. const videoId = data.videoId;
  164. if (data.rsVideoId === videoId && typeof data.rsChannelName === 'string') {
  165. nameToAdd = data.rsChannelName;
  166. } else {
  167. let promise = resolvedValues.get(videoId);
  168. if (!promise) {
  169. promise = createPromise(videoId);
  170. resolvedValues.set(videoId, promise);
  171. }
  172. promise.then(name => {
  173. this.data = Object.assign({}, this.data, { rsVideoId: videoId, rsChannelName: name });
  174. });
  175. }
  176. }
  177.  
  178. let ml = HTMLElement.prototype.querySelector.call(this, 'ytd-rich-grid-slim-media #details #metadata-line');
  179. if (ml) {
  180. if (nameToAdd) {
  181. ml.setAttribute('ePCWh', nameToAdd);
  182. } else if (ml.hasAttribute('ePCWh')) {
  183. ml.removeAttribute('ePCWh');
  184. }
  185. }
  186.  
  187. return onDataChanged.apply(this, arguments);
  188. };
  189.  
  190. })(proto.onDataChanged)
  191.  
  192. });
  193.  
  194. })({ Promise, fetch });

QingJ © 2025

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