Benissou / JVC Imgur Uploader

// ==UserScript==
// @name         JVC Imgur Uploader
// @namespace    JVC Imgur Uploader
// @version      1.0
// @description  Uploader vos images sur Imgur et les voir en miniature dans les messages
// @author       Benissou
// @require      http://ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js
// @match        http://www.jeuxvideo.com/forums/*
// @connect      imgur.com
// @grant        GM_xmlhttpRequest
// @grant        GM_addStyle
// @noframes
// ==/UserScript==


var imgur = /http[s]*:\/\/(i.imgur.com|.*.tinypic.com|www.hapshack.com|puu.sh)\/.+.(jpg|gif|png)/; //http://i.imgur.com/xxxxxx.xxx
var liens_hypertext = document.getElementsByClassName("xXx");
var match;

for(i in liens_hypertext)
{
  if (imgur.test(liens_hypertext[i].innerHTML))
  {
	var s = liens_hypertext[i].getAttribute("href");
	var new_string = s.substring(0, s.lastIndexOf(".")) + "s" + s.substring(s.lastIndexOf("."))
    var miniature = document.createElement("img");
	
    miniature.className = "img-shack";
    miniature.setAttribute("src", new_string);
	miniature.setAttribute("href", liens_hypertext[i].getAttribute("href"));
    miniature.setAttribute("width", "auto");
    miniature.setAttribute("height", "51");
    liens_hypertext[i].innerText = "";
    liens_hypertext[i].appendChild(miniature);
  }
}



GM_addStyle('input.upload {   position: absolute;  width:100%; height:100%; top: 0;  overflow: hidden; right: 0;  margin: 0; padding: 10px;  font-size: 10px;  cursor: pointer;  opacity: 0;  filter: alpha(opacity=0);}'); 
$(".btn-group:eq(2)").append ('<div class="btn btn-jv-editor-toolbar " title="Imgur"><span class="jvcode-image" style="color:#ffcc99"></span><input type="file" title="Imgur" class="upload" id="file-imgur"></div>');


	
(function() {
    'use strict';

    $('#file-imgur').on('change', function() {

        var _file = this.files[0];

        GM_xmlhttpRequest({
            method: "POST",
            url: "https://imgur.com/upload/checkcaptcha",
            data: "total_uploads=1&create_album=true",
            headers: {
                "Content-Type": "application/x-www-form-urlencoded",
                'Referer': 'https://imgur.com/'
            },
            onload: function(resp) {
                var json = $.parseJSON(resp.responseText);

                var r = new FileReader();
                r.onload = function(e) {
                    var contents = e.target.result;
                    FileTransfer(json.data.new_album_id,contents, _file.type, _file.name);
                };

                r.readAsBinaryString(_file);

            }
        });


        function FileTransfer(id,file, type, UploadFile) {
            var UploadUrl = 'https://imgur.com/upload/';

            var Seperator = '--SeperatorWebkit02390293DJSDxS2'; 
            var _FormData = '--'+Seperator+'\r\n\
Content-Disposition: form-data; name="new_album_id"\r\n\r\n\
'+id+'\r\n\
--'+Seperator+'\r\n\
Content-Disposition: form-data; name="Filedata"; filename="'+UploadFile+'"\r\n\
Content-Type: '+type+'\r\n\
\r\n';

            _FormData += file +'\r\n';

            _FormData += '--'+Seperator+'--\r\n';

            // Daten übertragen
            GM_xmlhttpRequest({
                method: 'POST',
                url: UploadUrl,
                headers: {'Referer': 'https://imgur.com/', 'Content-Type': 'multipart/form-data; boundary='+Seperator },
                data: _FormData,
                binary: true,
                onload:function(response) {
                    if(response.status == 200){
                       var json = $.parseJSON(response.responseText);
                        console.log(json);

                       if(json.success){
                           var url = " https://i.imgur.com/"+json.data.hash + json.data.ext + " ";

                           $('#message_topic').val($('#message_topic').val() + url);
                       }
                    }
                }
            });

        }

    });

})();