Upload image from upload page

Upload album art from within the PTH upload page

当前为 2018-03-03 提交的版本,查看 最新版本

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