cs.android.com 优化

cs.android.com 优化描述不能与名称相同

目前為 2025-04-08 提交的版本,檢視 最新版本

  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.0
  8. // @author 5ec1cff
  9. // @description cs.android.com 优化描述不能与名称相同
  10. // @license MIT
  11. // ==/UserScript==
  12.  
  13. // 2023/12/1 11:59:54
  14. const observer = new MutationObserver(function(mutationsList, observe) {
  15. mutationsList.forEach(l => {
  16. l.addedNodes?.forEach(e => {
  17. if (e.nodeType != 1) return;
  18. if (e.tagName == 'PATH-BREADCRUMB') {
  19. if (!location.pathname.match(/^\/android\/platform\//)) return;
  20. installButtons(e);
  21. }
  22. })
  23. })
  24. });
  25.  
  26. observer.observe(document.body, { 'childList': true, 'subtree': true });
  27.  
  28. const jumpList = {
  29. 'a15qpr2': 'superproject/+/android15-qpr2-release',
  30. 'U': 'superproject/+/android-14.0.0_r2',
  31. 'T': 'superproject/+/android-13.0.0_r3',
  32. 'Sv2': 'superproject/+/android-12.1.0_r27',
  33. 'S': 'superproject/+/android-12.0.0_r3',
  34. 'R': 'superproject/+/android-11.0.0_r21',
  35. 'Main': 'superproject/main/+/main',
  36. 'Master': 'superproject/+/master',
  37. 'Master-Main': 'superproject/+/main',
  38. 'Q': 'superproject/+/android-10.0.0_r47',
  39. 'P': 'superproject/+/android-9.0.0_r61',
  40. 'O_MR1': 'superproject/+/android-8.1.0_r81', // 8.1, 27
  41. 'O': 'superproject/+/android-8.0.0_r36', // 8.0, 26
  42. 'N_MR1': 'superproject/+/android-7.1.2_r39', // 7.1, 25
  43. 'N': 'superproject/+/android-7.0.0_r7', // 7.0, 24
  44. 'M': 'superproject/+/android-6.0.1_r9', // 6, 23
  45. }
  46.  
  47. function getJump(to) {
  48. return location.href.replace(/(?<=platform\/)(.*)(?=:)/, to)
  49. }
  50.  
  51. function installButtons(root) {
  52. for (const item in jumpList) {
  53. const v = jumpList[item];
  54. let btn = document.createElement('a');
  55. btn.textContent = item;
  56. btn.href = getJump(v);
  57. btn.style = 'margin-right: 1em;';
  58. root.appendChild(btn)
  59. }
  60. }
  61.  
  62. // 2025-04-08:增加 diff 的提交+行号链接跳转
  63.  
  64. function installDiffLineLinkListener() {
  65. let last = null;
  66. let f = (e) => {
  67. if (last == e.srcElement) return;
  68. if (e.srcElement?.parentElement == last) return;
  69. if (
  70. e?.srcElement?.tagName == "DIV" &&
  71. e?.srcElement?.classList?.contains?.("CodeMirror-linenumber")
  72. ) {
  73. last = e.srcElement;
  74. try {
  75. let p = last.parentElement;
  76. let url = null, prefix, commit;
  77. let filepath = document.querySelector(
  78. "#skiplink-navigation-target"
  79. )?.textContent;
  80. let diffPage = false;
  81. if (filepath == null) {
  82. // in diff page
  83. let p = last.parentElement;
  84. while (p) {
  85. if (p?.classList?.contains('mat-expansion-panel-content-wrapper')) {
  86. filepath = p.previousSibling?.querySelector('a')?.innerText;
  87. diffPage = true;
  88. break;
  89. }
  90. p = p.parentElement;
  91. }
  92. }
  93. if (filepath == null) return;
  94. let line = last.textContent;
  95. while (p) {
  96. if (p?.classList?.contains("CodeMirror-merge-left")) {
  97. if (!diffPage) {
  98. url = document.querySelector(".left-diff a").href;
  99. } else {
  100. let pos = location.href.lastIndexOf('/');
  101. let prefix = location.href.substring(0, pos);
  102. let commits = location.href.substring(pos + 1);
  103. url = prefix + '/' + commits.split('...')[0];
  104. }
  105. break;
  106. } else if (
  107. p?.classList?.contains("CodeMirror-merge-pane-rightmost")
  108. ) {
  109. if (!diffPage) {
  110. url = document.querySelector(".right-diff a").href;
  111. } else {
  112. let pos = location.href.lastIndexOf('/');
  113. let prefix = location.href.substring(0, pos);
  114. let commits = location.href.substring(pos + 1);
  115. url = prefix + '/' + commits.split('...')[1];
  116. }
  117. break;
  118. }
  119. p = p?.parentElement;
  120. }
  121. let p1 = url.lastIndexOf("/");
  122. prefix = url.substring(0, p1);
  123. commit = url.substring(p1 + 1);
  124. url = `${prefix}/${commit}:${filepath};l=${line}`;
  125. // console.log(url);
  126. let a = document.createElement("a");
  127. a.href = url;
  128. a.terget = "_blank";
  129. a.textContent = line;
  130. last.replaceChild(a, last.childNodes[0]);
  131. } catch (err) {
  132. console.error(last, err);
  133. }
  134. } else {
  135. try {
  136. if (last)
  137. last.replaceChild(new Text(last.textContent), last.childNodes[0]);
  138. } catch (err) {
  139. console.error(err);
  140. }
  141. last = null;
  142. }
  143. };
  144. addEventListener("mousemove", f);
  145. }
  146.  
  147. installDiffLineLinkListener()
  148.