JSFiddle - React, Tailwind, and code Playground
by Shobhit Sharma
JavaScript
function imageTransloader(info) {
console.log(info);
//setup clipboard shiv
var background = chrome.extension.getBackgroundPage();
var ta = background.document.createElement("textarea");
ta.id = "clipboard";
background.document.body.appendChild(ta);
//get filename
var filename = info.srcUrl.substring(info.srcUrl.lastIndexOf('/') + 1);
//facebook id fix
var pattern = /fbcdn\-sphotos/;
if (pattern.test(info.srcUrl)) {
filename = "fb.jpg";
}
//make sure it's not empty
if (filename === "") {
filename = "something.jpg";
}
// fetch the image
var fileGet = new XMLHttpRequest();
fileGet.open("GET", info.srcUrl, true);
fileGet.responseType = "arraybuffer";
fileGet.onreadystatechange = function (oEvent) {
if (fileGet.readyState === 4) {
if (fileGet.status === 200) {
//get neccessary metadata
var filesize = fileGet.getResponseHeader("Content-Length");
var mimetype = fileGet.getResponseHeader("Content-Type");
//build blob
var dataview = new DataView(fileGet.response);
var blob = new Blob([dataview]);
//check if gif && > 2MB
if (filesize > (1024 * 1024 * 2) && mimetype === "image/gif") {
//notify user
var notification = webkitNotifications.createNotification(
'Style/images/lueshi_48.png',
'Image transloading failed',
'This gif is too big (>2MB)');
notification.show();
setTimeout(function () {
notification.cancel();
}, 6000);
} else {
//construct FormData object
var formData = new FormData();
formData.append("file", blob, filename);
//open...