Greasy Fork 还支持 简体中文。

cs.android.com 优化

用于 cs.android.com 的优化

  1. // ==UserScript==
  2. // @name cs.android.com 优化
  3. // @namespace Violentmonkey Scripts
  4. // @match https://cs.android.com/*
  5. // @grant none
  6. // @run-at document-idle
  7. // @version 1.2
  8. // @author 5ec1cff
  9. // @description 用于 cs.android.com 的优化
  10. // @license MIT
  11. // ==/UserScript==
  12.  
  13. let buttonsRoot = null;
  14.  
  15. const observer = new MutationObserver(function(mutationsList, observe) {
  16. mutationsList.forEach(l => {
  17. l.addedNodes?.forEach(e => {
  18. if (e.nodeType != 1) return;
  19. if (e.tagName == 'BREADCRUMB') {
  20. buttonsRoot = e;
  21. installButtons(location.href);
  22. }
  23. })
  24. })
  25. });
  26. observer.observe(document.body, { 'childList': true, 'subtree': true });
  27.  
  28. const platformJumpList = {
  29. latest: "superproject/+/android-latest-release",
  30. 16: "superproject/+/android-16.0.0_r2",
  31. a15qpr2: "superproject/+/android15-qpr2-release",
  32. U: "superproject/+/android-14.0.0_r2",
  33. T: "superproject/+/android-13.0.0_r3",
  34. Sv2: "superproject/+/android-12.1.0_r27",
  35. S: "superproject/+/android-12.0.0_r3",
  36. R: "superproject/+/android-11.0.0_r21",
  37. Main: "superproject/main/+/main",
  38. Master: "superproject/+/master",
  39. "Master-Main": "superproject/+/main",
  40. Q: "superproject/+/android-10.0.0_r47",
  41. P: "superproject/+/android-9.0.0_r61",
  42. O_MR1: "superproject/+/android-8.1.0_r81", // 8.1, 27
  43. O: "superproject/+/android-8.0.0_r36", // 8.0, 26
  44. N_MR1: "superproject/+/android-7.1.2_r39", // 7.1, 25
  45. N: "superproject/+/android-7.0.0_r7", // 7.0, 24
  46. M: "superproject/+/android-6.0.1_r9", // 6, 23
  47. };
  48.  
  49. const kernelJumpList = {
  50. '12-5.10': 'superproject/+/common-android12-5.10',
  51. '13-5.10': 'superproject/+/common-android13-5.10',
  52. '13-5.15': 'superproject/+/common-android13-5.15',
  53. '14-5.15': 'superproject/+/common-android14-5.15',
  54. '14-6.1': 'superproject/+/common-android14-6.1',
  55. '15-6.6': 'superproject/+/common-android15-6.6',
  56. 'main': 'superproject/+/common-android-mainline',
  57. }
  58.  
  59. function getJump(url, to) {
  60. if (url.match(/android\/platform\/superproject$/)) url += '/+/android-latest-release:';
  61. else if (url.match(/android\/platform\/superproject\/main$/)) url += '/+/main:';
  62. else if (url.match(/android\/kernel\/superproject$/)) url += '/+/common-android-mainline:';
  63. return url.replace(/(?<=android\/)(.*)(?=:)/, to);
  64. }
  65.  
  66. function installButtons(urlstr) {
  67. if (buttonsRoot == null) return;
  68. Array.from(buttonsRoot.querySelectorAll('.jumpbtn')).forEach(x=>x.remove());
  69. let url = new URL(urlstr);
  70. if (url.pathname.match(/^\/android\/platform\//)) {
  71. for (const item in platformJumpList) {
  72. const v = platformJumpList[item];
  73. let btn = document.createElement("a");
  74. btn.textContent = item;
  75. btn.href = getJump(urlstr, 'platform/' + v);
  76. btn.style = "margin-right: 1em;";
  77. btn.classList.add("jumpbtn");
  78. buttonsRoot.appendChild(btn);
  79. }
  80. } else if (url.pathname.match(/^\/android\/kernel\//)) {
  81. for (const item in kernelJumpList) {
  82. const v = kernelJumpList[item];
  83. let btn = document.createElement("a");
  84. btn.textContent = item;
  85. btn.href = getJump(urlstr, 'kernel/' + v);
  86. btn.style = "margin-right: 1em;";
  87. btn.classList.add("jumpbtn");
  88. buttonsRoot.appendChild(btn);
  89. }
  90. }
  91. }
  92.  
  93. navigation.addEventListener('navigate', (e) => {
  94. console.log('orig', location.href);
  95. console.log('dest', e.destination.url);
  96. installButtons(e.destination.url);
  97. })
  98.  
  99. // 2025-04-08:增加 diff 的提交+行号链接跳转
  100.  
  101. function installDiffLineLinkListener() {
  102. let last = null;
  103. let f = (e) => {
  104. if (last == e.srcElement) return;
  105. if (e.srcElement?.parentElement == last) return;
  106. if (
  107. e?.srcElement?.tagName == "DIV" &&
  108. e?.srcElement?.classList?.contains?.("CodeMirror-linenumber")
  109. ) {
  110. last = e.srcElement;
  111. try {
  112. let p = last.parentElement;
  113. let url = null,
  114. prefix,
  115. commit;
  116. let filepath = document.querySelector(
  117. "#skiplink-navigation-target"
  118. )?.textContent;
  119. let diffPage = false;
  120. if (filepath == null) {
  121. // in diff page
  122. let p = last.parentElement;
  123. while (p) {
  124. if (p?.classList?.contains("mat-expansion-panel-content-wrapper")) {
  125. filepath = p.previousSibling?.querySelector("a")?.innerText;
  126. diffPage = true;
  127. break;
  128. }
  129. p = p.parentElement;
  130. }
  131. }
  132. if (filepath == null) return;
  133. let line = last.textContent;
  134. while (p) {
  135. if (p?.classList?.contains("CodeMirror-merge-left")) {
  136. if (!diffPage) {
  137. url = document.querySelector(".left-diff a").href;
  138. } else {
  139. let pos = location.href.lastIndexOf("/");
  140. let prefix = location.href.substring(0, pos);
  141. let commits = location.href.substring(pos + 1);
  142. url = prefix + "/" + commits.split("...")[0];
  143. }
  144. break;
  145. } else if (
  146. p?.classList?.contains("CodeMirror-merge-pane-rightmost")
  147. ) {
  148. if (!diffPage) {
  149. url = document.querySelector(".right-diff a").href;
  150. } else {
  151. let pos = location.href.lastIndexOf("/");
  152. let prefix = location.href.substring(0, pos);
  153. let commits = location.href.substring(pos + 1);
  154. url = prefix + "/" + commits.split("...")[1];
  155. }
  156. break;
  157. }
  158. p = p?.parentElement;
  159. }
  160. let p1 = url.lastIndexOf("/");
  161. prefix = url.substring(0, p1);
  162. commit = url.substring(p1 + 1);
  163. url = `${prefix}/${commit}:${filepath};l=${line}`;
  164. // console.log(url);
  165. let a = document.createElement("a");
  166. a.href = url;
  167. // a.terget = "_blank";
  168. a.textContent = line;
  169. last.replaceChild(a, last.childNodes[0]);
  170. } catch (err) {
  171. console.error(last, err);
  172. }
  173. } else {
  174. try {
  175. if (last)
  176. last.replaceChild(new Text(last.textContent), last.childNodes[0]);
  177. } catch (err) {
  178. console.error(err);
  179. }
  180. last = null;
  181. }
  182. };
  183. addEventListener("mousemove", f);
  184. }
  185.  
  186. installDiffLineLinkListener();