Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // ==UserScript==
- // @author BZHDeveloper, roger21
- // @name [HFR] Téléversement d'images
- // @version 0.4.3.1
- // @namespace forum.hardware.fr
- // @description envoi d'image sur reho.st (copie du BBCode si réussite) ou conversion de texte enrichi
- // @icon http://reho.st/self/40f387c9f48884a57e8bbe05e108ed4bd59b72ce.png
- // @downloadURL http://breizhodrome.free.fr/hfr/scripts/hfr_cc.user.js
- // @updateURL http://breizhodrome.free.fr/hfr/scripts/hfr_cc.user.js
- // @require http://breizhodrome.free.fr/hfr/scripts/hfr.js
- // @include https://forum.hardware.fr/message.php*
- // @include https://forum.hardware.fr/forum2.php*
- // @include https://forum.hardware.fr/hfr/*
- // @noframes
- // @grant GM_info
- // @grant GM_deleteValue
- // @grant GM_getValue
- // @grant GM_listValues
- // @grant GM_setValue
- // @grant GM_getResourceText
- // @grant GM_getResourceURL
- // @grant GM_addStyle
- // @grant GM_log
- // @grant GM_openInTab
- // @grant GM_registerMenuCommand
- // @grant GM_setClipboard
- // @grant GM_xmlhttpRequest
- // ==/UserScript==
- // Historique
- // 0.0.3 - [roger21] ajout d'une icône de chargement
- // 0.0.6.3 - insertion des données au curseur
- // 0.0.7 - [roger21] possibilité de copié/collé dans une réponse rapide
- // 0.0.8 - Glisser/déposer les fichiers depuis un explorateur de fichiers vers la zone de texte
- // 0.1.1 - Maintenant que tout est bon, on vire imgur et on met reho.st
- // 0.1.2 - Ajout d'une balise [url] vers l'image originale
- // 0.2.0 - Réintégration d'imgur, si l'image fait > 2 Mo, basculement vers imgur, sinon rester sur reho.st
- // 0.2.1 - Possibilité d'envoi de plusieurs images
- // 0.2.2 - [roger21] envoi des images dans l'ordre.
- // 0.2.3 - compatibilité ViolentMonkey
- // 0.3.0 - si les données glisées sont du type "text/uri-list", les télécharger et passer à la fonction "process".
- // 0.3.2 - correction du glisser/déposer des images
- // 0.3.3 - conversion des URI base64 en Blob
- // 0.3.4 - certaines images ont une balise "data-src", et non "src". Allez comprendre
- // 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.
- // 0.4.0.1 - compatibilité HTTPS.
- // 0.4.2 - correction du bouton d'envoi d'images (taille / position)
- // 0.4.3 - Compatibilité TamperMonkey / GreaseMonkey
- // 0.4.3.1 - taille du bouton d'envoi d'image à 24px (fixe le conflit avec des scripts comme "smileys favoris")
- var throbber = new HfrThrobber();
- function process (area, file) {
- var form = new FormData();
- var options = {
- "imgur" : {
- "form" : "image",
- "url" : "https://api.imgur.com/3/image"
- },
- "rehost" : {
- "form" : "fichier",
- "url" : "https://reho.st/upload"
- }
- };
- var host = "rehost";
- if (file.size >= 2000000)
- host = "imgur";
- form.append (options[host].form, file);
- // chargement
- throbber.display();
- GM_xmlhttpRequest({
- method : "POST",
- data : form,
- context : {
- textarea : area
- },
- headers : {
- "Authorization" : "Client-ID d1619618d2ac442"
- },
- url : options[host].url,
- onabort : throbber.hide,
- onerror : throbber.hide,
- ontimeout : throbber.hide,
- onload : function (response) {
- // fin du chargement
- throbber.hide();
- var src = "";
- var link = "";
- var success = false;
- var textarea = area;
- if (this.context != null)
- textarea = this.context.textarea;
- if (host == "imgur") {
- var object = JSON.parse (response.responseText);
- if (object.success) {
- success = true;
- src = object.data.link;
- if (object.data.type != "image/gif") {
- src = src.replace (object.data.id, object.data.id + "l");
- object.data.id = object.data.id + "l";
- }
- link = object.data.link;
- }
- }
- else {
- var doc = new DOMParser().parseFromString (response.responseText, "text/html");
- if (doc.querySelector ("#maincontent > img")) {
- success = true;
- src = doc.querySelector ("#maincontent > img").getAttribute ("src").replace ("/thumb/", "/preview/");
- link = doc.querySelector ("#maincontent > img").getAttribute ("src").replace ("/thumb/", "/");
- }
- }
- if (success) {
- insert_text_at_cursor (textarea, "[url=" + link + "][img]" + src + "[/img][/url]");
- if (textarea.files != null && textarea.files_index < textarea.files.length)
- process (textarea, textarea.files.item (textarea.files_index++));
- }
- }
- });
- }
- function download (area, uri) {
- GM_xmlhttpRequest({
- method : "GET",
- url : uri,
- responseType : "blob",
- context : {
- textarea : area
- },
- onload : function (response) {
- var textarea = area;
- if (this.context != null)
- textarea = this.context.textarea;
- process (textarea, response.response);
- }
- });
- }
- function drop (event) {
- var dt = event.dataTransfer;
- console.log (dt.types);
- if (dt.types.includes ("text/uri-list")) {
- if (dt.types.includes ("text/html")) {
- console.log (dt.getData ("text/html"));
- var doc = new DOMParser().parseFromString (dt.getData ("text/html"), "text/html");
- var img = doc.querySelector("img");
- if (img != null) {
- var src = img.getAttribute ("src");
- if (img.getAttribute ("data-src") != null)
- src = img.getAttribute ("data-src");
- if (src.indexOf ("data:image") == 0) {
- var blob = dataURItoBlob (src);
- process (this, blob);
- }
- else
- download (this, src);
- return event.preventDefault();
- }
- }
- download (this, dt.getData ("URL"));
- return event.preventDefault();
- }
- this.files = dt.files;
- this.files_index = 0;
- if (this.files.length > 0) {
- process (this, this.files.item (this.files_index++));
- return event.preventDefault();
- }
- }
- function pasting (event) {
- console.log (event.clipboardData.types);
- if (event.clipboardData.types.includes ("text/uri-list")) {
- download (this, event.clipboardData.getData ("URL"));
- return event.preventDefault();
- }
- var files = event.clipboardData.files;
- if (files.length > 0) {
- process (this, files.item (0));
- return event.preventDefault();
- }
- }
- function create_button (callback) {
- var div = document.createElement ("div");
- 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");
- var input = document.createElement ("input");
- input.setAttribute ("style", "width : 24px; height: 24px; opacity:0; filter:alpha(opacity=0); float:right; cursor:pointer");
- input.setAttribute ("type", "file");
- input.setAttribute ("title", "Glissez vos images ici");
- input.setAttribute ("class", "hfr-upload-button");
- input.onchange = function() {
- callback (input);
- };
- div.appendChild (input);
- return div;
- }
- function focus (event) {
- if (this.parentNode.querySelector (".hfr-upload-button") != null)
- return;
- var current_area = this;
- var upload_button = create_button (function (input) {
- process (current_area, input.files.item (0));
- });
- if (this.classList.contains ("reponserapide")) {
- upload_button.style.marginLeft = "50%";
- var button = this.parentNode.querySelector ("input[type='submit']");
- this.parentNode.insertBefore (upload_button, this);
- }
- else if (this.getAttribute ("id").indexOf ("rep_editin_") == 0) {
- var button = this.parentNode.querySelector ("div > input[accesskey='s']");
- var div_center = button.parentNode;
- div_center.setAttribute ("align", "");
- div_center.parentNode.insertBefore (upload_button, div_center);
- }
- else {
- upload_button.style.width = "24px";
- var button = this.parentNode.querySelector ("input[type='submit']");
- button.parentNode.insertBefore (upload_button, button);
- }
- /*
- var buttons = this.parentNode.querySelectorAll ("input[type='button'] , input[type='submit']");
- var button = buttons.item (buttons.length - 1);
- console.log (this.getAttribute ("id").indexOf ("rep_editin_"));
- if (document.querySelector ("#submitreprap") != null) {
- button = document.querySelector ("#submitreprap");
- button.parentNode.appendChild(div, button.nextSibling);
- }
- else if (this.getAttribute ("id").indexOf ("rep_editin_") == 0) {
- button = this.parentElement.querySelector ("[value='Valider votre message']");
- console.log (button);
- button.parentNode.insertBefore (div, button);
- }
- else
- button.parentNode.appendChild(div, button.nextSibling);
- */
- }
- var content_form = document.querySelector("#content_form");
- if (content_form != null) {
- content_form.addEventListener("focus", focus);
- content_form.addEventListener('paste', pasting);
- content_form.addEventListener('drop', drop);
- }
- var observer=new MutationObserver(function(mutations, observer){
- var textareas=document.querySelectorAll("textarea[id^=\"rep_editin_\"]" );
- if(textareas.length){
- for(var textarea of textareas) {
- textarea.removeEventListener('focus', focus, false);
- textarea.addEventListener('focus', focus, false);
- textarea.removeEventListener('paste', pasting, false);
- textarea.addEventListener('paste', pasting, false);
- textarea.removeEventListener('drop', drop, false);
- textarea.addEventListener('drop', drop, false);
- }
- }
- });
- observer.observe(document, {attributes: false, childList: true, characterData: false, subtree: true});
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement