Conquistadork

ptpimg script

Sep 17th, 2021
69
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 11.06 KB | None | 0 0
  1. // ==UserScript==
  2. // @name PTH PTPImg It
  3. // @namespace https://orbitalzero.ovh/scripts
  4. // @description PTPImg It script for PTH
  5. // @include https://ptpimg.me/
  6. // @include https://passtheheadphones.me/upload.php*
  7. // @include https://passtheheadphones.me/torrents.php?action=editgroup*
  8. // @include https://passtheheadphones.me/torrents.php?id=*
  9. // @include https://passtheheadphones.me/user.php?action=edit&userid=*
  10. // @include https://passtheheadphones.me/artist.php?action=edit&artistid=*
  11. // @include https://passtheheadphones.me/requests.php?action=new*
  12. // @include https://passtheheadphones.me/requests.php?action=edit*
  13. // @include https://redacted.ch/upload.php*
  14. // @include https://redacted.ch/torrents.php?action=editgroup*
  15. // @include https://redacted.ch/torrents.php?id=*
  16. // @include https://redacted.ch/user.php?action=edit&userid=*
  17. // @include https://redacted.ch/artist.php?action=edit&artistid=*
  18. // @include https://redacted.ch/requests.php?action=new
  19. // @include https://redacted.ch/requests.php?action=edit*
  20. // @require https://greasemonkey.github.io/gm4-polyfill/gm4-polyfill.js
  21. // @version 0.15
  22. // @grant GM_xmlhttpRequest
  23. // @grant GM_setValue
  24. // @grant GM_getValue
  25. // @grant GM_addStyle
  26. // @grant GM.xmlHttpRequest
  27. // @grant GM.setValue
  28. // @grant GM.getValue
  29. // @grant GM.addStyle
  30. // @require https://code.jquery.com/jquery-3.1.1.min.js
  31. // ==/UserScript==
  32.  
  33. var css = `
  34. input[type="button"]:disabled {
  35. opacity: 0.5;
  36. cursor: not-allowed;
  37. }
  38. `;
  39.  
  40. var default_settings = {
  41. alert_when_done: true,
  42. api_key: "",
  43. host: "ptpimg"
  44. };
  45.  
  46. function get_api_key() {
  47. if (!window.localStorage.ptpimg_it) window.localStorage.ptpimg_it = JSON.stringify(default_settings);
  48. var settings = JSON.parse(window.localStorage.ptpimg_it);
  49. switch (settings.host) {
  50. default:
  51. window.localStorage.ptpimg_it = JSON.stringify(default_settings);
  52. case "lutim":
  53. return Promise.resolve("");
  54. case "ptpimg":
  55. return ptpimg_get_api_key();
  56. }
  57. }
  58.  
  59. function ptpimg_get_api_key() {
  60. if (!window.localStorage.ptpimg_it) window.localStorage.ptpimg_it = JSON.stringify(default_settings);
  61. var settings = JSON.parse(window.localStorage.ptpimg_it);
  62. if (settings.api_key) return new Promise(function (resolve, reject) {
  63. resolve(settings.api_key);
  64. });
  65.  
  66. return new Promise( function (resolve, reject) {
  67. var request = GM.xmlHttpRequest({method: "GET",
  68. url: "https://ptpimg.me/",
  69. onload: function(response) {
  70. if (response.status != 200) reject("Response error " + response.status);
  71. if (response.finalUrl !== "https://ptpimg.me/") reject("Couldn't retrieve api key");
  72. settings.api_key = $(response.response).find("#api_key").first().val();
  73. window.localStorage.ptpimg_it = JSON.stringify(settings);
  74. resolve(settings.api_key);
  75. }
  76. });
  77. });
  78. }
  79.  
  80. function prep_urls(urls) {
  81. return urls.map(function (url) {
  82. if (url.indexOf("reho.st") == -1 && url.indexOf("discogs.com") != -1) return "http://reho.st/" + url;
  83. return url;
  84. });
  85. }
  86.  
  87. function send_images(urls, api_key) {
  88. if (!window.localStorage.ptpimg_it) window.localStorage.ptpimg_it = JSON.stringify(default_settings);
  89. var settings = JSON.parse(window.localStorage.ptpimg_it);
  90. switch (settings.host) {
  91. default:
  92. window.localStorage.ptpimg_it = JSON.stringify(default_settings);
  93. case "lutim":
  94. return lutim_send_images(urls);
  95. case "ptpimg":
  96. return ptpimg_send_images(urls, api_key);
  97. }
  98. }
  99.  
  100. function ptpimg_send_images(urls, api_key) {
  101. return new Promise(function(resolve, reject) {
  102. urls = prep_urls(urls);
  103. var boundary = "--NN-GGn-PTPIMG";
  104. var data = "";
  105. data += boundary + "\n";
  106. data += "Content-Disposition: form-data; name=\"link-upload\"\n\n";
  107. data += urls.join("\n") + "\n";
  108. data += boundary + "\n";
  109. data += "Content-Disposition: form-data; name=\"api_key\"\n\n";
  110. data += api_key + "\n";
  111. data += boundary + "--";
  112. var request = GM.xmlHttpRequest({"method": "POST",
  113. "url": "https://ptpimg.me/upload.php",
  114. "responseType": "json",
  115. "headers": {
  116. "Content-type": "multipart/form-data; boundary=NN-GGn-PTPIMG"
  117. },
  118. "data": data,
  119. "onload": function(response) {
  120. if (response.status != 200) reject("Response error " + response.status);
  121. resolve(response.response.map(function (item) {
  122. return "https://ptpimg.me/" + item.code + "." + item.ext;
  123. }));
  124.  
  125. }
  126. });
  127. });
  128. }
  129.  
  130. function lutim_send_images(urls) {
  131. urls = prep_urls(urls);
  132.  
  133. var promises = urls.map(function (url) {
  134. return new Promise(function (resolve, reject) {
  135. var data = "lutim-file-url="+url+"&"+
  136. "format=json&"+
  137. "first-view=0&"+
  138. "crypt=0&"+
  139. "delete-day=0&";
  140. var request = GM.xmlHttpRequest({"method": "POST",
  141. "url": "https://lut.im/",
  142. "responseType": "json",
  143. "headers": {
  144. "Content-Type": "application/x-www-form-urlencoded"
  145. },
  146. "data": data,
  147. "onload": function (response) {
  148. if (response.status != 200) reject("Response error " + response.status);
  149. if (response.response.success !== true) reject("Failed rehosting image to lut.im");
  150. else {
  151. var msg = response.response.msg;
  152. resolve("https://lut.im/" + msg.short + "." + msg.ext);
  153. }
  154. }
  155. });
  156. });
  157. });
  158. return Promise.all(promises);
  159. }
  160.  
  161. function get_settings() {
  162. if (!window.localStorage.ptpimg_it) window.localStorage.ptpimg_it = JSON.stringify(default_settings);
  163. return JSON.parse(window.localStorage.ptpimg_it);
  164. }
  165.  
  166. function rehost_button() {
  167.  
  168. var settings = get_settings();
  169.  
  170. var button = $(this);
  171. button.prop("disabled", true);
  172. var input = $("input[name='image']");
  173. var url = input.val();
  174. get_api_key().then(function (key) {
  175. return send_images([url], key);
  176. }).then(function (new_urls) {
  177. input.val(new_urls[0]);
  178. button.prop("disabled", false);
  179. }).catch(function (message) {
  180. button.prop("disabled", false);
  181. alert(message);
  182. });
  183. }
  184.  
  185. function rehost_button_alt() {
  186.  
  187. var settings = get_settings();
  188.  
  189. var button = $(this);
  190. button.prop("disabled", true);
  191. var inputs = $("input[name='image[]']");
  192. var urls = inputs.map(function () {
  193. return this.value;
  194. }).get();
  195. get_api_key().then(function (key) {
  196. return send_images(urls, key);
  197. }).then(function (new_urls) {
  198. inputs.map(function (index) {
  199. $(this).val(new_urls[index]);
  200. });
  201. button.prop("disabled", false);
  202. }).catch(function (message) {
  203. button.prop("disabled", false);
  204. alert(message);
  205. });
  206. }
  207.  
  208. function add_cover_button() {
  209.  
  210. var settings = get_settings();
  211.  
  212. $("input[name='image']").after("<input type='button' value='Rehost It!' class='rehost_it_cover'>");
  213. $(".rehost_it_cover").click(rehost_button);
  214. }
  215.  
  216. function add_cover_button_alt() {
  217.  
  218. var settings = get_settings();
  219.  
  220. $(".box.box_image a.brackets[href='#']").click(function () {
  221. if ($("#rehost_it_cover").length === 0) {
  222. $("#add_cover").after("<input type='button' value='Rehost It!' id='rehost_it_cover'>");
  223. $("#rehost_it_cover").click(rehost_button_alt);
  224. }
  225. });
  226. }
  227.  
  228. function add_config() {
  229.  
  230. var settings = get_settings();
  231.  
  232. $(".main_column").append("<table id='pth_ptpimg_it'><tbody></tbody</table>");
  233. $("#pth_ptpimg_it>tbody").append("<tr class='colhead_dark'><td colspan='2'><strong>PTPIMG It Settings</strong></td></tr>");
  234. $("#pth_ptpimg_it>tbody").append("<tr><td class='label'>PTPIMG API Key</td><td><input type='text' id='api_key' placeholder='Enter your API Key here' value='" + (settings.api_key ? settings.api_key : "") + "'></td></tr>");
  235. $("#pth_ptpimg_it>tbody").append("<tr><td class='label'>Alert when done ?</td><td><input type='checkbox' id='alert_when_done' value='Alert when done ?'" + (settings.alert_when_done ? "checked" : "") + "></td></tr>");
  236. $("#pth_ptpimg_it>tbody").append("<tr><td class='label'>Image host provider:</td><td><select id='image_host_provider'><option value='lutimg' " + (settings.host === "lutimg" ? "selected": "") + ">lut.img</option><option value='ptpimg' " + (settings.host === "ptpimg" ? "selected": "") + ">ptpimg.me</option></select></td></tr>");
  237. $("#pth_ptpimg_it>tbody").append("<tr><td colspan='2'><input type='button' id='save' value='Save settings'></td></tr>");
  238.  
  239. $("#pth_ptpimg_it #save").click(function () {
  240. var settings_form = $("#pth_ptpimg_it");
  241. settings = {
  242. alert_when_done: settings_form.find("#alert_when_done").prop("checked"),
  243. api_key: $("#api_key").val() === "" ? undefined : $("#api_key").val(),
  244. host: $("#image_host_provider").val()
  245. };
  246. window.localStorage.ptpimg_it = JSON.stringify(settings);
  247. alert("Settings saved !");
  248. });
  249. }
  250.  
  251. (function() {
  252. 'use strict';
  253. GM.addStyle(css);
  254. if (/user\.php\?action=edit/.test(window.location.href)) {
  255. add_config();
  256. } else if (/torrents\.php\?id=/.test(window.location.href)) {
  257. add_cover_button_alt();
  258. } else {
  259. $("#categories").on("change", function () {
  260. setTimeout(add_cover_button, 250);
  261. });
  262. add_cover_button();
  263. }
  264. })();
Add Comment
Please, Sign In to add comment