Greasyfork - 为脚本添加备注

为脚本添加备注功能,以帮助识别和搜索

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

  1. // ==UserScript==
  2. // @name Greasyfork - Add notes to the script
  3. // @name:zh-CN Greasyfork - 为脚本添加备注
  4. // @name:zh-TW Greasyfork - 為腳本新增備註
  5. // @name:ja Greasyfork - スクリプトにコメントを追加
  6. // @name:ko Greasyfork - 스크립트에 메모 추가
  7. // @namespace https://greasyfork.org/zh-CN/users/193133-pana
  8. // @homepage https://www.sailboatweb.com
  9. // @icon data:image/svg+xml;base64,PHN2ZyByb2xlPSJpbWciIHhtbG5zPSJodHRwOi8vd3d3LnczLm9yZy8yMDAwL3N2ZyIgd2lkdGg9IjI0cHgiIGhlaWdodD0iMjRweCIgdmlld0JveD0iMCAwIDI0IDI0IiBhcmlhLWxhYmVsbGVkYnk9Im5ld0ljb25UaXRsZSIgc3Ryb2tlPSJyZ2JhKDI5LDE2MSwyNDIsMS4wMCkiIHN0cm9rZS13aWR0aD0iMiIgc3Ryb2tlLWxpbmVjYXA9InNxdWFyZSIgc3Ryb2tlLWxpbmVqb2luPSJtaXRlciIgZmlsbD0ibm9uZSIgY29sb3I9InJnYmEoMjksMTYxLDI0MiwxLjAwKSI+IDx0aXRsZSBpZD0ibmV3SWNvblRpdGxlIj5OZXc8L3RpdGxlPiA8cGF0aCBkPSJNMTkgMTRWMjJIMi45OTk5N1Y0SDEzIi8+IDxwYXRoIGQ9Ik0xNy40NjA4IDQuMDM5MjFDMTguMjQxOCAzLjI1ODE3IDE5LjUwODIgMy4yNTgxNiAyMC4yODkyIDQuMDM5MjFMMjAuOTYwOCA0LjcxMDc5QzIxLjc0MTggNS40OTE4NCAyMS43NDE4IDYuNzU4MTcgMjAuOTYwOCA3LjUzOTIxTDExLjU4NTggMTYuOTE0MkMxMS4yMTA3IDE3LjI4OTMgMTAuNzAyIDE3LjUgMTAuMTcxNiAxNy41TDcuNSAxNy41TDcuNSAxNC44Mjg0QzcuNSAxNC4yOTggNy43MTA3MSAxMy43ODkzIDguMDg1NzkgMTMuNDE0MkwxNy40NjA4IDQuMDM5MjFaIi8+IDxwYXRoIGQ9Ik0xNi4yNSA1LjI1TDE5Ljc1IDguNzUiLz4gPC9zdmc+
  10. // @version 2.0.0
  11. // @description Add a note for scripts to help identify and search
  12. // @description:zh-CN 为脚本添加备注功能,以帮助识别和搜索
  13. // @description:zh-TW 為腳本新增備註功能,以幫助識別和搜尋
  14. // @description:ja スクリプトにコメント機能を追加して、識別と検索を支援します
  15. // @description:ko 식별 및 검색에 도움이되도록 스크립트에 메모 추가
  16. // @author pana
  17. // @license GNU General Public License v3.0 or later
  18. // @include http*://*greasyfork.org/*
  19. // @include http*://*sleazyfork.org/*
  20. // @require https://cdn.jsdelivr.net/npm/vue@2.6.11/dist/vue.min.js
  21. // @require https://greasyfork.org/scripts/408454-note-obj/code/Note_Obj.js?version=846124
  22. // @grant GM_info
  23. // @grant GM_getValue
  24. // @grant GM_setValue
  25. // @grant GM_deleteValue
  26. // @grant GM_listValues
  27. // @grant GM_registerMenuCommand
  28. // @grant GM_unregisterMenuCommand
  29. // @grant GM_addValueChangeListener
  30. // @grant GM_openInTab
  31. // ==/UserScript==
  32.  
  33. (function() {
  34. 'use strict';
  35. const GF_ICON = {
  36. 'NOTE_BLACK': 'url(data:image/svg+xml;base64,PHN2ZyByb2xlPSJpbWciIHhtbG5zPSJodHRwOi8vd3d3LnczLm9yZy8yMDAwL3N2ZyIgd2lkdGg9IjI0cHgiIGhlaWdodD0iMjRweCIgdmlld0JveD0iMCAwIDI0IDI0IiBhcmlhLWxhYmVsbGVkYnk9Im5ld0ljb25UaXRsZSIgc3Ryb2tlPSJyZ2IoMzgsIDM4LCAzOCkiIHN0cm9rZS13aWR0aD0iMiIgc3Ryb2tlLWxpbmVjYXA9InNxdWFyZSIgc3Ryb2tlLWxpbmVqb2luPSJtaXRlciIgZmlsbD0ibm9uZSIgY29sb3I9InJnYigzOCwgMzgsIDM4KSI+IDx0aXRsZSBpZD0ibmV3SWNvblRpdGxlIj5OZXc8L3RpdGxlPiA8cGF0aCBkPSJNMTkgMTRWMjJIMi45OTk5N1Y0SDEzIi8+IDxwYXRoIGQ9Ik0xNy40NjA4IDQuMDM5MjFDMTguMjQxOCAzLjI1ODE3IDE5LjUwODIgMy4yNTgxNiAyMC4yODkyIDQuMDM5MjFMMjAuOTYwOCA0LjcxMDc5QzIxLjc0MTggNS40OTE4NCAyMS43NDE4IDYuNzU4MTcgMjAuOTYwOCA3LjUzOTIxTDExLjU4NTggMTYuOTE0MkMxMS4yMTA3IDE3LjI4OTMgMTAuNzAyIDE3LjUgMTAuMTcxNiAxNy41TDcuNSAxNy41TDcuNSAxNC44Mjg0QzcuNSAxNC4yOTggNy43MTA3MSAxMy43ODkzIDguMDg1NzkgMTMuNDE0MkwxNy40NjA4IDQuMDM5MjFaIi8+IDxwYXRoIGQ9Ik0xNi4yNSA1LjI1TDE5Ljc1IDguNzUiLz4gPC9zdmc+)'
  37. };
  38. const GF_STYLE = `
  39. .note-obj-gf-note-btn {
  40. background-image: ${GF_ICON.NOTE_BLACK};
  41. background-repeat: no-repeat;
  42. background-position: center;
  43. cursor: pointer;
  44. vertical-align: top;
  45. }
  46. .note-obj-gf-info-note-btn {
  47. background-size: 32px auto;
  48. width: 32px;
  49. height: 32px;
  50. margin-left: 20px;
  51. display: inline-block;
  52. }
  53. .note-obj-gf-list-note-btn {
  54. background-size: 24px auto;
  55. width: 24px;
  56. height: 24px;
  57. margin-left: 10px;
  58. display: none;
  59. }
  60. .note-obj-gf-ts-note-btn {
  61. background-size: 16px auto;
  62. width: 16px;
  63. height: 16px;
  64. margin-left: 10px;
  65. display: none;
  66. vertical-align: sub;
  67. }
  68. ol.script-list li:hover .note-obj-gf-list-note-btn,
  69. #script-table tbody tr:hover .note-obj-gf-ts-note-btn {
  70. display: inline-block;
  71. }
  72. .note-obj-gf-note-tag,
  73. .note-obj-gf-ts-note-tag {
  74. background-color: #3c81df;
  75. color: #fff;
  76. display: inline-block;
  77. align-items: center;
  78. white-space: nowrap;
  79. border-radius: 50px;
  80. }
  81. .note-obj-gf-note-tag {
  82. padding: 1px 10px;
  83. }
  84. .note-obj-gf-ts-note-tag {
  85. padding: 0px 10px;
  86. }
  87. .note-obj-gf-list-note-tag {
  88. text-decoration: none;
  89. }
  90. `;
  91. function change_Event(note_obj, id = null) {
  92. let pathname = location.pathname;
  93. if (/^\/[\w-]+\/scripts\/\d+-/i.test(pathname)) {
  94. let script_id = /^\/[\w-]+\/scripts\/(\d+)-/i.exec(pathname)[1];
  95. let ele = document.querySelector('#script-info h2');
  96. ele && (! id || id == script_id) && note_obj.handler(script_id, ele, null, {
  97. 'add': 'span',
  98. 'class': 'note-obj-gf-note-tag'
  99. });
  100. } else if (/^\/[\w-]+\/scripts/i.test(pathname) || /^\/[\w-]+\/users\/\d+/i.test(pathname)) {
  101. let browse_list = document.querySelectorAll('ol.script-list li');
  102. for (let ele of browse_list) {
  103. let script_id = ele.getAttribute('data-script-id');
  104. if (script_id) {
  105. let separator = ele.querySelector('.name-description-separator');
  106. separator && (! id || id == script_id) && note_obj.handler(script_id, separator, null, {
  107. 'add': 'span',
  108. 'class': ['note-obj-gf-note-tag', 'note-obj-gf-list-note-tag']
  109. });
  110. }
  111. }
  112. document.querySelectorAll('#script-table tbody tr').forEach(item => {
  113. let script_title = item.querySelector('.thetitle a');
  114. if (script_title) {
  115. let script_id = script_title.href.match(/\d+$/) && script_title.href.match(/\d+$/)[0];
  116. (! id || id == script_id) && note_obj.handler(script_id, script_title, null, {
  117. 'add': 'span',
  118. 'class': 'note-obj-gf-ts-note-tag'
  119. });
  120. }
  121. });
  122. }
  123. }
  124. function init_TS(note_obj) {
  125. document.querySelectorAll('#script-table tbody tr').forEach(item => {
  126. let script_title = item.querySelector('.thetitle a');
  127. if (script_title) {
  128. let script_id = script_title.href.match(/\d+$/) && script_title.href.match(/\d+$/)[0];
  129. let thetitle = item.querySelector('.thetitle');
  130. script_id && thetitle.appendChild(note_obj.createNoteBtn(script_id, script_title.textContent, ['note-obj-gf-note-btn', 'note-obj-gf-ts-note-btn']));
  131. note_obj.judgeUsers(script_id) && note_obj.handler(script_id, script_title, null, {
  132. 'add': 'span',
  133. 'class': 'note-obj-gf-ts-note-tag'
  134. });
  135. }
  136. });
  137. }
  138. function init() {
  139. let old_config = GM_getValue('greasyfork_config', null);
  140. if (old_config && old_config.scripts_array) {
  141. for (let item of old_config.scripts_array) {
  142. GM_setValue(item.id, {
  143. 'tag': item.tag
  144. });
  145. }
  146. GM_deleteValue('greasyfork_config');
  147. }
  148. let note_obj = new Note_Obj('myGreasyForkNote');
  149. note_obj.init({
  150. 'style': GF_STYLE,
  151. 'changeEvent': change_Event,
  152. 'script': {
  153. 'author': {
  154. 'name': 'pana',
  155. 'homepage': 'https://www.sailboatweb.com/'
  156. },
  157. 'address': 'https://greasyfork.org/scripts/404275',
  158. 'updated': '2020-9-9',
  159. 'library': [
  160. {
  161. 'name': 'Vue.js',
  162. 'version': '2.6.11',
  163. 'url': 'https://vuejs.org/'
  164. }
  165. ]
  166. },
  167. 'itemClick': key => 'https://greasyfork.org/scripts/' + key,
  168. 'type': 'script'
  169. });
  170. let pathname = location.pathname;
  171. if (/^\/[\w-]+\/scripts\/\d+-/i.test(pathname)) {
  172. let script_id = /^\/[\w-]+\/scripts\/(\d+)-/i.exec(pathname)[1];
  173. let install_help_link = document.querySelector('#install-area .install-help-link');
  174. if (install_help_link) {
  175. let script_name = (document.querySelector('header h2') && document.querySelector('header h2').textContent) || '';
  176. install_help_link.after(note_obj.createNoteBtn(script_id, script_name, ['note-obj-gf-note-btn', 'note-obj-gf-info-note-btn']));
  177. }
  178. let ele = document.querySelector('#script-info h2');
  179. ele && note_obj.judgeUsers(script_id) && note_obj.handler(script_id, ele, null, {
  180. 'add': 'span',
  181. 'class': 'note-obj-gf-note-tag'
  182. });
  183. } else if (/^\/[\w-]+\/scripts/i.test(pathname) || /^\/[\w-]+\/users\/\d+/i.test(pathname)) {
  184. let browse_list = document.querySelectorAll('ol.script-list li');
  185. for (let ele of browse_list) {
  186. let script_id = ele.getAttribute('data-script-id');
  187. if (script_id) {
  188. let description = ele.querySelector('.description');
  189. if (description) {
  190. let script_name = (ele.querySelector('article > h2 > a') && ele.querySelector('article > h2 > a').textContent) || '';
  191. description.before(note_obj.createNoteBtn(script_id, script_name, ['note-obj-gf-note-btn', 'note-obj-gf-list-note-btn']));
  192. }
  193. let header = ele.querySelector('article > h2 > a');
  194. header && note_obj.judgeUsers(script_id) && note_obj.handler(script_id, header, null, {
  195. 'add': 'span',
  196. 'class': ['note-obj-gf-note-tag', 'note-obj-gf-list-note-tag']
  197. });
  198. }
  199. }
  200. init_TS(note_obj);
  201. let ts_tbody = document.querySelector('#script-table tbody');
  202. if (ts_tbody) {
  203. let observer = new MutationObserver(() => {
  204. init_TS(note_obj);
  205. });
  206. observer.observe(ts_tbody, {
  207. 'childList': true
  208. });
  209. }
  210. }
  211. }
  212. init();
  213. })();