Upload image from upload page

Upload album art from within the PTH upload page

当前为 2017-02-20 提交的版本,查看 最新版本

  1. // ==UserScript==
  2. // @name Upload image from upload page
  3. // @version 1.4
  4. // @description Upload album art from within the PTH upload page
  5. // @author Chameleon
  6. // @include http*://*redacted.ch/upload.php*
  7. // @include http*://*redacted.ch/forums.php*threadid=1725*
  8. // @include http*://*redacted.ch/artist.php*action=edit*
  9. // @include http*://*redacted.ch/torrents.php*action=editgroup*
  10. // @grant GM_xmlhttpRequest
  11. // @namespace https://greasyfork.org/users/87476
  12. // ==/UserScript==
  13.  
  14. (function() {
  15. 'use strict';
  16.  
  17. if(window.location.href.indexOf("threadid=1725") != -1)
  18. showSettings();
  19. else if(window.location.href.indexOf('upload.php') != -1)
  20. {
  21. showUpload();
  22. }
  23. else if(window.location.href.indexOf('artist.php') != -1 || window.location.href.indexOf('torrents.php') != -1)
  24. {
  25. showArtistEdit();
  26. }
  27. }());
  28.  
  29. function showSettings(message)
  30. {
  31. var div=document.getElementById('rehostToSettings');
  32. if(!div)
  33. {
  34. var before = document.getElementsByClassName('forum_post')[0];
  35. div = document.createElement('div');
  36. div.setAttribute('id', 'rehostToSettings');
  37. before.parentNode.insertBefore(div, before);
  38. div.setAttribute('style', 'width: 100%; text-align: center; padding-bottom: 10px;');
  39. div.setAttribute('class', 'box');
  40. }
  41. div.innerHTML = '<h2>Upload image from upload page Settings</h2><br />';
  42. var settings = getSettings();
  43.  
  44. var a=document.createElement('a');
  45. a.href='javascript:void(0);';
  46. a.innerHTML = 'Use image host: '+settings.site;
  47. a.addEventListener('click', changeSite.bind(undefined, a, div), false);
  48. div.appendChild(a);
  49. div.appendChild(document.createElement('br'));
  50.  
  51. var labelStyle = '';
  52.  
  53. var label = document.createElement('span');
  54. label.setAttribute('style', labelStyle);
  55. label.innerHTML = 'ptpimg.me API Key: ';
  56. div.appendChild(label);
  57. var input=document.createElement('input');
  58. input.setAttribute('style', 'width: 21em;');
  59. input.placeholder='ptpimg.me API Key';
  60. input.value = settings.apiKey ? settings.apiKey:'';
  61. div.appendChild(input);
  62. input.addEventListener('keyup', changeSettings.bind(undefined, div), false);
  63.  
  64. var a=document.createElement('a');
  65. a.href='javascript:void(0);';
  66. a.innerHTML = 'Get ptpimg.me API Key';
  67. div.appendChild(document.createElement('br'));
  68. div.appendChild(a);
  69. div.appendChild(document.createTextNode(' '));
  70. var s=document.createElement('span');
  71. s.innerHTML = message ? message : '';
  72. div.appendChild(s);
  73. a.addEventListener('click', getAPIKey.bind(undefined, input, s, div), false);
  74. }
  75.  
  76. function getAPIKey(input, span, div)
  77. {
  78. span.innerHTML = 'Loading ptpimg.me';
  79. /*var xhr=new XMLHttpRequest();
  80. xhr.open('GET', "https://ptpimg.me");
  81. xhr.onreadystatechange = xhr_func.bind(undefined, span, xhr, gotAPIKey.bind(undefined, input, span, div), rehost.bind(undefined, input, span, div));
  82. xhr.send();*/
  83. GM_xmlhttpRequest({
  84. method: "GET",
  85. url: 'https://ptpimg.me',
  86. onload: function(response) { if(response.status == 200) {gotAPIKey(input, span, div, response.responseText); } else { span.innerHTML = 'ptpimg.me error: '+response.status; } }
  87. });
  88.  
  89. }
  90.  
  91. function gotAPIKey(input, span, div, response)
  92. {
  93. var key=response.split("value='")[1].split("'")[0];
  94. if(key.length != 36)
  95. {
  96. span.innerHTML = "You aren't logged in to ptpimg.me";
  97. return;
  98. }
  99. input.value=key;
  100. changeSettings(div, 0, "Successfully added API Key");
  101. }
  102.  
  103. function changeSite(a, div)
  104. {
  105. if(a.innerHTML.indexOf('imgur.com') != -1)
  106. {
  107. a.innerHTML = a.innerHTML.replace('imgur.com', 'ptpimg.me');
  108. }
  109. else if(a.innerHTML.indexOf('ptpimg.me') != -1)
  110. {
  111. a.innerHTML = a.innerHTML.replace('ptpimg.me', 'imgur.com');
  112. }
  113.  
  114. changeSettings(div);
  115. }
  116.  
  117. function changeSettings(div, nul, message)
  118. {
  119. var settings = getSettings();
  120. var as=div.getElementsByTagName('a');
  121. if(as[0].innerHTML.indexOf('imgur.com') != -1)
  122. settings.site = 'imgur.com';
  123. else if(as[0].innerHTML.indexOf('ptpimg.me') != -1)
  124. settings.site = 'ptpimg.me';
  125.  
  126. var inputs=div.getElementsByTagName('input');
  127. settings.apiKey = inputs[0].value;
  128. window.localStorage.ptpimgAPIKey = settings.apiKey;
  129.  
  130. window.localStorage.uploadFromUploadPageSettings = JSON.stringify(settings);
  131. showSettings(message);
  132. }
  133.  
  134. function getSettings()
  135. {
  136. var settings = window.localStorage.uploadFromUploadPageSettings;
  137. if(!settings)
  138. {
  139. settings = {site:'imgur.com', apiKey:window.localStorage.ptpimgAPIKey ? window.localStorage.ptpimgAPIKey : ''};
  140. }
  141. else
  142. settings = JSON.parse(settings);
  143. return settings;
  144. }
  145.  
  146. function showArtistEdit()
  147. {
  148. var image=document.getElementsByName('image')[0];
  149. var div=document.createElement('div');
  150. image.parentNode.insertBefore(div, image);
  151. div.appendChild(image);
  152. image.setAttribute('id', 'image');
  153. showUpload();
  154. }
  155.  
  156. function showUpload()
  157. {
  158. var imageInput = document.getElementById('image');
  159. var parent = imageInput.parentNode;
  160. if(imageInput.parentNode.innerHTML.indexOf('Auto-rehost') == -1)
  161. {
  162. var span=document.createElement('span');
  163. var a=document.createElement('a');
  164. a.href='javascript:void(0);';
  165. a.innerHTML = 'Auto-rehost: Off';
  166. a.addEventListener('click', toggleAutoRehost.bind(undefined, a, imageInput, span), false);
  167. parent.appendChild(document.createTextNode(' '));
  168. parent.appendChild(a);
  169. parent.appendChild(document.createTextNode(' '));
  170. parent.appendChild(span);
  171. if(window.localStorage.autoUpload == "true")
  172. {
  173. imageInput.setAttribute('autorehost', 'true');
  174. a.innerHTML = 'Auto-rehost: On';
  175. }
  176. imageInput.addEventListener('keyup', rehost.bind(undefined, imageInput, span), false);
  177. }
  178. var file = document.createElement('input');
  179. file.type='file';
  180. parent.appendChild(file);
  181. var status = document.createElement('div');
  182. parent.appendChild(status);
  183. file.addEventListener('change', uploadFile.bind(undefined, status), false);
  184. file.accept="image/*";
  185. var dropzone = document.createElement('div');
  186. parent.appendChild(dropzone);
  187. dropzone.addEventListener("dragenter", dragenter, false);
  188. dropzone.addEventListener("dragover", dragenter, false);
  189. dropzone.addEventListener("drop", drop.bind(undefined, status), false);
  190. dropzone.innerHTML = 'Or drop files here';
  191. dropzone.setAttribute('style', 'width: 400px; height: 30px; background: rgba(64,64,64,0.8); border: dashed; border-radius: 10px; margin: auto; text-align: center; font-size: 20px;');
  192. }
  193.  
  194. function dragenter(event)
  195. {
  196. event.preventDefault();
  197. event.stopPropagation();
  198. }
  199.  
  200. function drop(status, event)
  201. {
  202. event.preventDefault();
  203. event.stopPropagation();
  204. var dt = event.dataTransfer;
  205. var files = dt.files;
  206. uploadFile(status, {target:{files:files}});
  207. }
  208.  
  209. function rehost(imageInput, span)
  210. {
  211. if(imageInput.getAttribute('autorehost') != "true")
  212. return;
  213. var whitelisted = ["imgur.com", "ptpimg.me"];
  214. if(imageInput.value.length < 1)
  215. return;
  216. for(var i=0; i<whitelisted.length; i++)
  217. {
  218. var whitelist=whitelisted[i];
  219. if(imageInput.value.indexOf(whitelist) != -1)
  220. return;
  221. }
  222.  
  223. if(imageInput.value.indexOf("discogs.com") != -1)
  224. {
  225. imageInput.value = "http://reho.st/"+imageInput.value;
  226. }
  227.  
  228. span.innerHTML = 'Rehosting';
  229. var formData = new FormData();
  230. formData.append('image', imageInput.value);
  231. if(imageInput.getAttribute('working') == "true")
  232. return;
  233. imageInput.setAttribute('working', "true");
  234. window.setTimeout(unworking.bind(undefined, imageInput), 1000);
  235.  
  236. var settings = getSettings();
  237.  
  238. if(settings.site == 'imgur.com')
  239. {
  240. var xhr = new XMLHttpRequest();
  241. xhr.open('POST', 'https://api.imgur.com/3/image');
  242. xhr.setRequestHeader('Authorization', 'Client-ID 735033a56fe790b');
  243. xhr.onreadystatechange = xhr_func.bind(undefined, span, xhr, rehosted.bind(undefined, imageInput, span), rehost.bind(undefined, imageInput, span));
  244. xhr.send(formData);
  245. }
  246. else if(settings.site == 'ptpimg.me')
  247. {
  248. if(!settings.apiKey || settings.apiKey.length != 36)
  249. {
  250. a.innerHTML = 'No valid ptpimg.me API key set';
  251. return;
  252. }
  253. /*var formData = new FormData();
  254. formData.append('link-upload', image_input.value);
  255. formData.append('api_key', 'xx');
  256. // ptpimg.me doesn't have 'Access-Control-Allow-Origin' set
  257. var xhr = new XMLHttpRequest();
  258. xhr.open('POST', 'https://ptpimg.me/upload.php');
  259. xhr.onreadystatechange = xhr_func.bind(undefined, a, xhr, uploaded.bind(undefined, a, form, settings), doRehost.bind(undefined, a, image_input, form, settings));
  260. xhr.send(formData);*/
  261. // use GM_xmlhttpRequest for cross-domain
  262. GM_xmlhttpRequest({
  263. method: "POST",
  264. url: 'https://ptpimg.me/upload.php',
  265. data: "link-upload="+encodeURIComponent(imageInput.value)+'&api_key='+settings.apiKey,
  266. headers: {
  267. "Content-Type": "application/x-www-form-urlencoded"
  268. },
  269. onload: function(response) { rehosted(imageInput, span, response.responseText); }
  270. });
  271. }
  272. }
  273.  
  274. function unworking(input)
  275. {
  276. input.setAttribute('working', "false");
  277. }
  278.  
  279. function rehosted(imageInput, span, response)
  280. {
  281. var settings = getSettings();
  282. var newLink='';
  283. try
  284. {
  285. if(settings.site == 'imgur.com')
  286. newLink = JSON.parse(response).data.link.replace(/http:/, 'https:');
  287. else if(settings.site == 'ptpimg.me')
  288. {
  289. var r=JSON.parse(response)[0];
  290. newLink = "https://ptpimg.me/"+r.code+'.'+r.ext;
  291. }
  292. }
  293. catch(err)
  294. {
  295. span.innerHTML = err.message;
  296. return;
  297. }
  298. span.innerHTML = 'Rehosted';
  299. imageInput.value = newLink;
  300. }
  301.  
  302. function toggleAutoRehost(a, input, span)
  303. {
  304. if(a.innerHTML.indexOf('Off') != -1)
  305. {
  306. input.setAttribute('autorehost', 'true');
  307. a.innerHTML = 'Auto-rehost: On';
  308. window.localStorage.autoUpload = 'true';
  309. rehost(input, span);
  310. }
  311. else
  312. {
  313. input.setAttribute('autorehost', 'false');
  314. a.innerHTML = 'Auto-rehost: Off';
  315. window.localStorage.autoUpload = 'false';
  316. }
  317. }
  318.  
  319. function uploadFile(status, event)
  320. {
  321. var files=event.target.files;
  322. for(var i=0; i<files.length; i++)
  323. {
  324. var f=files[i];
  325. if(f.type.indexOf("image") != -1)
  326. {
  327. status.innerHTML = 'Uploading...';
  328. upload(status, f);
  329. break;
  330. }
  331. else
  332. {
  333. status.innerHTML = 'Not an image';
  334. }
  335. }
  336. }
  337.  
  338. function upload(status, file)
  339. {
  340. var settings = getSettings();
  341.  
  342. if(settings.site == 'imgur.com')
  343. {
  344. var formData = new FormData();
  345. formData.append('image', file);
  346. var xhr = new XMLHttpRequest();
  347. xhr.open('POST', 'https://api.imgur.com/3/image');
  348. xhr.setRequestHeader('Authorization', 'Client-ID 735033a56fe790b');
  349. xhr.onreadystatechange = xhr_func.bind(undefined, status, xhr, uploaded.bind(undefined, status), upload.bind(undefined, status, file));
  350. xhr.send(formData);
  351. }
  352. else if(settings.site == 'ptpimg.me')
  353. {
  354. if(!settings.apiKey || settings.apiKey.length != 36)
  355. {
  356. a.innerHTML = 'No valid ptpimg.me API key set';
  357. return;
  358. }
  359. /*var formData = new FormData();
  360. formData.append('link-upload', image_input.value);
  361. formData.append('api_key', 'xx');
  362. // ptpimg.me doesn't have 'Access-Control-Allow-Origin' set
  363. var xhr = new XMLHttpRequest();
  364. xhr.open('POST', 'https://ptpimg.me/upload.php');
  365. xhr.onreadystatechange = xhr_func.bind(undefined, a, xhr, uploaded.bind(undefined, a, form, settings), doRehost.bind(undefined, a, image_input, form, settings));
  366. xhr.send(formData);*/
  367. // use GM_xmlhttpRequest for cross-domain
  368. var formData = new FormData();
  369. formData.append('file-upload[0]', file);
  370. formData.append('api_key', settings.apiKey);
  371. GM_xmlhttpRequest({
  372. method: "POST",
  373. url: 'https://ptpimg.me/upload.php',
  374. //binary: true,
  375. data: formData,
  376. /* headers: {
  377. "Content-Type": "multipart/form-data"
  378. },*/
  379. onload: function(response) {
  380. if(response.status == 200)
  381. {
  382. uploaded(status, response.responseText);
  383. }
  384. else
  385. {
  386. console.log("Failed to upload: \n"+response.responseHeaders+' '+response.status+' '+response.statusText);
  387. status.innerHTML = "Failed to upload to ptpimg.me: "+response.status;
  388. return;
  389. }
  390. }
  391. });
  392. }
  393. }
  394.  
  395. function uploaded(status, response)
  396. {
  397. var settings=getSettings();
  398. console.log(response);
  399. var newLink='';
  400. try
  401. {
  402. if(settings.site == 'imgur.com')
  403. newLink = JSON.parse(response).data.link;
  404. else if(settings.site == 'ptpimg.me')
  405. {
  406. var r=JSON.parse(response)[0];
  407. newLink = "https://ptpimg.me/"+r.code+'.'+r.ext;
  408. }
  409. }
  410. catch(err)
  411. {
  412. status.innerHTML = err.message;
  413. status.style.color = 'red';
  414. return;
  415. }
  416.  
  417. status.innerHTML = 'Uploaded<br />';
  418. var img=document.createElement('img');
  419. var a=document.createElement('a');
  420. status.appendChild(a);
  421. status.appendChild(document.createElement('br'));
  422. status.appendChild(img);
  423. a.innerHTML='Hide image';
  424. a.href='javascript:void(0);';
  425. a.addEventListener('click', toggleImage.bind(undefined, a, img), false);
  426. img.src=newLink;
  427. document.getElementById('image').value = newLink;
  428. }
  429.  
  430. function toggleImage(a, img)
  431. {
  432. if(img.style.display=='none')
  433. {
  434. img.style.display='initial';
  435. a.innerHTML = 'Hide image';
  436. }
  437. else
  438. {
  439. img.style.display='none';
  440. a.innerHTML = 'Show image';
  441. }
  442. }
  443.  
  444. function xhr_func(messageDiv, xhr, func, repeatFunc)
  445. {
  446. if(xhr.readyState == 4)
  447. {
  448. if(xhr.status == 200)
  449. func(xhr.responseText);
  450. else
  451. {
  452. messageDiv.innerHTML = 'Error: '+xhr.status+'<br />retrying in 1 second';
  453. window.setTimeout(repeatFunc, 1000);
  454. }
  455. }
  456. }