Advertisement
Guest User

Untitled

a guest
May 19th, 2019
115
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 9.88 KB | None | 0 0
  1. // ==UserScript==
  2. // @author BZHDeveloper, roger21
  3. // @name [HFR] Téléversement d'images
  4. // @version 0.4.3.1
  5. // @namespace forum.hardware.fr
  6. // @description envoi d'image sur reho.st (copie du BBCode si réussite) ou conversion de texte enrichi
  7. // @icon http://reho.st/self/40f387c9f48884a57e8bbe05e108ed4bd59b72ce.png
  8. // @downloadURL http://breizhodrome.free.fr/hfr/scripts/hfr_cc.user.js
  9. // @updateURL http://breizhodrome.free.fr/hfr/scripts/hfr_cc.user.js
  10. // @require http://breizhodrome.free.fr/hfr/scripts/hfr.js
  11. // @include https://forum.hardware.fr/message.php*
  12. // @include https://forum.hardware.fr/forum2.php*
  13. // @include https://forum.hardware.fr/hfr/*
  14. // @noframes
  15. // @grant GM_info
  16. // @grant GM_deleteValue
  17. // @grant GM_getValue
  18. // @grant GM_listValues
  19. // @grant GM_setValue
  20. // @grant GM_getResourceText
  21. // @grant GM_getResourceURL
  22. // @grant GM_addStyle
  23. // @grant GM_log
  24. // @grant GM_openInTab
  25. // @grant GM_registerMenuCommand
  26. // @grant GM_setClipboard
  27. // @grant GM_xmlhttpRequest
  28. // ==/UserScript==
  29.  
  30. // Historique
  31. // 0.0.3 - [roger21] ajout d'une icône de chargement
  32. // 0.0.6.3 - insertion des données au curseur
  33. // 0.0.7 - [roger21] possibilité de copié/collé dans une réponse rapide
  34. // 0.0.8 - Glisser/déposer les fichiers depuis un explorateur de fichiers vers la zone de texte
  35. // 0.1.1 - Maintenant que tout est bon, on vire imgur et on met reho.st
  36. // 0.1.2 - Ajout d'une balise [url] vers l'image originale
  37. // 0.2.0 - Réintégration d'imgur, si l'image fait > 2 Mo, basculement vers imgur, sinon rester sur reho.st
  38. // 0.2.1 - Possibilité d'envoi de plusieurs images
  39. // 0.2.2 - [roger21] envoi des images dans l'ordre.
  40. // 0.2.3 - compatibilité ViolentMonkey
  41. // 0.3.0 - si les données glisées sont du type "text/uri-list", les télécharger et passer à la fonction "process".
  42. // 0.3.2 - correction du glisser/déposer des images
  43. // 0.3.3 - conversion des URI base64 en Blob
  44. // 0.3.4 - certaines images ont une balise "data-src", et non "src". Allez comprendre
  45. // 0.3.9 - ajout d'un bouton type "image/file" à chaque zone de texte, fixant le problème de glisser/déposer de certains explorateurs de fichiers.
  46. // 0.4.0.1 - compatibilité HTTPS.
  47. // 0.4.2 - correction du bouton d'envoi d'images (taille / position)
  48. // 0.4.3 - Compatibilité TamperMonkey / GreaseMonkey
  49. // 0.4.3.1 - taille du bouton d'envoi d'image à 24px (fixe le conflit avec des scripts comme "smileys favoris")
  50.  
  51. var throbber = new HfrThrobber();
  52.  
  53. function process (area, file) {
  54. var form = new FormData();
  55. var options = {
  56. "imgur" : {
  57. "form" : "image",
  58. "url" : "https://api.imgur.com/3/image"
  59. },
  60. "rehost" : {
  61. "form" : "fichier",
  62. "url" : "https://reho.st/upload"
  63. }
  64. };
  65.  
  66. var host = "rehost";
  67. if (file.size >= 2000000)
  68. host = "imgur";
  69.  
  70. form.append (options[host].form, file);
  71.  
  72. // chargement
  73. throbber.display();
  74.  
  75. GM_xmlhttpRequest({
  76. method : "POST",
  77. data : form,
  78. context : {
  79. textarea : area
  80. },
  81. headers : {
  82. "Authorization" : "Client-ID d1619618d2ac442"
  83. },
  84. url : options[host].url,
  85. onabort : throbber.hide,
  86. onerror : throbber.hide,
  87. ontimeout : throbber.hide,
  88. onload : function (response) {
  89. // fin du chargement
  90. throbber.hide();
  91.  
  92. var src = "";
  93. var link = "";
  94. var success = false;
  95.  
  96. var textarea = area;
  97. if (this.context != null)
  98. textarea = this.context.textarea;
  99.  
  100. if (host == "imgur") {
  101. var object = JSON.parse (response.responseText);
  102. if (object.success) {
  103. success = true;
  104. src = object.data.link;
  105. if (object.data.type != "image/gif") {
  106. src = src.replace (object.data.id, object.data.id + "l");
  107. object.data.id = object.data.id + "l";
  108. }
  109. link = object.data.link;
  110. }
  111. }
  112. else {
  113. var doc = new DOMParser().parseFromString (response.responseText, "text/html");
  114. if (doc.querySelector ("#maincontent > img")) {
  115. success = true;
  116. src = doc.querySelector ("#maincontent > img").getAttribute ("src").replace ("/thumb/", "/preview/");
  117. link = doc.querySelector ("#maincontent > img").getAttribute ("src").replace ("/thumb/", "/");
  118. }
  119. }
  120. if (success) {
  121. insert_text_at_cursor (textarea, "[url=" + link + "][img]" + src + "[/img][/url]");
  122. if (textarea.files != null && textarea.files_index < textarea.files.length)
  123. process (textarea, textarea.files.item (textarea.files_index++));
  124. }
  125. }
  126. });
  127. }
  128.  
  129. function download (area, uri) {
  130. GM_xmlhttpRequest({
  131. method : "GET",
  132. url : uri,
  133. responseType : "blob",
  134. context : {
  135. textarea : area
  136. },
  137. onload : function (response) {
  138. var textarea = area;
  139. if (this.context != null)
  140. textarea = this.context.textarea;
  141. process (textarea, response.response);
  142. }
  143. });
  144. }
  145.  
  146. function drop (event) {
  147. var dt = event.dataTransfer;
  148. console.log (dt.types);
  149. if (dt.types.includes ("text/uri-list")) {
  150. if (dt.types.includes ("text/html")) {
  151. console.log (dt.getData ("text/html"));
  152. var doc = new DOMParser().parseFromString (dt.getData ("text/html"), "text/html");
  153. var img = doc.querySelector("img");
  154. if (img != null) {
  155. var src = img.getAttribute ("src");
  156. if (img.getAttribute ("data-src") != null)
  157. src = img.getAttribute ("data-src");
  158. if (src.indexOf ("data:image") == 0) {
  159. var blob = dataURItoBlob (src);
  160. process (this, blob);
  161. }
  162. else
  163. download (this, src);
  164. return event.preventDefault();
  165. }
  166. }
  167. download (this, dt.getData ("URL"));
  168. return event.preventDefault();
  169. }
  170. this.files = dt.files;
  171. this.files_index = 0;
  172. if (this.files.length > 0) {
  173. process (this, this.files.item (this.files_index++));
  174. return event.preventDefault();
  175. }
  176. }
  177.  
  178. function pasting (event) {
  179. console.log (event.clipboardData.types);
  180. if (event.clipboardData.types.includes ("text/uri-list")) {
  181. download (this, event.clipboardData.getData ("URL"));
  182. return event.preventDefault();
  183. }
  184. var files = event.clipboardData.files;
  185. if (files.length > 0) {
  186. process (this, files.item (0));
  187. return event.preventDefault();
  188. }
  189. }
  190.  
  191. function create_button (callback) {
  192. var div = document.createElement ("div");
  193. div.setAttribute ("style", "width:24px; height:24px; background:red url('data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAABgAAAAYCAMAAADXqc3KAAAAq1BMVEUAAAD///////8AAAABAQEGBgYTExMXFxcfHx8iIiIjIyMqKiorKystLS0vLy8wMDAyMjI+Pj5CQkJfX19jY2NlZWVmZmZpaWlqamp9fX2JiYmbm5ugoKChoaGioqKlpaWmpqapqamqqqqxsbG0tLS4uLi6urq7u7vBwcHCwsLDw8PU1NTY2Njd3d3e3t7f39/h4eHp6ens7Ozz8/P29vb4+Pj5+fn+/v7///8MV1ZKAAAAA3RSTlMAwcJd/YFNAAAAoElEQVR42ozQUwLEMACE4Wht27a3c/+L1Wb+169IhiTHEBPVAathpDkMGPJInUzYXHztfXBRZoMl7B5+WIiaWIMYBaFbR7UXB1P9jaUDkX+wGIAeY9CjITC2+Y37o+Ur+sannWuVROHuh8nhcNg28ifg3azMwpOUr/rv8Sz6J9nd9L7GsRj+x/A/4ibZP3ydZWaPh3kn0gTacCUGJtyJBwCrETM5o222rQAAAABJRU5ErkJggg==') no-repeat");
  194. var input = document.createElement ("input");
  195. input.setAttribute ("style", "width : 24px; height: 24px; opacity:0; filter:alpha(opacity=0); float:right; cursor:pointer");
  196. input.setAttribute ("type", "file");
  197. input.setAttribute ("title", "Glissez vos images ici");
  198. input.setAttribute ("class", "hfr-upload-button");
  199. input.onchange = function() {
  200. callback (input);
  201. };
  202. div.appendChild (input);
  203. return div;
  204. }
  205.  
  206. function focus (event) {
  207. if (this.parentNode.querySelector (".hfr-upload-button") != null)
  208. return;
  209.  
  210. var current_area = this;
  211. var upload_button = create_button (function (input) {
  212. process (current_area, input.files.item (0));
  213. });
  214.  
  215. if (this.classList.contains ("reponserapide")) {
  216. upload_button.style.marginLeft = "50%";
  217. var button = this.parentNode.querySelector ("input[type='submit']");
  218. this.parentNode.insertBefore (upload_button, this);
  219. }
  220. else if (this.getAttribute ("id").indexOf ("rep_editin_") == 0) {
  221. var button = this.parentNode.querySelector ("div > input[accesskey='s']");
  222. var div_center = button.parentNode;
  223. div_center.setAttribute ("align", "");
  224. div_center.parentNode.insertBefore (upload_button, div_center);
  225. }
  226. else {
  227. upload_button.style.width = "24px";
  228. var button = this.parentNode.querySelector ("input[type='submit']");
  229. button.parentNode.insertBefore (upload_button, button);
  230. }
  231. /*
  232. var buttons = this.parentNode.querySelectorAll ("input[type='button'] , input[type='submit']");
  233. var button = buttons.item (buttons.length - 1);
  234. console.log (this.getAttribute ("id").indexOf ("rep_editin_"));
  235. if (document.querySelector ("#submitreprap") != null) {
  236. button = document.querySelector ("#submitreprap");
  237. button.parentNode.appendChild(div, button.nextSibling);
  238. }
  239. else if (this.getAttribute ("id").indexOf ("rep_editin_") == 0) {
  240. button = this.parentElement.querySelector ("[value='Valider votre message']");
  241. console.log (button);
  242. button.parentNode.insertBefore (div, button);
  243. }
  244. else
  245. button.parentNode.appendChild(div, button.nextSibling);
  246. */
  247. }
  248.  
  249. var content_form = document.querySelector("#content_form");
  250. if (content_form != null) {
  251. content_form.addEventListener("focus", focus);
  252. content_form.addEventListener('paste', pasting);
  253. content_form.addEventListener('drop', drop);
  254. }
  255.  
  256. var observer=new MutationObserver(function(mutations, observer){
  257. var textareas=document.querySelectorAll("textarea[id^=\"rep_editin_\"]" );
  258. if(textareas.length){
  259. for(var textarea of textareas) {
  260. textarea.removeEventListener('focus', focus, false);
  261. textarea.addEventListener('focus', focus, false);
  262. textarea.removeEventListener('paste', pasting, false);
  263. textarea.addEventListener('paste', pasting, false);
  264. textarea.removeEventListener('drop', drop, false);
  265. textarea.addEventListener('drop', drop, false);
  266. }
  267. }
  268. });
  269. observer.observe(document, {attributes: false, childList: true, characterData: false, subtree: true});
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement