From f6bfa4161bbc37cf50a80697ad1eb41584b641d1 Mon Sep 17 00:00:00 2001 From: Magnus Hauge Bakke Date: Fri, 9 Oct 2015 13:59:34 +0200 Subject: [PATCH] Add imgur provider --- index.html | 1 + js/imgur.js | 75 +++++++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 76 insertions(+) create mode 100644 js/imgur.js diff --git a/index.html b/index.html index c42ecae..9a9ae1f 100644 --- a/index.html +++ b/index.html @@ -39,6 +39,7 @@ + diff --git a/js/imgur.js b/js/imgur.js new file mode 100644 index 0000000..be003c8 --- /dev/null +++ b/js/imgur.js @@ -0,0 +1,75 @@ +(function() { +'use strict'; + +var weechat = angular.module('weechat'); + +weechat.factory('imgur', ['$rootScope', function($rootScope) { + + var process = function(image) { + + // Is it an image? + if (!image || !image.type.match(/image.*/)) return; + + // New file reader + var reader = new FileReader(); + + // When image is read + reader.onload = function (event) { + var image = event.target.result.split(',')[1]; + upload(image); + }; + + // Read image as data url + reader.readAsDataURL(image); + + }; + + // Upload image to imgur from base64 + var upload = function( base64img ) { + + // Set client ID (Glowing Bear) + var clientId = "164efef8979cd4b"; + + // Create new form data + var fd = new FormData(); + fd.append("image", base64img); // Append the file + fd.append("type", "base64"); // Set image type to base64 + + // Create new XMLHttpRequest + var xhttp = new XMLHttpRequest(); + + // Post request to imgur api + xhttp.open("POST", "https://api.imgur.com/3/image", true); + + // Set headers + xhttp.setRequestHeader("Authorization", "Client-ID " + clientId); + xhttp.setRequestHeader("Accept", "application/json"); + + // Handler for response + xhttp.onreadystatechange = function() { + + // Check state and response status + if (xhttp.readyState == 4 && xhttp.status == 200) { + + // Get response text + var response = JSON.parse(xhttp.responseText); + + // Open image in new window + window.open(response.data.link, '_blank'); + + } + + }; + + // Send request with form data + xhttp.send(fd); + + }; + + return { + process: process + }; + +}]); + +})();