Upload image from upload page

Upload album art from within the PTH upload page

当前为 2016-12-17 提交的版本,查看 最新版本

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