导入yande.re图片到Eagle

在yande.re网页上添加下载按钮直接导入Eagle,仅实现post,未处理pool

  1. // ==UserScript==
  2. // @name Save yande.re Images to Eagle
  3. // @name:zh 导入yande.re图片到Eagle
  4. // @name:zh-CN 导入yande.re图片到Eagle
  5. // @description Save images from yande.re to Eagle.
  6. // @description:zh 在yande.re网页上添加下载按钮直接导入Eagle,仅实现post,未处理pool
  7. // @description:zh-CN 在yande.re网页上添加下载按钮直接导入Eagle,仅实现post,未处理pool
  8.  
  9. // @namespace https://github.com/miracleXL
  10. // @homepageURL https://github.com/miracleXL/scripts-for-Eagle
  11. // @icon https://yande.re/favicon.ico
  12. // @version 0.1.3
  13.  
  14. // @author miracleXL
  15. // @match yande.re/post/show/*
  16. // @match yande.re/pool/show/*
  17. // @connect yande.re
  18. // @connect localhost
  19. // @grant GM_xmlhttpRequest
  20. // ==/UserScript==
  21.  
  22. (function() {
  23. 'use strict';
  24.  
  25. // Eagle API 服务器位置
  26. const EAGLE_SERVER_URL = "http://localhost:41595";
  27. const EAGLE_IMPORT_API_URL = `${EAGLE_SERVER_URL}/api/item/addFromURL`;
  28. const EAGLE_IMPORT_API_URLS = `${EAGLE_SERVER_URL}/api/item/addFromURLs`;
  29. const EAGLE_CREATE_FOLDER_API_URL = `${EAGLE_SERVER_URL}/api/folder/create`;
  30. const EAGLE_GET_FOLDERS_API_URL = `${EAGLE_SERVER_URL}/api/folder/list`;
  31.  
  32. // 是否保存标签
  33. const saveTags = true;
  34.  
  35. let mode = document.URL.split("/")[3];
  36.  
  37. function addButton(){
  38. if(mode == "post"){
  39. let button = document.createElement("a");
  40. button.innerText = "下载";
  41. button.href = "javascript:;";
  42. let buttons_div = document.getElementsByClassName("js-posts-show-comments-tab")[0].parentNode;
  43. buttons_div.append(" | ");
  44. buttons_div.appendChild(button);
  45. //绑定下载事件
  46. button.addEventListener("click",async ()=>{
  47. let [data,pool] = getImageData();
  48. if(pool){
  49. let folderId = await getFolderId(pool);
  50. if(folderId){
  51. data.folderId = folderId;
  52. }
  53. }
  54. download(data);
  55. })
  56. }
  57. else{
  58.  
  59. }
  60. }
  61.  
  62. addButton();
  63.  
  64. function download(data){
  65. // console.log(data);
  66. GM_xmlhttpRequest({
  67. url: EAGLE_IMPORT_API_URL,
  68. method: "POST",
  69. data: JSON.stringify(data),
  70. onload: function(response) {
  71. if(response.statusText !== "OK"){
  72. console.log(response);
  73. alert("下载失败!")
  74. }
  75. }
  76. });
  77. }
  78.  
  79. function getImageData(){
  80. let pool = document.getElementsByClassName("status-notice");
  81. pool = pool[pool.length-1];
  82. let poolName;
  83. if(pool && pool.id.startsWith("pool")){
  84. try{
  85. poolName = pool.querySelector("p").children[3].textContent;
  86. }
  87. catch(e){
  88. console.log("获取pool名失败!");
  89. console.log(e);
  90. }
  91. }
  92. let url = document.getElementById("highres");
  93. let data = {
  94. "url": url.href,
  95. "name": document.title,
  96. "website": document.URL,
  97. "tags": [],
  98. "headers": {
  99. "referer" : document.URL
  100. }
  101. };
  102. if(saveTags){
  103. for(let tag of document.getElementsByClassName("tag-type-artist")){
  104. data.tags.push(tag.children[1].textContent);
  105. }
  106. for(let tag of document.getElementsByClassName("tag-type-copyright")){
  107. data.tags.push(tag.children[1].textContent);
  108. }
  109. for(let tag of document.getElementsByClassName("tag-type-general")){
  110. data.tags.push(tag.children[1].textContent);
  111. };
  112. }
  113. return [data,poolName];
  114. };
  115.  
  116. // 获取文件夹id
  117. async function getFolderId(pool){
  118. let folders = await getFolders();
  119. let dlFolder;
  120. if(folders){
  121. for(let folder of folders){
  122. if(folder.name === pool){
  123. dlFolder = folder;
  124. }
  125. }
  126. if(dlFolder === undefined) dlFolder = await creatFolder(pool);
  127. }
  128. else{
  129. console.log("获取文件夹信息失败!");
  130. alert("下载失败!");
  131. return;
  132. }
  133. return dlFolder.id;
  134. }
  135.  
  136. // 获取文件夹
  137. function getFolders(){
  138. return new Promise((resolve, reject) => {
  139. GM_xmlhttpRequest({
  140. url: EAGLE_GET_FOLDERS_API_URL,
  141. method: "GET",
  142. redirect:'follow',
  143. onload: function(response) {
  144. if(response.status !== 200){
  145. reject();
  146. }
  147. resolve(JSON.parse(response.response).data);
  148. }
  149. });
  150. })
  151. }
  152.  
  153. // 创建文件夹
  154. function creatFolder(folderName){
  155. return new Promise((resolve, reject) => {
  156. GM_xmlhttpRequest({
  157. url: EAGLE_CREATE_FOLDER_API_URL,
  158. method: "POST",
  159. data: JSON.stringify({ folderName: folderName }),
  160. onload: function(response) {
  161. var result = JSON.parse(response.response);
  162. if (result.status === "success" && result.data && result.data.id) {
  163. return resolve(result.data);
  164. }
  165. else{
  166. console.log("文件夹创建失败!")
  167. return reject();
  168. }
  169. }
  170. })
  171. })
  172. }
  173.  
  174. })();