Greasyfork - 为脚本添加备注(别名/标签)

为脚本添加备注(别名/标签)功能,以帮助识别和搜索

当前为 2023-02-27 提交的版本,查看 最新版本

  1. // ==UserScript==
  2. // @name Greasyfork - Add notes to the script
  3. // @name:zh-CN Greasyfork - 为脚本添加备注(别名/标签)
  4. // @name:zh-TW Greasyfork - 為指令碼新增備註(別名/標籤)
  5. // @namespace https://greasyfork.org/zh-CN/users/193133-pana
  6. // @homepage https://greasyfork.org/zh-CN/users/193133-pana
  7. // @icon data:image/svg+xml;base64,PHN2ZyByb2xlPSJpbWciIHhtbG5zPSJodHRwOi8vd3d3LnczLm9yZy8yMDAwL3N2ZyIgd2lkdGg9IjI0cHgiIGhlaWdodD0iMjRweCIgdmlld0JveD0iMCAwIDI0IDI0IiBhcmlhLWxhYmVsbGVkYnk9Im5ld0ljb25UaXRsZSIgc3Ryb2tlPSJyZ2JhKDI5LDE2MSwyNDIsMS4wMCkiIHN0cm9rZS13aWR0aD0iMiIgc3Ryb2tlLWxpbmVjYXA9InNxdWFyZSIgc3Ryb2tlLWxpbmVqb2luPSJtaXRlciIgZmlsbD0ibm9uZSIgY29sb3I9InJnYmEoMjksMTYxLDI0MiwxLjAwKSI+IDx0aXRsZSBpZD0ibmV3SWNvblRpdGxlIj5OZXc8L3RpdGxlPiA8cGF0aCBkPSJNMTkgMTRWMjJIMi45OTk5N1Y0SDEzIi8+IDxwYXRoIGQ9Ik0xNy40NjA4IDQuMDM5MjFDMTguMjQxOCAzLjI1ODE3IDE5LjUwODIgMy4yNTgxNiAyMC4yODkyIDQuMDM5MjFMMjAuOTYwOCA0LjcxMDc5QzIxLjc0MTggNS40OTE4NCAyMS43NDE4IDYuNzU4MTcgMjAuOTYwOCA3LjUzOTIxTDExLjU4NTggMTYuOTE0MkMxMS4yMTA3IDE3LjI4OTMgMTAuNzAyIDE3LjUgMTAuMTcxNiAxNy41TDcuNSAxNy41TDcuNSAxNC44Mjg0QzcuNSAxNC4yOTggNy43MTA3MSAxMy43ODkzIDguMDg1NzkgMTMuNDE0MkwxNy40NjA4IDQuMDM5MjFaIi8+IDxwYXRoIGQ9Ik0xNi4yNSA1LjI1TDE5Ljc1IDguNzUiLz4gPC9zdmc+
  8. // @version 3.0.0
  9. // @description Add notes (aliases/tags) for scripts to help identify and search
  10. // @description:zh-CN 为脚本添加备注(别名/标签)功能,以帮助识别和搜索
  11. // @description:zh-TW 為指令碼新增備註(別名/標籤)功能,以幫助識別和搜尋
  12. // @author pana
  13. // @license GNU General Public License v3.0 or later
  14. // @compatible chrome
  15. // @compatible firefox
  16. // @match *://*.greasyfork.org/*
  17. // @match *://*.sleazyfork.org/*
  18. // @require https://gcore.jsdelivr.net/gh/LightAPIs/greasy-fork-library@05dffeb4eefb1a39df31d518cd45a4e6929929f3/Note_Obj.js
  19. // @noframes
  20. // @grant GM_info
  21. // @grant GM_getValue
  22. // @grant GM_setValue
  23. // @grant GM_deleteValue
  24. // @grant GM_listValues
  25. // @grant GM_openInTab
  26. // @grant GM_addStyle
  27. // @grant GM_registerMenuCommand
  28. // @grant GM_unregisterMenuCommand
  29. // @grant GM_addValueChangeListener
  30. // @grant GM_removeValueChangeListener
  31. // ==/UserScript==
  32.  
  33. (function () {
  34. 'use strict';
  35. const UPDATED = '2023-02-27';
  36. const GF_ICON = {
  37. NOTE_BLACK: 'url(data:image/svg+xml;base64,PHN2ZyByb2xlPSJpbWciIHhtbG5zPSJodHRwOi8vd3d3LnczLm9yZy8yMDAwL3N2ZyIgd2lkdGg9IjI0cHgiIGhlaWdodD0iMjRweCIgdmlld0JveD0iMCAwIDI0IDI0IiBhcmlhLWxhYmVsbGVkYnk9Im5ld0ljb25UaXRsZSIgc3Ryb2tlPSJyZ2IoMzgsIDM4LCAzOCkiIHN0cm9rZS13aWR0aD0iMiIgc3Ryb2tlLWxpbmVjYXA9InNxdWFyZSIgc3Ryb2tlLWxpbmVqb2luPSJtaXRlciIgZmlsbD0ibm9uZSIgY29sb3I9InJnYigzOCwgMzgsIDM4KSI+IDx0aXRsZSBpZD0ibmV3SWNvblRpdGxlIj5OZXc8L3RpdGxlPiA8cGF0aCBkPSJNMTkgMTRWMjJIMi45OTk5N1Y0SDEzIi8+IDxwYXRoIGQ9Ik0xNy40NjA4IDQuMDM5MjFDMTguMjQxOCAzLjI1ODE3IDE5LjUwODIgMy4yNTgxNiAyMC4yODkyIDQuMDM5MjFMMjAuOTYwOCA0LjcxMDc5QzIxLjc0MTggNS40OTE4NCAyMS43NDE4IDYuNzU4MTcgMjAuOTYwOCA3LjUzOTIxTDExLjU4NTggMTYuOTE0MkMxMS4yMTA3IDE3LjI4OTMgMTAuNzAyIDE3LjUgMTAuMTcxNiAxNy41TDcuNSAxNy41TDcuNSAxNC44Mjg0QzcuNSAxNC4yOTggNy43MTA3MSAxMy43ODkzIDguMDg1NzkgMTMuNDE0MkwxNy40NjA4IDQuMDM5MjFaIi8+IDxwYXRoIGQ9Ik0xNi4yNSA1LjI1TDE5Ljc1IDguNzUiLz4gPC9zdmc+)',
  38. };
  39. const GF_STYLE = `
  40. .note-obj-gf-note-btn {
  41. background-image: ${GF_ICON.NOTE_BLACK};
  42. background-repeat: no-repeat;
  43. background-position: center;
  44. cursor: pointer;
  45. vertical-align: top;
  46. }
  47. .note-obj-gf-info-note-btn {
  48. background-size: 32px auto;
  49. width: 32px;
  50. height: 32px;
  51. margin-left: 20px;
  52. display: inline-block;
  53. }
  54. .note-obj-gf-library-note-btn {
  55. background-size: 24px auto;
  56. width: 24px;
  57. height: 24px;
  58. margin-left: 20px;
  59. display: inline-block;
  60. }
  61. .note-obj-gf-list-note-btn {
  62. background-size: 24px auto;
  63. width: 24px;
  64. height: 24px;
  65. margin-left: 10px;
  66. display: none;
  67. }
  68. .note-obj-gf-ts-note-btn {
  69. background-size: 16px auto;
  70. width: 16px;
  71. height: 16px;
  72. margin-left: 10px;
  73. display: none;
  74. vertical-align: sub;
  75. }
  76. ol.script-list li:hover .note-obj-gf-list-note-btn,
  77. #script-table tbody tr:hover .note-obj-gf-ts-note-btn {
  78. display: inline-block;
  79. }
  80. .note-obj-gf-note-tag,
  81. .note-obj-gf-ts-note-tag {
  82. background-color: #3c81df;
  83. color: #fff;
  84. display: inline-block;
  85. align-items: center;
  86. white-space: nowrap;
  87. border-radius: 50px;
  88. padding: 1px 10px;
  89. line-height: 1em;
  90. }
  91. .note-obj-gf-list-note-tag {
  92. text-decoration: none;
  93. }
  94. `;
  95. const noteObj = new Note_Obj({
  96. id: 'myGreasyForkNote',
  97. script: {
  98. author: {
  99. name: 'pana',
  100. homepage: 'https://greasyfork.org/zh-CN/users/193133-pana',
  101. },
  102. url: 'https://greasyfork.org/scripts/404275',
  103. updated: UPDATED,
  104. },
  105. itemClick: (key) => `${location.origin}/scripts/${key}`,
  106. language: {
  107. userIdText: {
  108. en: 'Script ID',
  109. zhHans: '脚本 ID',
  110. zhHant: '指令碼 ID',
  111. },
  112. userNameText: {
  113. en: 'Script name',
  114. zhHans: '脚本名',
  115. zhHant: '指令碼名',
  116. },
  117. },
  118. changeEvent,
  119. style: GF_STYLE,
  120. });
  121. function changeEvent(id) {
  122. const scriptId = getScriptIdFromPathname(location.pathname);
  123. if (scriptId) {
  124. infoPageNotes(scriptId, undefined, id);
  125. }
  126. else {
  127. listPageNotes(id);
  128. initTS(id);
  129. }
  130. }
  131. function initTS(changeId) {
  132. Note_Obj.fn.docQueryAll('#script-table tbody tr').forEach(item => {
  133. const scriptTitle = Note_Obj.fn.queryAnchor(item, '.thetitle a');
  134. if (scriptTitle) {
  135. const res = scriptTitle.href.match(/\d+$/);
  136. if (res) {
  137. const scriptId = res[0];
  138. const scriptName = scriptTitle.textContent?.trim();
  139. const thetitle = Note_Obj.fn.query(item, '.thetitle');
  140. if (thetitle && !Note_Obj.fn.query(thetitle, '.' + Note_Obj.btnClassName, 'none')) {
  141. thetitle.appendChild(noteObj.createNoteBtn(scriptId, scriptName, ['note-obj-gf-note-btn', 'note-obj-gf-ts-note-btn']));
  142. }
  143. if (!changeId || changeId === scriptId) {
  144. noteObj.handler(scriptId, scriptTitle, undefined, {
  145. add: 'span',
  146. className: ['note-obj-gf-ts-note-tag'],
  147. }, scriptName);
  148. }
  149. }
  150. }
  151. });
  152. }
  153. function getScriptIdFromPathname(pathname) {
  154. const res = pathname.match(/^\/[\w-]+\/scripts\/(\d+)-/);
  155. if (res && res.length === 2) {
  156. return res[1];
  157. }
  158. return null;
  159. }
  160. function infoPageNotes(scriptId, scriptName, changeId) {
  161. const ele = Note_Obj.fn.docQuery('#script-info h2', 'info');
  162. if (ele) {
  163. if (!changeId || changeId === scriptId)
  164. noteObj.handler(scriptId, ele, undefined, {
  165. add: 'sapn',
  166. className: ['note-obj-gf-note-tag'],
  167. }, scriptName);
  168. }
  169. }
  170. function listPageNotes(changeId) {
  171. const list = Note_Obj.fn.docQueryAll('ol.script-list li', 'info');
  172. for (const ele of list) {
  173. const scriptId = ele.dataset.scriptId;
  174. if (scriptId) {
  175. const description = Note_Obj.fn.query(ele, '.description');
  176. const scriptName = Note_Obj.fn.getText(ele, 'article > h2 > a', 'warn');
  177. if (description) {
  178. const desParent = description.parentElement;
  179. if (desParent && !Note_Obj.fn.query(desParent, '.' + Note_Obj.btnClassName, 'none')) {
  180. description.before(noteObj.createNoteBtn(scriptId, scriptName, ['note-obj-gf-note-btn', 'note-obj-gf-list-note-btn']));
  181. }
  182. }
  183. const header = Note_Obj.fn.query(ele, 'article > h2 > a');
  184. if (header) {
  185. if (!changeId || changeId === scriptId)
  186. noteObj.handler(scriptId, header, undefined, {
  187. add: 'span',
  188. className: ['note-obj-gf-note-tag', 'note-obj-gf-list-note-tag'],
  189. }, scriptName);
  190. }
  191. }
  192. }
  193. }
  194. function init() {
  195. const scriptId = getScriptIdFromPathname(location.pathname);
  196. if (scriptId) {
  197. const installHelpLink = Note_Obj.fn.docQuery('#install-area .install-help-link:last-child', 'info');
  198. const scriptName = Note_Obj.fn.docGetText('header h2');
  199. if (installHelpLink) {
  200. installHelpLink.after(noteObj.createNoteBtn(scriptId, scriptName, ['note-obj-gf-note-btn', 'note-obj-gf-info-note-btn']));
  201. }
  202. else {
  203. const suggestion = Note_Obj.fn.docQuery('#script-feedback-suggestion');
  204. suggestion?.appendChild(noteObj.createNoteBtn(scriptId, scriptName, ['note-obj-gf-note-btn', 'note-obj-gf-library-note-btn']));
  205. }
  206. infoPageNotes(scriptId, scriptName);
  207. }
  208. else {
  209. listPageNotes();
  210. const scriptList = Note_Obj.fn.docQuery('#browse-script-list', 'info');
  211. if (scriptList) {
  212. const listObserver = new MutationObserver(() => {
  213. listPageNotes();
  214. });
  215. listObserver.observe(scriptList, {
  216. childList: true,
  217. });
  218. }
  219. initTS();
  220. const tsTbody = Note_Obj.fn.docQuery('#script-table tbody', 'none');
  221. if (tsTbody) {
  222. const tsObserver = new MutationObserver(() => {
  223. initTS();
  224. });
  225. tsObserver.observe(tsTbody, {
  226. childList: true,
  227. });
  228. }
  229. }
  230. }
  231. init();
  232. })();