GreasyFork: 导航栏增强

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

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

  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://greasyfork.org/*
  10. // @match https://sleazyfork.org/*
  11. // @grant none
  12. // @version 0.3.1.7
  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. // ==/UserScript==
  22.  
  23. (async () => {
  24. let sections = [
  25. { id: '#user-script-sets-section' },
  26. { id: '#control-panel' },
  27. // { id: '#user-library-list-section', name: '库' },
  28. //{ id: '#user-unlisted-script-list-section', name: '没上架' },
  29. // { id: '#user-discussions', name: '讨论' },
  30. { id: '#user-script-list-section' }
  31. ]
  32.  
  33. function preSetup() {
  34. let pos = document.querySelectorAll('#site-nav>nav>li.with-submenu')
  35. pos = pos.length >= 1 ? pos[pos.length - 1] : null
  36.  
  37. if (!pos) return
  38.  
  39. pos.parentNode.style.minHeight = '2.8rem'
  40.  
  41. return { pos }
  42. }
  43.  
  44. function setup(m, namespace) {
  45. const { cpmRoot } = m
  46.  
  47. let h = cpmRoot.querySelector('h3') || cpmRoot.querySelector('header')
  48. if (!h) return
  49.  
  50. let nav = document.createElement('nav')
  51.  
  52. let addedText = new Set()
  53. let lastText = null
  54.  
  55. for (const anchor of cpmRoot.querySelectorAll('li a[href]')) {
  56. let textContent = anchor.textContent.trim()
  57.  
  58. if (addedText.has(textContent)) {
  59. lastText = textContent
  60. continue
  61. }
  62.  
  63. let li = nav.appendChild(document.createElement('li'))
  64. li.appendChild(anchor)
  65.  
  66. addedText.add(textContent)
  67. }
  68.  
  69. if (lastText !== null) {
  70. nav.querySelectorAll('li').forEach(li => {
  71. if (li.querySelector('a').textContent.trim() === lastText) {
  72. li.remove()
  73. }
  74. })
  75. }
  76.  
  77. let tm = document.createElement('template')
  78. tm.innerHTML = `
  79. <li class="with-submenu" style="display: block;">
  80. <a href="#" onclick="return false">${namespace ? namespace : h.textContent}</a>
  81. <nav style="min-width: initial;">
  82. ${nav.innerHTML}
  83. </nav>
  84. </li>
  85. `.trim()
  86.  
  87. return tm.content
  88. }
  89.  
  90. function bufferToHex(buffer) {
  91. const byteArray = new Uint8Array(buffer)
  92. const len = byteArray.length
  93. const hexCodes = new Array(len * 2)
  94. const chars = '0123456789abcdef'
  95. for (let i = 0, j = 0; i < len; i++) {
  96. const byte = byteArray[i]
  97. hexCodes[j++] = chars[byte >> 4]
  98. hexCodes[j++] = chars[byte & 0x0F]
  99. }
  100. return hexCodes.join('')
  101. }
  102.  
  103. async function digestMessage(message) {
  104. const encoder = new TextEncoder("utf-8")
  105. const msgUint8 = encoder.encode(message)
  106. const hashBuffer = await crypto.subtle.digest('SHA-1', msgUint8)
  107. return bufferToHex(hashBuffer)
  108. }
  109.  
  110. async function fetchHTML(href) {
  111. let response = await fetch(href, {
  112. method: "GET",
  113. mode: "same-origin",
  114. cache: "force-cache",
  115. credentials: "same-origin",
  116. redirect: "follow",
  117. referrerPolicy: "no-referrer",
  118. })
  119.  
  120. return response.text()
  121. }
  122.  
  123. async function addSectionsToNav() {
  124. let presetup = preSetup()
  125. if (!presetup) return
  126. const { pos } = presetup
  127.  
  128. let plink = document.querySelector('.user-profile-link')
  129. if (!plink) return
  130. let href = plink.querySelector('a[href*="/users/"]').href
  131. if (href.includes('/users/sign')) return
  132.  
  133. let dm = await digestMessage(href)
  134. const stKey = `gf_user_page_${dm}`
  135.  
  136. for (let trialN = 8; trialN--;) {
  137. let s = sessionStorage.getItem(stKey)
  138. let d = typeof s === 'string' ? parseInt(s) : 0
  139. if (d > 9 && Date.now() - d < 8000) await new Promise(r => setTimeout(r, 320))
  140. else break
  141. }
  142.  
  143. const userPageHTML = await fetchHTML(href)
  144. if (!userPageHTML || typeof userPageHTML !== 'string') return
  145.  
  146. sessionStorage.setItem(stKey, userPageHTML)
  147.  
  148. let template = document.createElement('template')
  149. template.innerHTML = userPageHTML
  150. const content = template.content
  151.  
  152.  
  153.  
  154. sections.forEach(({ id, name }) => {
  155. let section = content.querySelector(id)
  156. if (section) {
  157. const kc = setup({ cpmRoot: section, pos }, name)
  158. if (kc) {
  159. pos.parentNode.insertBefore(kc, pos.nextSibling)
  160. }
  161. }
  162. })
  163. }
  164.  
  165. if (!document.querySelector('.sign-out-link') || document.querySelector('.sign-in-link')) return
  166.  
  167. await addSectionsToNav()
  168. })()