Greasyfork 快捷编辑收藏

在GF脚本页添加快速打开收藏集编辑页面功能

As of 2023-11-02. See the latest version.

  1. /* eslint-disable no-multi-spaces */
  2.  
  3. // ==UserScript==
  4. // @name Greasyfork 快捷编辑收藏
  5. // @name:zh-CN Greasyfork 快捷编辑收藏
  6. // @name:zh-TW Greasyfork 快捷編輯收藏
  7. // @name:en Greasyfork script-set-edit button
  8. // @name:en-US Greasyfork script-set-edit button
  9. // @namespace Greasyfork-Favorite
  10. // @version 0.1.8
  11. // @description 在GF脚本页添加快速打开收藏集编辑页面功能
  12. // @description:zh-CN 在GF脚本页添加快速打开收藏集编辑页面功能
  13. // @description:zh-TW 在GF腳本頁添加快速打開收藏集編輯頁面功能
  14. // @description:en Add open script-set-edit-page button in GF script page
  15. // @description:en-US Add open script-set-edit-page button in GF script page
  16. // @author PY-DNG
  17. // @license GPL-3
  18. // @match http*://*.greatest.deepsurf.us/*
  19. // @match http*://*.sleazyfork.org/*
  20. // @require https://greatest.deepsurf.us/scripts/456034-basic-functions-for-userscripts/code/script.js?version=1226884
  21. // @require https://greatest.deepsurf.us/scripts/460385-gm-web-hooks/code/script.js?version=1221394
  22. // @icon data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAABAAAAAQCAYAAAAf8/9hAAAAAXNSR0IArs4c6QAAAbBJREFUOE+Vk7GKGlEUhr8pAiKKDlqpCDpLUCzWBxCENBa+hBsL9wHsLWxXG4tNtcGH0MIiWopY7JSGEUWsbESwUDMw4Z7siLsZDbnlPff/7n/+e67G38sA6sAXIPVWXgA/gCdgfinRPuhfCoXCw3Q65XA4eLBl6zvw1S2eAZqmvTqOc5/NZhkMBqRSKWzbvgYxgbwquoAX4MGyLHK5HIlEgtFo9C+IOFEAo1gsWsvlUmyPx2MymYxAhsMh6XT6lpM7BXjWdf1xNpuRz+fl8GQywTAMGo0G1WpVnJxOJ692vinADPgcDAaZz+cCOR6PmKZJPB4XUb/fp1wuewF+KoBCf1JVBVE5dDodms3mWdDtdqlUKl6AX+8ALmS9XgtM0/5kvNlspKX9fv8RIgBp4bISCoXo9XqsVitKpRK6rrPb7STQ7XZ7eVRaeAYerz14OBxGOfL7/eIgmUwKzHEcJZEQ1eha1wBqPxqNihufzyeQWCzmtiPPqJYM0jWIyiISibBYLAgEAtTrdVqt1nmQXN0rcH/LicqmVqvRbrdN27bfjbKru+nk7ZD3Z7q4+b++82/YPKIrXsKZ3AAAAABJRU5ErkJggg==
  23. // @grant GM_xmlhttpRequest
  24. // @grant GM_setValue
  25. // @grant GM_getValue
  26. // ==/UserScript==
  27.  
  28. /* global LogLevel DoLog Err $ $All $CrE $AEL $$CrE addStyle detectDom destroyEvent copyProp copyProps parseArgs escJsStr replaceText getUrlArgv dl_browser dl_GM AsyncManager */
  29. /* global GMXHRHook GMDLHook */
  30.  
  31. (function __MAIN__() {
  32. 'use strict';
  33.  
  34. const CONST = {
  35. Text: {
  36. 'zh-CN': {
  37. FavEdit: '收藏集:',
  38. Add: '加入此集',
  39. Edit: '手动编辑',
  40. CopySID: '复制脚本ID',
  41. Working: ['正在添加...', '就快好了...'],
  42. InSetStatus: ['未添加', '已添加'],
  43. Error: {
  44. Unknown: '未知错误'
  45. }
  46. },
  47. 'zh-TW': {
  48. FavEdit: '收藏集:',
  49. Add: '加入此集',
  50. Edit: '手動編輯',
  51. CopySID: '複製腳本ID',
  52. Working: ['正在添加...', '就快好了...'],
  53. InSetStatus: ['未添加', '已添加'],
  54. Error: {
  55. Unknown: '未知錯誤'
  56. }
  57. },
  58. 'en': {
  59. FavEdit: 'Add to/Remove from favorite list: ',
  60. Add: 'Add',
  61. Edit: 'Edit Manually',
  62. CopySID: 'Copy-Script-ID',
  63. Working: ['Working...', 'Just a moment...'],
  64. InSetStatus: ['Not Added', 'Added'],
  65. Error: {
  66. Unknown: 'Unknown Error'
  67. }
  68. },
  69. 'default': {
  70. FavEdit: 'Add to/Remove from favorite list: ',
  71. Add: 'Add',
  72. Edit: 'Edit Manually',
  73. CopySID: 'Copy-Script-ID',
  74. Working: ['Working...', 'Just a moment...'],
  75. InSetStatus: ['Not Added', 'Added'],
  76. Error: {
  77. Unknown: 'Unknown Error'
  78. }
  79. },
  80. }
  81. }
  82.  
  83. // Get i18n code
  84. let i18n = navigator.language;
  85. if (!Object.keys(CONST.Text).includes(i18n)) {i18n = 'default';}
  86.  
  87. main()
  88. function main() {
  89. const HOST = getHost();
  90. const API = getAPI();
  91.  
  92. // Common actions
  93. GMXHRHook(5);
  94. commons();
  95.  
  96. // API-based actions
  97. switch(API[1]) {
  98. case "scripts":
  99. API[2] && centerScript(API);
  100. break;
  101. default:
  102. DoLog('API is {}'.replace('{}', API));
  103. }
  104. }
  105.  
  106. function centerScript(API) {
  107. switch(API[3]) {
  108. case undefined:
  109. pageScript();
  110. break;
  111. case 'code':
  112. pageCode();
  113. break;
  114. case 'feedback':
  115. pageFeedback();
  116. break;
  117. }
  118. }
  119.  
  120. function commons() {
  121. // Your common actions here...
  122. }
  123.  
  124. function pageScript() {
  125. addFavPanel();
  126. }
  127.  
  128. function pageCode() {
  129. addFavPanel();
  130. }
  131.  
  132. function pageFeedback() {
  133. addFavPanel();
  134. }
  135.  
  136. function addFavPanel() {
  137. if (!getUserpage()) {return false;}
  138. GUI();
  139.  
  140. function GUI() {
  141. // Get elements
  142. const script_after = $('#script-feedback-suggestion+*') || $('#new-script-discussion');
  143. const script_parent = script_after.parentElement;
  144.  
  145. // My elements
  146. const script_favorite = $CrE('div');
  147. script_favorite.id = 'script-favorite';
  148. script_favorite.style.margin = '0.75em 0';
  149. script_favorite.innerHTML = CONST.Text[i18n].FavEdit;
  150.  
  151. const favorite_groups = $CrE('select');
  152. favorite_groups.id = 'favorite-groups';
  153.  
  154. const stored_sets = GM_getValue('script-sets', {sets: []}).sets;
  155. for (const set of stored_sets) {
  156. // Make <option>
  157. const option = $CrE('option');
  158. option.innerText = set.name;
  159. option.value = set.linkedit;
  160. $APD(favorite_groups, option);
  161. }
  162. adjustWidth();
  163.  
  164. getScriptSets(function(sets) {
  165. clearChildnodes(favorite_groups);
  166. for (const set of sets) {
  167. // Make <option>
  168. const option = set.elmOption = $CrE('option');
  169. option.innerText = set.name;
  170. option.value = set.linkedit;
  171. $APD(favorite_groups, option);
  172. }
  173. adjustWidth();
  174.  
  175. // Set edit-button.href
  176. favorite_edit.href = favorite_groups.value;
  177.  
  178. // Check script in-set status
  179. getInSets(sets, getStrSID(), inSets => {
  180. sets.forEach(set => {
  181. const inSet = inSets.includes(set);
  182. set.elmOption.innerText = `(${CONST.Text[i18n].InSetStatus[inSet+0]}) ${set.name}`;
  183. });
  184. adjustWidth();
  185. });
  186. })
  187. favorite_groups.addEventListener('change', function(e) {
  188. favorite_edit.href = favorite_groups.value;
  189. });
  190.  
  191. const favorite_add = $CrE('a');
  192. favorite_add.id = 'favorite-add';
  193. favorite_add.innerHTML = CONST.Text[i18n].Add;
  194. favorite_add.style.margin = favorite_add.style.margin = '0px 0.5em';
  195. favorite_add.href = 'javascript:void(0);'
  196. favorite_add.addEventListener('click', function(e) {
  197. addFav();
  198. });
  199.  
  200. const favorite_edit = $CrE('a');
  201. favorite_edit.id = 'favorite-edit';
  202. favorite_edit.innerHTML = CONST.Text[i18n].Edit;
  203. favorite_edit.style.margin = favorite_edit.style.margin = '0px 0.5em';
  204. favorite_edit.target = '_blank';
  205.  
  206. const favorite_copy = $CrE('a');
  207. favorite_copy.id = 'favorite-copy';
  208. favorite_copy.href = 'javascript: void(0);';
  209. favorite_copy.innerHTML = CONST.Text[i18n].CopySID;
  210. favorite_copy.addEventListener('click', function() {
  211. copyText(getStrSID());
  212. });
  213.  
  214. // Append to document
  215. $APD(script_favorite, favorite_groups);
  216. script_parent.insertBefore(script_favorite, script_after);
  217. $APD(script_favorite, favorite_add);
  218. $APD(script_favorite, favorite_edit);
  219. $APD(script_favorite, favorite_copy);
  220.  
  221. function adjustWidth() {
  222. favorite_groups.style.width = Math.max.apply(null, Array.from(favorite_groups.children).map((o) => (o.innerText.length))).toString() + 'em';
  223. favorite_groups.style.maxWidth = '40vw';
  224. }
  225.  
  226. function addFav() {
  227. const iframe = $CrE('iframe');
  228. iframe.style.width = iframe.style.height = iframe.style.border = '0';
  229. iframe.addEventListener('load', edit_onload, {once: true});
  230. iframe.src = favorite_groups.value;
  231. $APD(document.body, iframe);
  232. displayNotice(CONST.Text[i18n].Working[0]);
  233.  
  234. function edit_onload() {
  235. const oDom = iframe.contentDocument;
  236. const input = $CrE('input');
  237. input.value = getStrSID();
  238. input.name = 'scripts-included[]';
  239. input.type = 'hidden';
  240. $APD($(oDom, '#script-set-scripts'), input);
  241. $(oDom, 'button[name="save"]').click();
  242. iframe.addEventListener('load', finish_onload, {once: true});
  243. displayNotice(CONST.Text[i18n].Working[1]);
  244. }
  245.  
  246. function finish_onload() {
  247. const status = $(iframe.contentDocument, 'p.notice');
  248. const status_text = status ? status.innerText : CONST.Text[i18n].Error.Unknown;
  249. displayNotice(status_text);
  250. iframe.parentElement.removeChild(iframe);
  251. }
  252.  
  253. function displayNotice(text) {
  254. const notice = $CrE('p');
  255. notice.classList.add('notice');
  256. notice.id = 'fav-notice';
  257. notice.innerText = text;
  258. const old_notice = $('#fav-notice');
  259. old_notice && old_notice.parentElement.removeChild(old_notice);
  260. $('#script-content').insertAdjacentElement('afterbegin', notice);
  261. }
  262. }
  263. }
  264. }
  265.  
  266. function getScriptSets(callback, args=[]) {
  267. const userpage = getUserpage();
  268. getDocument(userpage, function(oDom) {
  269. /*
  270. const user_script_sets = oDom.querySelector('#user-script-sets');
  271. const script_sets = [];
  272.  
  273. for (const li of user_script_sets.querySelectorAll('li')) {
  274. // Get fav info
  275. const name = li.childNodes[0].nodeValue.trimRight();
  276. const link = li.children[0].href;
  277. const linkedit = li.children[1] ? li.children[1].href : 'https://greatest.deepsurf.us/' + $('#language-selector-locale').value + '/users/' + $('#nav-user-info>.user-profile-link>a').href.match(/[a-zA-Z\-]+\/users\/([^\/]*)/)[1] + '/sets/' + li.children[0].href.match(/[\?&]set=(\d+)/)[1] + '/edit';
  278.  
  279. // Append to script_sets
  280. script_sets.push({
  281. name: name,
  282. link: link,
  283. linkedit: linkedit
  284. });
  285. }
  286. */
  287. const script_sets = Array.from($(oDom, 'ul#user-script-sets').children).map(li => ({
  288. name: li.children[0].innerText,
  289. link: li.children[0].href,
  290. linkedit: li.children[1].href
  291. }));
  292.  
  293. // Save to GM_storage
  294. GM_setValue('script-sets', {
  295. sets: script_sets,
  296. time: (new Date()).getTime(),
  297. version: '0.2'
  298. });
  299.  
  300. // callback
  301. callback.apply(null, [script_sets].concat(args));
  302. });
  303. }
  304.  
  305. function getUserpage() {
  306. const a = $('#nav-user-info>.user-profile-link>a');
  307. return a ? a.href : null;
  308. }
  309.  
  310. function getInSet(set, sid, callback) {
  311. sid = sid.toString();
  312. getDocument(set.linkedit, oDom => {
  313. const inSet = [...$(oDom, '#script-set-scripts').children].some(input => input.value === sid);
  314. callback(inSet);
  315. });
  316. }
  317.  
  318. function getInSets(sets, sid, callback) {
  319. const AM = new AsyncManager();
  320. const inSets = [];
  321. for (const set of sets) {
  322. AM.add();
  323. getInSet(set, sid, inSet => {
  324. inSet && inSets.push(set);
  325. AM.finish();
  326. });
  327. }
  328. AM.onfinish = e => {
  329. callback(inSets);
  330. };
  331. AM.finishEvent = true;
  332. }
  333.  
  334. function getStrSID(url=location.href) {
  335. const API = getAPI(url);
  336. const strSID = API[2].match(/\d+/);
  337. return strSID;
  338. }
  339.  
  340. function getSID(url=location.href) {
  341. return Number(getStrSID(url));
  342. }
  343. // Basic functions
  344. function $APD(a,b) {return a.appendChild(b);}
  345.  
  346. // Remove all childnodes from an element
  347. function clearChildnodes(element) {
  348. const cns = []
  349. for (const cn of element.childNodes) {
  350. cns.push(cn);
  351. }
  352. for (const cn of cns) {
  353. element.removeChild(cn);
  354. }
  355. }
  356.  
  357. // Download and parse a url page into a html document(dom).
  358. // when xhr onload: callback.apply([dom, args])
  359. function getDocument(url, callback, args=[]) {
  360. GM_xmlhttpRequest({
  361. method : 'GET',
  362. url : url,
  363. responseType : 'blob',
  364. onloadstart : function() {
  365. DoLog(LogLevel.Info, 'getting document, url=\'' + url + '\'');
  366. },
  367. onload : function(response) {
  368. const htmlblob = response.response;
  369. parseDocument(htmlblob, callback, args);
  370. }
  371. })
  372. }
  373.  
  374. function parseDocument(htmlblob, callback, args=[]) {
  375. const reader = new FileReader();
  376. reader.onload = function(e) {
  377. const htmlText = reader.result;
  378. const dom = new DOMParser().parseFromString(htmlText, 'text/html');
  379. args = [dom].concat(args);
  380. callback.apply(null, args);
  381. //callback(dom, htmlText);
  382. }
  383. reader.readAsText(htmlblob, document.characterSet);
  384. }
  385.  
  386. // Copy text to clipboard (needs to be called in an user event)
  387. function copyText(text) {
  388. // Create a new textarea for copying
  389. const newInput = document.createElement('textarea');
  390. document.body.appendChild(newInput);
  391. newInput.value = text;
  392. newInput.select();
  393. document.execCommand('copy');
  394. document.body.removeChild(newInput);
  395. }
  396.  
  397. // get '/' splited API array from a url
  398. function getAPI(url=location.href) {
  399. return url.replace(/https?:\/\/(.*?\.){1,2}.*?\//, '').replace(/\?.*/, '').match(/[^\/]+?(?=(\/|$))/g);
  400. }
  401.  
  402. // get host part from a url(includes '^https://', '/$')
  403. function getHost(url=location.href) {
  404. const match = location.href.match(/https?:\/\/[^\/]+\//);
  405. return match ? match[0] : match;
  406. }
  407.  
  408. function randint(min, max) {
  409. return Math.floor(Math.random() * (max - min + 1)) + min;
  410. }
  411. })();