GreasyFork: 导航栏增强

在导航栏上添加用户列表,控制台,收藏等..

目前为 2024-08-10 提交的版本。查看 最新版本

  1. // ==UserScript==
  2. // @name GreasyFork: User Control Panel Button
  3. // @name:en GreasyFork: User Control Panel Button
  4. // @name:zh-CN GreasyFork: 导航栏增强
  5. // @name:zh-TW GreasyFork: 導航欄增強
  6. // @name:ko GreasyFork: 네비게이션 바 향상
  7. // @name:ja GreasyFork: ナビゲーションバー強化
  8. // @namespace https://github.com/10086100886
  9. // @match https://gf.qytechs.cn/*
  10. // @match https://sleazyfork.org/*
  11. // @grant none
  12. // @version 0.3.1.18
  13. // @license MIT
  14. // @author CY Fung & 人民的勤务员 <toniaiwanowskiskr47@gmail.com>
  15. // @description To add User Control Panel Button into navigation bar
  16. // @description:en To add User Control Panel Button into navigation bar
  17. // @description:zh-CN 在导航栏上添加用户列表,控制台,收藏等..
  18. // @description:ko 네비게이션 바에 사용자 목록, 콘솔, 즐겨찾기 등을 추가...
  19. // @description:ja ナビゲーションバーにユーザーリスト、コンソール、お気に入りなどを追加...
  20. // @description:zh-TW 在導航欄上添加使用者列表、控制台、收藏等...
  21. // @icon https://gf.qytechs.cn/vite/assets/blacklogo96-CxYTSM_T.png
  22. // ==/UserScript==
  23.  
  24. (async () => {
  25. let sections = [
  26. { id: '#user-script-sets-section' },
  27. { id: '#control-panel' },
  28. // { id: '#user-library-list-section', name: '库' },
  29. //{ id: '#user-unlisted-script-list-section', name: '没上架' },
  30. // { id: '#user-discussions', name: '讨论' },
  31. { id: '#user-script-list-section' }
  32. ]
  33.  
  34. function preSetup() {
  35. let pos = document.querySelectorAll('#site-nav>nav>li.with-submenu')
  36. pos = pos.length >= 1 ? pos[pos.length - 1] : null
  37.  
  38. if (!pos) return
  39.  
  40. pos.parentNode.style.minHeight = '2.8rem'
  41.  
  42. return { pos }
  43. }
  44.  
  45. function setup(m, namespace) {
  46. const { cpmRoot } = m
  47.  
  48. let h = cpmRoot.querySelector('h3') || cpmRoot.querySelector('header')
  49. if (!h) return
  50.  
  51. let nav = document.createElement('nav')
  52.  
  53. let addedText = new Set()
  54. let lastText = null
  55.  
  56. for (const anchor of cpmRoot.querySelectorAll('li a[href]')) {
  57. let textContent = anchor.textContent.trim()
  58.  
  59. if (addedText.has(textContent)) {
  60. lastText = textContent
  61. continue
  62. }
  63.  
  64. let li = nav.appendChild(document.createElement('li'))
  65. li.appendChild(anchor)
  66.  
  67. addedText.add(textContent)
  68. }
  69.  
  70. if (lastText !== null) {
  71. nav.querySelectorAll('li').forEach(li => {
  72. if (li.querySelector('a').textContent.trim() === lastText) {
  73. li.remove()
  74. }
  75. })
  76. }
  77.  
  78. let tm = document.createElement('template')
  79. tm.innerHTML = `
  80. <li class="with-submenu" style="display: block;">
  81. <a href="#" onclick="return false">${namespace ? namespace : h.textContent}</a>
  82. <nav style="min-width: initial;">
  83. ${nav.innerHTML}
  84. </nav>
  85. </li>
  86. `.trim()
  87.  
  88. return tm.content
  89. }
  90.  
  91. function bufferToHex(buffer) {
  92. const byteArray = new Uint8Array(buffer)
  93. const len = byteArray.length
  94. const hexCodes = new Array(len * 2)
  95. const chars = '0123456789abcdef'
  96. for (let i = 0, j = 0; i < len; i++) {
  97. const byte = byteArray[i]
  98. hexCodes[j++] = chars[byte >> 4]
  99. hexCodes[j++] = chars[byte & 0x0F]
  100. }
  101. return hexCodes.join('')
  102. }
  103.  
  104. async function digestMessage(message) {
  105. const encoder = new TextEncoder("utf-8")
  106. const msgUint8 = encoder.encode(message)
  107. const hashBuffer = await crypto.subtle.digest('SHA-1', msgUint8)
  108. return bufferToHex(hashBuffer)
  109. }
  110.  
  111. async function fetchHTML(href) {
  112. let response = await fetch(href, {
  113. method: "GET",
  114. mode: "same-origin",
  115. cache: "force-cache",
  116. credentials: "same-origin",
  117. redirect: "follow",
  118. referrerPolicy: "no-referrer",
  119. })
  120.  
  121. return response.text()
  122. }
  123.  
  124. async function addSectionsToNav() {
  125. let presetup = preSetup()
  126. if (!presetup) return
  127. const { pos } = presetup
  128.  
  129. let plink = document.querySelector('.user-profile-link')
  130. if (!plink) return
  131. let href = plink.querySelector('a[href*="/users/"]').href
  132. if (href.includes('/users/sign')) return
  133.  
  134. let dm = await digestMessage(href)
  135. const stKey = `gf_user_page_${dm}`
  136.  
  137. for (let trialN = 8; trialN--;) {
  138. let s = sessionStorage.getItem(stKey)
  139. let d = typeof s === 'string' ? parseInt(s) : 0
  140. if (d > 9 && Date.now() - d < 8000) await new Promise(r => setTimeout(r, 320))
  141. else break
  142. }
  143.  
  144. const userPageHTML = await fetchHTML(href)
  145. if (!userPageHTML || typeof userPageHTML !== 'string') return
  146.  
  147. sessionStorage.setItem(stKey, userPageHTML)
  148.  
  149. let template = document.createElement('template')
  150. template.innerHTML = userPageHTML
  151. const content = template.content
  152.  
  153.  
  154.  
  155. sections.forEach(({ id, name }) => {
  156. let section = content.querySelector(id)
  157. if (section) {
  158. const kc = setup({ cpmRoot: section, pos }, name)
  159. if (kc) {
  160. pos.parentNode.insertBefore(kc, pos.nextSibling)
  161. }
  162. }
  163. })
  164. }
  165.  
  166. if (!document.querySelector('.sign-out-link') || document.querySelector('.sign-in-link')) return
  167.  
  168. await addSectionsToNav()
  169. })()

QingJ © 2025

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