Reddit - Subs - default sort

Apply default sort to subs listing (SH bug)

当前为 2025-01-22 提交的版本,查看 最新版本

  1. // ==UserScript==
  2. // @name Reddit - Subs - default sort
  3. // @namespace https://github.com/Procyon-b
  4. // @version 0.4
  5. // @description Apply default sort to subs listing (SH bug)
  6. // @author Achernar
  7. // @match https://www.reddit.com/*
  8. // @run-at document-start
  9. // @grant none
  10. // ==/UserScript==
  11.  
  12. (function() {
  13. "use strict";
  14.  
  15. var sort={};
  16.  
  17. const RE=/^(?:https:\/\/(?:sh|www)\.reddit\.com)?(\/(?:r\/[^\/]+\/)?)(?:(best|new|hot|top|rising)\/)?(?:\?.*)?$/;
  18. const REval=/^(best|new|hot|top|rising)?$/;
  19. var SR=[];
  20.  
  21. var done=false;
  22. function init() {
  23. if (done) return;
  24. done=true;
  25. document && document.body && chk();
  26. newObs(document.body);
  27. window.addEventListener('focus', function(){
  28. if (load()) {
  29. chk(null, true);
  30. updLeftNav();
  31. }
  32. });
  33.  
  34. if (window == top) {
  35. var e=document.createElement('div');
  36. e.style='position: fixed; top: 0; left: 0; width: 15px; height: 15px; z-index: 9999;';
  37. document.body.appendChild(e);
  38. e.ondblclick=function(){
  39. var p;
  40.  
  41. do {
  42. p=prompt('Default sort\nChoose between: [empty] - hot - new - top - rising', sort.default);
  43. if (p == null) break;
  44. } while (!REval.test(p));
  45.  
  46. if (p != null) {
  47. if (p) sort.default=p;
  48. else delete sort.default;
  49. store();
  50. chk(null, true);
  51. updLeftNav();
  52. }
  53.  
  54. p=prompt('Auto-redirect to correct sort ?\nEither: [empty field] - true - force - ask', sort.redir);
  55. if (p != null) {
  56. if (p) sort.redir = p;
  57. else delete sort.redir;
  58. store();
  59. }
  60.  
  61. };
  62. }
  63. }
  64.  
  65. function gen(re) {
  66. var s=sort[re[1]] || '';
  67. if (s) s+='/';
  68. else if ((re[1] != '/') && sort.default) s=sort.default+'/';
  69. return s;
  70. }
  71.  
  72. function chk(r, force=false) {
  73. var a=(r||document).getElementsByTagName("a");
  74. if (r && (r.nodeName == 'A')) a=[r, ...a];
  75. for (let i=0,e; e=a[i]; i++) {
  76. if (!force && e._sortfixed) continue;
  77. e._sortfixed=true;
  78. if (e.hostname == 'www.reddit.com') {
  79. let re;
  80. if (re=RE.exec(e.pathname + (!e.pathname.endsWith('/') ? '/':'' ) )) {
  81. if (e.closest('[slot="dropdown-items"], faceplate-dropdown-menu')) continue;
  82. e.classList.add('_marked_');
  83. let s=gen(re);
  84. e.pathname=re[1]+s;
  85. }
  86. }
  87. }
  88. a=(r||document).querySelectorAll('pdp-back-button[subreddit-prefixed-name]');
  89. for (let i=0,e; e=a[i]; i++) {
  90. if (!force && e._sortfixed) continue;
  91. e._sortfixed=true;
  92. let re, v=e.attributes['subreddit-prefixed-name'].value;
  93. if (re=RE.exec('/'+v+'/')) {
  94. e.classList.add('_marked_');
  95. let s=gen(re);
  96. e.attributes['subreddit-prefixed-name'].value=(re[1]+s).replace(/^\/(.+)\/$/, "$1");
  97. }
  98. }
  99. }
  100.  
  101. var AS=HTMLElement.prototype.attachShadow;
  102. HTMLElement.prototype.attachShadow=function(m){/*[native */
  103. var e=this;
  104. let sr=AS.call(e,m);
  105.  
  106. if (e.tagName == 'REDDIT-RECENT-PAGES') {
  107. SR.push(sr);
  108. newObs(sr, cbRec);
  109. e.SR=true;
  110. }
  111. else if (e.tagName.startsWith('LEFT-NAV-') || (e.tagName == 'SHREDDIT-SUBREDDIT-HEADER') || (e.tagName == 'MOD-NAV') ) {
  112. SR.push(sr);
  113. newObs(sr);
  114. e.SR=true;
  115. }
  116. return sr;
  117. }
  118.  
  119. function updLeftNav() {
  120. for(let r, i=0; r=SR[i]; i++) {
  121. for(let e, j=0; e=r.children[j]; j++) {
  122. chk(e, true);
  123. }
  124. }
  125. }
  126.  
  127. function ds(sub, s) {
  128. let d= sub == '/' ? 'best' : (sort.default?'DEF':'');
  129. return s == d ? '' : s;
  130. }
  131.  
  132. function patchFetch() {
  133. // XHR - Fetch
  134. const _fetch=window.fetch;
  135. window.fetch = async (...args) => {
  136. let [resource, config ] = args;
  137. let response = await _fetch(resource, config);
  138. let re;
  139. if ( re=RE.exec(resource) ) {
  140. let s=ds(re[1], re[2]);
  141. if (s != ds(re[1],sort[re[1]] || '') ) {
  142. if ( (re[1] != '/') && sort.default && (s == sort.default) ) s='';
  143. if (s) sort[re[1]] = s;
  144. else delete sort[re[1]];
  145. store();
  146. chk(null, true);
  147. updLeftNav();
  148. }
  149. }
  150. return response;
  151. };
  152. }
  153.  
  154. function newObs(r, f=cb) {
  155. var o=new MutationObserver(f), config = { attributes: false, childList: true, subtree: true};
  156. o.observe(r, config);
  157. return o;
  158. }
  159.  
  160. function cb(mutL) {
  161. for(let mut of mutL) {
  162. if (mut.type == 'childList') {
  163. for (let e,i=0; e=mut.addedNodes[i]; i++) {
  164. if (e.nodeType == 1) chk(e);
  165. }
  166. }
  167. }
  168. }
  169.  
  170. function cbRec(mutL) {
  171. for(let mut of mutL) {
  172. if (mut.type == 'childList') {
  173. for (let e,i=0; e=mut.addedNodes[i]; i++) {
  174. if (e.nodeType == 1) {
  175. let r=e.closest('details');
  176. if (r) chk(r, true);
  177. else chk(e);
  178. }
  179. }
  180. }
  181. }
  182. }
  183.  
  184. function load() {
  185. var o=sort.upd;
  186. sort=JSON.parse(localStorage.getItem('_sort_') || '{}');
  187. if (o != sort.upd) return true;
  188. }
  189.  
  190. function store() {
  191. sort.upd=Date.now();
  192. localStorage.setItem('_sort_', JSON.stringify(sort) );
  193. }
  194.  
  195. patchFetch();
  196. load();
  197.  
  198. if (document.readyState != 'loading') init();
  199. else {
  200. document.addEventListener('DOMContentLoaded', init);
  201. window.addEventListener('load', init);
  202. }
  203.  
  204. var redir=sort.redir;
  205.  
  206. if (redir) {
  207. let re;
  208. if (re = RE.exec(location.pathname)) {
  209. if ( !re[2] || (redir == 'force') ) {
  210. let l=re[1]+gen(re);
  211. if (l != location.pathname) {
  212. if (redir == 'ask') {
  213. if ( (window == top) && (window.document.visibilityState == 'visible') && confirm('Redirect to correct order ?\n'+location.href+'\n'+l) ) redir=true;
  214. else redir=false;
  215. }
  216. if (redir) location.pathname=l;
  217. }
  218. }
  219. }
  220. }
  221.  
  222. })();