Apple Developer Documentation - UI Cleanup

Make the Apple Developer Documentation more user friendly with links to each subsection

目前为 2020-07-23 提交的版本。查看 最新版本

  1. // ==UserScript==
  2. // @name Apple Developer Documentation - UI Cleanup
  3. // @namespace https://www.raisen.dev/
  4. // @version 0.1
  5. // @description Make the Apple Developer Documentation more user friendly with links to each subsection
  6. // @author Raisen
  7. // @match https://developer.apple.com/documentation/*
  8. // @grant none
  9. // ==/UserScript==
  10.  
  11. (function() {
  12. 'use strict';
  13.  
  14. var origPushState = history.pushState;
  15.  
  16. var numTriesChangeHeader = 0;
  17. function changeHeader() {
  18. const retryOrLeave = () => {
  19. if(numTriesChangeHeader++ < 3) {
  20. return window.setTimeout(changeHeader, 1000);
  21. }
  22. }
  23. const currentItem = document.querySelector('.current.item');
  24. if(!currentItem) return retryOrLeave();
  25.  
  26. currentItem.style.cursor = 'pointer';
  27. currentItem.addEventListener('click', e => {
  28. e.preventDefault();
  29. e.stopPropagation();
  30. window.scrollTo(0,0);
  31. });
  32. document.querySelector('main > div.topictitle').style.width = '80%';
  33. document.querySelector('main > div.row').style.width = '80%';
  34. }
  35.  
  36. var numTriesRun = 0;
  37. function run() {
  38. const retryOrLeave = () => {
  39. if(numTriesRun++ < 3) {
  40. return window.setTimeout(run, 1000);
  41. }
  42. console.warn('giving up');
  43. }
  44. const h3s = document.querySelectorAll('h3.title');
  45. if(!h3s) { return retryOrLeave(); }
  46.  
  47. const summary = document.querySelector('.summary-section nav:last-child ul.summary-list');
  48. if(!summary) { return retryOrLeave(); }
  49.  
  50. const li = summary.querySelector('li');
  51. if(!li) { return retryOrLeave(); }
  52.  
  53. h3s.forEach((h3,ix) => {
  54. const li2 = li.cloneNode(true);
  55. li2.querySelector('span').textContent = h3.textContent;
  56. const _a = li2.querySelector('a')
  57. const a = _a.cloneNode(true);
  58. a.setAttribute('href', '#');
  59. a.addEventListener('click', e => {
  60. e.stopPropagation();
  61. e.preventDefault();
  62. h3.classList.add('header' + ix);
  63. window.scrollBy(h3.getBoundingClientRect().x, h3.getBoundingClientRect().y - 100);
  64. origPushState.apply(history, [{}, "", "#header" + ix]);
  65. return true;
  66. });
  67. _a.parentNode.replaceChild(a, _a);
  68.  
  69. summary.appendChild(li2);
  70. });
  71.  
  72. document.querySelectorAll('.summary-section nav:last-child ul.summary-list a').forEach(a => a.classList.remove('link'));
  73. }
  74. window.addEventListener('hashchange', () => {
  75. if(!window.location.hash) {
  76. window.scrollTo(0,0);
  77. }
  78. let matches = window.location.hash.match(/#header(\d*)/);
  79. if(matches && matches.length === 2) {
  80. let ix = matches[1];
  81. const h3 = document.querySelector('.header' + ix);
  82. window.scrollBy(h3.getBoundingClientRect().x, h3.getBoundingClientRect().y - 100);
  83. }
  84. });
  85. run();
  86. changeHeader();
  87.  
  88. history.pushState = function(...args) {
  89. numTriesRun = 0;
  90. numTriesChangeHeader = 0;
  91. origPushState.apply(history, args);
  92. setTimeout(() => {
  93. run();
  94. changeHeader();
  95. }, 500);
  96. }
  97.  
  98. })();

QingJ © 2025

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