Add imgur provider

This commit is contained in:
Magnus Hauge Bakke 2015-10-09 13:59:34 +02:00 committed by Lorenz Hübschle-Schneider
parent 0d7d8e0cce
commit f6bfa4161b
2 changed files with 76 additions and 0 deletions

View file

@ -39,6 +39,7 @@
<script type="text/javascript" src="js/websockets.js"></script>
<script type="text/javascript" src="js/models.js"></script>
<script type="text/javascript" src="js/plugins.js"></script>
<script type="text/javascript" src="js/imgur.js"></script>
<script type="text/javascript" src="3rdparty/favico-0.3.5.min.js"></script>
</head>
<body ng-controller="WeechatCtrl" ng-keydown="handleKeyPress($event)" ng-keyup="handleKeyRelease($event)" ng-keypress="handleKeyPress($event)" ng-class="{'no-overflow': connected}" ng-init="init()" lang="en-US">

75
js/imgur.js Normal file
View file

@ -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
};
}]);
})();