PickTrueBrowser

A tool to get meta info form ArtStation within browser to provide downloading service.

目前为 2019-09-29 提交的版本,查看 最新版本

  1. // ==UserScript==
  2. // @name PickTrueBrowser
  3. // @author winkidney@gmail.com
  4. // @version 0.0.2
  5. // @namespace tools
  6. // @description A tool to get meta info form ArtStation within browser to provide downloading service.
  7. // @match *://www.artstation.com/*
  8. // @grant GM_xmlhttpRequest
  9. // @require https://code.jquery.com/jquery-1.12.4.min.js
  10. // @run-at context-menu
  11. // ==/UserScript==
  12. let utils = {
  13. isFirefox: function () {
  14. return (navigator.userAgent.indexOf("Firefox") !== -1)
  15. },
  16. isChrome: function () {
  17. return (navigator.userAgent.indexOf("Chrome") !== -1)
  18. }
  19. };
  20.  
  21. let logger = {
  22. info: function(...args) {
  23. console.log("[PickTrue]: ", ...args);
  24. }
  25. };
  26.  
  27. let Artstation = function () {
  28. function _parseUserId(rawUrl) {
  29. var urlArray = rawUrl.split("/");
  30. var userId = urlArray[urlArray.length - 1];
  31. return userId;
  32. }
  33.  
  34. function fetchUrl(url, callback) {
  35. logger.info("Fetching url:", url);
  36. return $.get(url, callback);
  37. }
  38.  
  39. function _getUrl(userId, page) {
  40. return "https://www.artstation.com/users/" + userId + "/projects.json?page=" + page;
  41. }
  42.  
  43. return {
  44. getPage: _getUrl,
  45. fetchUrl: fetchUrl,
  46. }
  47. };
  48.  
  49. let RequestProxy = function () {
  50. let client = Artstation();
  51.  
  52. function submitTask(respData, callback) {
  53. logger.info("Submit response:", respData);
  54. let request_data = JSON.stringify(respData);
  55. let details = {
  56. url: "http://localhost:2333/tasks/submit/",
  57. data: request_data,
  58. method: "POST",
  59. onloadend: function (data) {
  60. logger.info("Submit response done: ", data);
  61. callback()
  62. },
  63. };
  64. return GM_xmlhttpRequest(details);
  65. }
  66. function getTask() {
  67. let details = {
  68. url: "http://localhost:2333/tasks/",
  69. method: "GET",
  70. onloadend: function (resp) {
  71. logger.info("Get task: ", resp);
  72. let data = JSON.parse(resp.responseText);
  73. if (data.length <= 0){
  74. return getTask()
  75. } else {
  76. client.fetchUrl(
  77. data[0],
  78. function (respData) {
  79. submitTask(respData, getTask)
  80. },
  81. )
  82. }
  83. },
  84. };
  85. return GM_xmlhttpRequest(details);
  86. }
  87. return {
  88. getTask: getTask,
  89. submitTask: submitTask,
  90. };
  91. };
  92.  
  93. function entry() {
  94. alert("请确保已经启动了PickTrue客户端。将要解析当前用户的所有图集并将下载地址发送PickTrue下载器,确认后将立即开始。");
  95. let proxy = RequestProxy();
  96. proxy.getTask();
  97. }
  98.  
  99. function _setUpContextMenuFirefox(entryFn) {
  100. var menu = document.body.appendChild(document.createElement("menu"));
  101. var html = document.documentElement;
  102. if (html.hasAttribute("contextmenu")) {
  103. // We don't want to override web page context menu if any
  104. var contextmenu = $("#" + html.getAttribute("contextmenu"));
  105. contextmenu[0].appendChild(menu); // Append to web page context menu
  106. } else {
  107. html.setAttribute("contextmenu", "userscript-picktrue-context-menu");
  108. }
  109.  
  110. menu.outerHTML = '<menu id="userscript-picktrue-context-menu"\
  111. type="context">\
  112. <menuitem id="userscript-picktrue-menuitem"\
  113. label="发送相册到PickTrue并下载">\
  114. </menuitem>\
  115. </menu>';
  116.  
  117. if ("contextMenu" in html && "HTMLMenuItemElement" in window) {
  118. var menuitem = $("#userscript-picktrue-menuitem")[0];
  119. menuitem.addEventListener("click", entryFn, false);
  120. }
  121. }
  122.  
  123. function _setUpContextMenuChrome(entryFn) {
  124. $(document).on("contextmenu", function (e) {
  125. if (e.ctrlKey){
  126. entryFn()
  127. }
  128. });
  129. }
  130.  
  131. function setUpContextMenu(entryFn) {
  132. if (utils.isFirefox()) {
  133. _setUpContextMenuFirefox(entryFn);
  134. } else if (utils.isChrome()) {
  135. _setUpContextMenuChrome(entryFn);
  136. } else {
  137. alert("Unsupported browser " + navigator.userAgent);
  138. }
  139. }
  140.  
  141. setUpContextMenu(entry);