Github回到顶部

Github return to the top(回到顶部)脚本

  1. // ==UserScript==
  2. // @name Github回到顶部
  3. // @namespace http://tampermonkey.net/
  4. // @version 1.0.0
  5. // @description Github return to the top(回到顶部)脚本
  6. // @author yangyang (https://wanzij.cn)
  7. // @match https://github.com/*
  8. // @match http://github.com/*
  9. // @icon https://www.google.com/s2/favicons?sz=64&domain=github.com
  10. // @include http://github.com/*
  11. // @include https://github.com/*
  12. // @grant none
  13. // @license MIT
  14. // ==/UserScript==
  15.  
  16. (function() {
  17. 'use strict';
  18. //------- 回到顶部
  19.  
  20. function scrollToTop() {
  21. const scrollDuration = 300 // 滚动持续时间(毫秒)
  22. const scrollStep = -window.scrollY / (scrollDuration / 15)
  23. const scrollInterval = setInterval(() => {
  24. if (window.scrollY !== 0) {
  25. window.scrollBy(0, scrollStep)
  26. } else {
  27. clearInterval(scrollInterval)
  28. }
  29. }, 15)
  30. }
  31. function topTxt(){
  32. let text = 'return to the top' //lang en
  33. switch (navigator.language.toLowerCase()) {
  34. case 'zh':
  35. case 'zh-cn':
  36. case 'zh-sg':
  37. text = '回到顶部'
  38. break;
  39. case 'zh-tw':
  40. case 'zh-hk':
  41. text = '回到頂部'
  42. break;
  43. case 'ko':
  44. case 'ko_kr':
  45. text = '맨 위로 돌아가기'
  46. break;
  47. case 'ja':
  48. case 'ja_jp':
  49. text = 'トップに戻る'
  50. break
  51. case 'fr':
  52. case 'fr_be':
  53. case 'fr_ca':
  54. case 'fr_ch':
  55. case 'fr_fr':
  56. case 'fr_lu':
  57. text = 'retour au sommet'
  58. break
  59. default:
  60. break;
  61. }
  62. return text
  63. }
  64. //-------添加回到顶部按钮
  65. function addGoTopBtn() {
  66. const jumpContainer = document.createElement('div');
  67. jumpContainer.className = 'jump-container';
  68. jumpContainer.title=topTxt();
  69. jumpContainer.style.display = 'none';
  70. const svgIcon = document.createElementNS('http://www.w3.org/2000/svg', 'svg');
  71. svgIcon.setAttribute('t', '1696584351737');
  72. svgIcon.setAttribute('class', 'icon');
  73. svgIcon.setAttribute('viewBox', '0 0 1024 1024');
  74. svgIcon.setAttribute('version', '1.1');
  75. svgIcon.setAttribute('xmlns', 'http://www.w3.org/2000/svg');
  76. svgIcon.setAttribute('width', '32');
  77. svgIcon.setAttribute('height', '32');
  78.  
  79. const path = document.createElementNS('http://www.w3.org/2000/svg', 'path');
  80. path.setAttribute('d', 'M372.306898 792.431381v45.303732H296.992643v185.950064h-46.775464v-186.014052H176.95055v-45.303732H372.306898z m377.851042-1.279766c38.392993 0 100.909584 20.796205 98.478028 81.073204-2.30358 57.58949-55.925794 74.098477-94.638729 75.378243h-38.201028v76.082115H670.492479v-232.59755h79.729449z m-240.020196 0a116.522735 116.522735 0 1 1 0 232.661539 114.283143 114.283143 0 0 1-85.296434-31.418266 115.56291 115.56291 0 0 1-30.970348-84.848515 115.690886 115.690886 0 0 1 30.970348-85.168457 114.475108 114.475108 0 0 1 85.296434-31.290289z m0 46.51951a69.299353 69.299353 0 0 0-52.27846 17.468812 70.387154 70.387154 0 0 0-17.468812 52.406436 69.939236 69.939236 0 0 0 17.596789 52.150482 68.787446 68.787446 0 0 0 52.150483 17.596789 70.387154 70.387154 0 0 0 52.406435-17.596789 69.619294 69.619294 0 0 0 17.468812-52.278459 69.875248 69.875248 0 0 0-17.468812-52.406436 69.683283 69.683283 0 0 0-52.342447-17.468812z m206.874245 0v63.540404h31.994161c33.017974 0 52.598401-11.197956 53.302272-30.842371 0.703872-17.084882-18.620602-30.586418-45.623673-32.378091h-39.608772zM481.982882 7.550622a25.595329 25.595329 0 0 1 36.281378 0l367.420946 367.420946a25.595329 25.595329 0 0 1-17.852742 43.576047H691.736602a25.595329 25.595329 0 0 0-25.595329 25.595329v274.445914a25.595329 25.595329 0 0 1-25.595329 25.595328h-275.149785a25.595329 25.595329 0 0 1-25.595329-25.595328V444.206932a25.595329 25.595329 0 0 0-25.595329-25.595329H156.090357a25.595329 25.595329 0 0 1-18.428637-43.576047z');
  81. path.setAttribute('p-id', '5736');
  82. path.setAttribute('fill', '#8a8a8a');
  83.  
  84. svgIcon.appendChild(path);
  85.  
  86. const sideServiceItemIcon = document.createElement('div');
  87. sideServiceItemIcon.className = 'side-service-item-icon';
  88. sideServiceItemIcon.appendChild(svgIcon);
  89.  
  90. jumpContainer.appendChild(sideServiceItemIcon);
  91. document.body.appendChild(jumpContainer);
  92. jumpContainer.addEventListener('click', scrollToTop);
  93.  
  94. // 添加样式
  95. const style = document.createElement('style');
  96. style.innerHTML = `
  97. .jump-container {
  98. background: #fff;
  99. border-radius: 100%;
  100. bottom: 170px;
  101. box-shadow: 0 3px 12px rgba(0, 36, 153, .06);
  102. font-size: 14px;
  103. height: 56px;
  104. margin-top: 16px;
  105. position: fixed;
  106. right: 12px;
  107. width: 56px;
  108. z-index: 3001;
  109. cursor: pointer;
  110. }
  111.  
  112. .jump-container .side-service-item-icon {
  113. align-items: center;
  114. border-radius: 56px;
  115. color: #8a8a8a;
  116. display: flex;
  117. flex-direction: column;
  118. height: 100%;
  119. justify-content: center;
  120. margin: 0;
  121. position: relative;
  122. text-align: center;
  123. width: auto;
  124. }
  125.  
  126. .jump-container .side-service-item-icon svg {
  127. position: relative;
  128. }
  129. `;
  130. document.head.appendChild(style);
  131. }
  132. function checkScroll() {
  133. const scrollHeight = window.pageYOffset || document.documentElement.scrollTop
  134. if (scrollHeight >= 350) {
  135. document.querySelector('.jump-container').style.display = 'block';
  136. } else {
  137. document.querySelector('.jump-container').style.display = 'none';
  138. }
  139. }
  140. addGoTopBtn();
  141. window.removeEventListener('scroll', checkScroll);
  142. window.addEventListener('scroll', checkScroll)
  143. })();

QingJ © 2025

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