| |
| |
| |
| |
| |
| "use strict"; |
|
|
| |
| |
| |
| function upload(files, callback) { |
| |
| if (files.length > 1) { |
| album(files); |
| } |
| else { |
| verifyimage(files[0], callback); |
| } |
| } |
|
|
| |
| |
| |
| function verifyimage(file, callback) { |
| |
| if (!file || !file.type.match(/image.*/)) { |
| console.log("File not image"); |
| document.querySelector("#link").innerHTML = "The file you're trying to upload is not an image."; |
| return; |
| } |
|
|
| |
| if (file.size/1024/1024 >= 10) { |
| console.log("File too large to upload"); |
| document.querySelector("#link").innerHTML = "The image is too large to upload to imgur (10MB max). <a target='_blank' href='https://compressor.io/'>Compress</a> and retry."; |
| stoplinkprop("#link a"); |
| return; |
| } |
| uploadimage(file, callback); |
| } |
|
|
| |
| |
| |
| function uploadimage(file, callback) { |
| uploading(); |
| var fd = new FormData(); |
| fd.append("image", file); |
| var xhr = new XMLHttpRequest(); |
| xhr.open("POST", "https://api.imgur.com/3/image"); |
| xhr.setRequestHeader("Authorization", "Client-id f30578e81f80336"); |
| xhr.onload = function(){ |
| console.log(JSON.parse(xhr.response)); |
| |
| if (JSON.parse(xhr.response).data.error) { |
| document.querySelector("#link").innerHTML = JSON.parse(xhr.response).data.error; |
| return; |
| } |
| if (callback==null) { |
| uploaded(JSON.parse(xhr.response)); |
| } |
| else { |
| callback(JSON.parse(xhr.response)); |
| } |
| }; |
| xhr.send(fd); |
| } |
|
|
| var num_of_files; |
| |
| |
| |
| function album(files) { |
| num_of_files=files.length; |
| for (var i=0; i<files.length; i++) { |
| |
| (function(i){ |
| verifyimage(files[i], createalbum); |
| })(i); |
| } |
| } |
|
|
| var img_ids = []; |
| |
| |
| |
| function createalbum(response) { |
| |
| var id = response.data.id; |
| img_ids.push(id); |
|
|
| |
| if (img_ids.length == num_of_files) { |
|
|
| var formdata = new FormData; |
| for (var i = 0; i < img_ids.length; i++) { |
| formdata.append('ids[]', img_ids[i]); |
| } |
|
|
| var xhr = new XMLHttpRequest(); |
| xhr.open("POST", "https://api.imgur.com/3/album"); |
| xhr.setRequestHeader("Authorization", "Client-id f30578e81f80336"); |
| xhr.onload = function(){ |
| document.querySelector("#link").innerHTML = "<a target='_blank' href='http://imgur.com/a/"+JSON.parse(xhr.response).data.id+"'>http://imgur.com/a/"+JSON.parse(xhr.response).data.id+"</a> <i class='fa fa-paperclip'></i>"; |
| |
| img_ids = []; |
| num_of_files = null; |
| stoplinkprop("#link a"); |
| setcopybutton(); |
| }; |
| xhr.send(formdata); |
| } |
| } |
|
|
| |
| |
| |
| function uploading() { |
| document.querySelector("#link").innerHTML = "<i class='fa fa-spinner fa-spin' style='font-size: 2em;'></i>"; |
| } |
|
|
| |
| |
| |
| function uploaded(response) { |
| var original = response.data.link; |
| document.querySelector("#link").innerHTML = "<a target='_blank' href='"+original+"'>"+original+"</a> <i class='fa fa-paperclip'></i>"; |
| |
|
|
| stoplinkprop("#link a"); |
| setcopybutton(); |
|
|
| |
| document.querySelector(".desc img").src = original; |
| } |
|
|
| function setcopybutton() { |
| document.querySelector("#link i").addEventListener('click', function(event) { |
| copyTextToClipboard(document.querySelector("#link a").textContent); |
| event.stopPropagation(); |
| }, false); |
| } |
|
|
| function stoplinkprop(el) { |
| document.querySelector(el).addEventListener('click', function(event) { |
| event.stopPropagation(); |
| }, false); |
| } |
|
|
| |
| |
| |
| |
| function copyTextToClipboard(text) { |
| var textArea = document.createElement("textarea"); |
|
|
| |
| textArea.style.position = 'fixed'; |
| textArea.style.top = 0; |
| textArea.style.left = 0; |
|
|
| |
| |
| textArea.style.width = '2em'; |
| textArea.style.height = '2em'; |
|
|
| |
| textArea.style.padding = 0; |
|
|
| |
| textArea.style.border = 'none'; |
| textArea.style.outline = 'none'; |
| textArea.style.boxShadow = 'none'; |
|
|
| |
| textArea.style.background = 'transparent'; |
|
|
| textArea.value = text; |
|
|
| document.body.appendChild(textArea); |
|
|
| textArea.select(); |
|
|
| try { |
| var successful = document.execCommand('copy'); |
| var msg = successful ? 'successful' : 'unsuccessful'; |
| console.log('Copying text command was ' + msg); |
| } catch (err) { |
| console.log('Unable to copy'); |
| } |
|
|
| document.body.removeChild(textArea); |
|
|
| |
| document.querySelector("#link i").classList.remove("fa-paperclip"); |
| document.querySelector("#link i").classList.add("fa-check"); |
| } |
|
|
|
|
| function changeMessage(content) { |
| |
| var sheet = document.styleSheets[0]; |
| var rules = sheet.rules; |
| sheet.insertRule('.desc h2 a:before { content: "'+content+'"; }', rules.length); |
|
|
| document.querySelector(".desc h2 a").style.transform = "translateY(-100%)"; |
|
|
| } |
|
|
| |
| |
| |
|
|
| function error(desc) { |
|
|
| } |
|
|
|
|
|
|
| (function(document, window) { |
| |
| stoplinkprop("a"); |
| stoplinkprop("span a"); |
|
|
| |
| document.querySelector(".upload").addEventListener("click", function(){ |
| document.querySelector('input').click(); |
| }, false); |
|
|
| |
| document.querySelector("input").addEventListener("change", function(){ |
| upload(this.files, null); |
| }, false); |
|
|
| |
| var supportsDrag = function() { |
| var div = document.createElement('div'); |
| return (('draggable' in div) || ('ondragstart' in div && 'ondrop' in div)) && 'FormData' in window && 'FileReader' in window; |
| }(); |
|
|
| if (supportsDrag) { |
| ['drag', 'dragstart', 'dragend', 'dragover', 'dragenter', 'dragleave', 'drop'].forEach(function(event) { |
| document.querySelector(".upload").addEventListener(event, function(e) { |
|
|
| |
| e.preventDefault(); |
| e.stopPropagation(); |
| }); |
| }); |
| ['dragover', 'dragenter'].forEach(function(event) { |
| document.querySelector(".upload").addEventListener( event, function() { |
| this.classList.add("dragover"); |
| }); |
| }); |
| ['dragleave', 'dragend', 'drop'].forEach(function(event) { |
| document.querySelector(".upload").addEventListener( event, function() { |
| this.classList.remove("dragover"); |
| }); |
| }); |
| document.querySelector(".upload").addEventListener('drop', function(e) { |
| upload(e.dataTransfer.files, null); |
| }); |
| } |
|
|
| })(document, window); |
|
|