Change YouTube Leftbar Subscription Links To Channel/User Video Page

Change YouTube leftbar's subscription links to channel/user video page. This script can optionally also move updated links to top of the list (if there's enough space), optionally uncollapse all updated links which may extend the non collapsible links, and optionally display the number of updated & total links following the "SUBSCRIPTION" section label. All features can be enabled/disabled from the script code. For new YouTube layout only.

当前为 2021-08-15 提交的版本,查看 最新版本

  1. // ==UserScript==
  2. // @name Change YouTube Leftbar Subscription Links To Channel/User Video Page
  3. // @namespace ChangeYouTubeLeftbarSubscriptionLinksToChannelUserVideoPage
  4. // @version 1.3.19
  5. // @license AGPL v3
  6. // @author jcunews
  7. // @description Change YouTube leftbar's subscription links to channel/user video page. This script can optionally also move updated links to top of the list (if there's enough space), optionally uncollapse all updated links which may extend the non collapsible links, and optionally display the number of updated & total links following the "SUBSCRIPTION" section label. All features can be enabled/disabled from the script code. For new YouTube layout only.
  8. // @website https://greasyfork.org/en/users/85671-jcunews
  9. // @match *://www.youtube.com/*
  10. // @grant none
  11. // @run-at document-start
  12. // ==/UserScript==
  13.  
  14. (yigd => {
  15.  
  16. //===== Configuration Start ===
  17.  
  18. var changeLinksURL = true; //Change links' URLs from the Home tab, to the Videos tab.
  19. var moveUpdatedLinksToTop = true; //Move updated links to top of list without uncollapsing them.
  20. var UncollapseUpdatedLinks = true; //Uncollapse updated links. This may extend the length of uncollapsed links.
  21. //If enabled, it will implicitly enable moveUpdatedLinksToTop.
  22. var showLinksCount = true; //Display number of updated links and total links following the "SUBSCRIPTION" section label.
  23.  
  24. //===== Configuration End ===
  25.  
  26. function changeUrl(url, m) {
  27. if (m = url.match(/^\/feed\/subscriptions\/([^\/]+)$/)) {
  28. return "/channel/" + m[1] + "/videos";
  29. } else return url + "/videos";
  30. }
  31.  
  32. function updateSubscriptionCount(a, u, t) {
  33. function count(v, w) {
  34. if ((w = v.guideEntryRenderer) && w.entryData) {
  35. t++;
  36. if (w.presentationStyle === "GUIDE_ENTRY_PRESENTATION_STYLE_NEW_CONTENT") u++;
  37. } else if ((w = v.guideCollapsibleEntryRenderer) && (w = w.expandableItems) && w.forEach) {
  38. w.forEach(count)
  39. }
  40. }
  41. if (!window.ytlssc_ujs && (a = document.querySelector("ytd-guide-section-renderer:nth-child(2) #guide-section-title"))) {
  42. a.insertAdjacentHTML("beforeend", ' (<span id=ytlssc_ujs>-/-</span>)')
  43. }
  44. if (window.ytlssc_ujs) {
  45. u = 0; t = 0;
  46. if (a && (a = a.parentNode.parentNode.__data) && (a = a.shownItems) && a.forEach) a.forEach(count);
  47. ytlssc_ujs.textContent = u + "/" + t
  48. } else setTimeout(updateSubscriptionCount, 500)
  49. }
  50.  
  51. function updateEndpoint(ep) {
  52. ep.commandMetadata.webCommandMetadata.url = changeUrl(ep.commandMetadata.webCommandMetadata.url);
  53. if (ep.webNavigationEndpointData) ep.webNavigationEndpointData.url = changeUrl(ep.webNavigationEndpointData.url);
  54. (function waitGD(gd) {
  55. if ((gd = window["guide-renderer"].__data) && (gd = gd.data) && (gd = gd.items) && (gd = gd.reduce(
  56. (r, v) => {
  57. if (!r && (v = v.guideSubscriptionsSectionRenderer) && (v = v.items)) {
  58. r = v.reduce((r, e) => {
  59. if (e.guideCollapsibleEntryRenderer) {
  60. e.guideCollapsibleEntryRenderer.expandableItems.forEach(t => {
  61. if (!t.guideEntryRenderer.icon || (t.guideEntryRenderer.icon.iconType !== "ADD_CIRCLE")) r.push(t.guideEntryRenderer.navigationEndpoint)
  62. });
  63. } else r.push(e.guideEntryRenderer.navigationEndpoint);
  64. return r;
  65. }, []);
  66. }
  67. return r;
  68. }, null
  69. ))) {
  70. fetch(location.protocol + "//" + location.host + ep.commandMetadata.webCommandMetadata.url, {credentials: "omit"}).then(r => r.text().then((h, o) => {
  71. if ((h = h.match(/window\["ytInitialData"\] = (\{.*?)\};\n/)) && (h = JSON.parse(h[1] + "}").contents.twoColumnBrowseResultsRenderer.tabs)) {
  72. h.some(a => {
  73. if ((/^\/[^\/]+\/[^\/]+\/videos$/).test((a = a.tabRenderer.endpoint).commandMetadata.webCommandMetadata.url)) {
  74. ep.browseEndpoint.params = a.browseEndpoint.params;
  75. ep.commandMetadata.webCommandMetadata.url = a.commandMetadata.webCommandMetadata.url;
  76. if (ep.webNavigationEndpointData) ep.webNavigationEndpointData.url = a.commandMetadata.webCommandMetadata.url;
  77. gd.some(d => {
  78. if (d.browseEndpoint.browseId === a.browseEndpoint.browseId) {
  79. d.browseEndpoint.params = a.browseEndpoint.params;
  80. d.commandMetadata.webCommandMetadata.url = a.commandMetadata.webCommandMetadata.url;
  81. if (d.webNavigationEndpointData) d.webNavigationEndpointData.url = a.commandMetadata.webCommandMetadata.url;
  82. return true;
  83. }
  84. });
  85. return true;
  86. }
  87. })
  88. }
  89. }))
  90. } else setTimeout(waitGD, 100)
  91. })()
  92. }
  93.  
  94. function patchGuide(guide, z) {
  95. if (guide && guide.items && !guide.cysl_done) try {
  96. guide.cysl_done = 1;
  97. guide.items.forEach((v, vc, l, w, c, i, u) => {
  98. if (v.guideSubscriptionsSectionRenderer) {
  99. //change links' URL
  100. if (changeLinksURL) {
  101. v.guideSubscriptionsSectionRenderer.items.forEach(w => {
  102. if (w.guideCollapsibleEntryRenderer) {
  103. w.guideCollapsibleEntryRenderer.expandableItems.forEach(x => {
  104. if (x.guideEntryRenderer.badges && (x = x.guideEntryRenderer.navigationEndpoint)) updateEndpoint(x);
  105. });
  106. } else if (w = w.guideEntryRenderer.navigationEndpoint) updateEndpoint(w);
  107. });
  108. }
  109. //move links with new uploads to top
  110. if (moveUpdatedLinksToTop) {
  111. v = v.guideSubscriptionsSectionRenderer.items; //v = main links container. includes collapsed links container wrapper at end
  112. vc = v.length - 1; //vc = main links count
  113. w = v[vc].guideCollapsibleEntryRenderer; //w = collapsed links container wrapper
  114. l = v.splice(0, vc); //l = list1. move main links into list1. list1 now: mainLinks
  115. c = -1;
  116. if (w) { //has collapsed links container? w = collapsed links container
  117. (w = w.expandableItems).some((e, i) => { //count collapsed links
  118. if (!e.guideEntryRenderer.entryData) {
  119. c = i;
  120. return true;
  121. }
  122. });
  123. l.push.apply(l, w.splice(0, c)); //append collapsed links to list1. list1 now: mainLinks, collapsedLinks
  124. }
  125. c = []; //c = list2 = collapsed new links
  126. for (i = l.length - 1; i >= 0; i--) { //move new links in list1 into main links container
  127. u = l[i].guideEntryRenderer.presentationStyle === "GUIDE_ENTRY_PRESENTATION_STYLE_NEW_CONTENT";
  128. if (l[i].guideEntryRenderer.count || u) {
  129. if ((u = UncollapseUpdatedLinks && u) || vc--) {
  130. v.unshift(l.splice(i, 1)[0]);
  131. if (u) vc--;
  132. } else c.unshift(l.splice(i, 1)[0]);
  133. }
  134. }
  135. c.push.apply(c, l); //append any remaining list1 links (non updated links) into list2
  136. if (vc > 0) { //original-length main links container still has free slot?
  137. l = c.splice(0, vc); //move collapsed links in list2 into list1. same count as main links container free slots
  138. l.unshift.apply(l, [v.length - 1, 0]); //prepare arguments for below task
  139. v.splice.apply(v, l); //move collapsed links in list1 into main links container
  140. }
  141. if (w) w.unshift.apply(w, c); //if has collapsed links container, move remaining collapsed links in list2 into it
  142. }
  143. }
  144. });
  145. updateSubscriptionCount();
  146. return true;
  147. } catch(z) {}
  148. return false;
  149. }
  150.  
  151. Object.defineProperty(window, "ytInitialGuideData", {
  152. get(v) {
  153. return yigd;
  154. },
  155. set(v) {
  156. delete window.ytInitialGuideData;
  157. patchGuide(v);
  158. return yigd = v;
  159. }
  160. });
  161.  
  162. JSON.parse_cylslcvp = JSON.parse;
  163. JSON.parse = function(txt) {
  164. var res = JSON.parse_cylslcvp.apply(this, arguments);
  165. if ((/\/youtubei\/v1\/guide\?/).test(JSON.url_cylslcvp)) patchGuide(res);
  166. return res;
  167. };
  168.  
  169. var fetch_ = window.fetch;
  170. window.fetch = function(opts) {
  171. var fres = fetch_.apply(this, arguments), fthen = fres.then;
  172. if ((/\/youtubei\/v1\/guide\?/).test(opts.url)) {
  173. JSON.url_cylslcvp = opts.url;
  174. fres.then = function(fn) {
  175. var fthenfn = fn;
  176. fn = function(fresp) {
  177. var frtext = fresp.text;
  178. fresp.text = function() {
  179. var tres = frtext.apply(this, arguments), tthen = tres.then;
  180. tres.then = function(tfn) {
  181. var tthenfn = tfn;
  182. tfn = function(tresp) {
  183. var z, tr;
  184. try {
  185. tr = JSON.parse_cylslcvp(tresp);
  186. patchGuide(tr);
  187. tresp = JSON.stringify(tr);
  188. } catch(z) {}
  189. return tthenfn.apply(this, arguments);
  190. };
  191. return tthen.apply(this, arguments);
  192. };
  193. return tres;
  194. }
  195. var frjson = fresp.json;
  196. fresp.json = function() {
  197. var jres = frjson.apply(this, arguments), jthen = jres.then;
  198. jres.then = function(jfn) {
  199. var jthenfn = jfn;
  200. jfn = function(jresp) {
  201. patchGuide(jresp);
  202. return jthenfn.apply(this, arguments);
  203. };
  204. return jthen.apply(this, arguments);
  205. };
  206. return jres;
  207. }
  208. return fthenfn.apply(this, arguments);
  209. };
  210. return fthen.apply(this, arguments);
  211. };
  212. } else JSON.url_cylslcvp = "";
  213. return fres;
  214. };
  215.  
  216. addEventListener("click", (ev, a) => {
  217. if ((a = ev.target) && a.matches && a.matches("ytd-guide-section-renderer:nth-child(2) ytd-guide-entry-renderer *")) {
  218. while (a.tagName !== "A") a = a.parentNode;
  219. if (/^\/(c|channel|u|user)\//.test(a.pathname)) {
  220. a = a.__dataHost.__data.data.navigationEndpoint;
  221. ev.stopImmediatePropagation();
  222. ev.preventDefault();
  223. a.browseEndpoint.params = encodeURIComponent(btoa("\x12\x06videos"));
  224. a.browseEndpoint.canonicalBaseUrl = a.commandMetadata.webCommandMetadata.url.substring(0, a.commandMetadata.webCommandMetadata.url.lastIndexOf("/"));
  225. nav.navigate(a, false, undefined, {});
  226. }
  227. }
  228. }, true);
  229.  
  230. })();