|
| 1 | +function rehostImage(info, tab) { |
| 2 | + var xhr = new XMLHttpRequest(); |
| 3 | + xhr.open('POST', 'https://mediacru.sh/api/upload/url'); |
| 4 | + xhr.onload = function() { |
| 5 | + var result = JSON.parse(this.responseText); |
| 6 | + if (this.status == 409) { |
| 7 | + window.open('https://mediacru.sh/' + result.hash, '_blank'); |
| 8 | + } else if (this.status == 200) { |
| 9 | + var notification = webkitNotifications.createNotification('icon48.png', 'MediaCrush', 'Processing, please wait...'); |
| 10 | + notification.show(); |
| 11 | + setTimeout(function() { |
| 12 | + checkStatus(result.hash, notification); |
| 13 | + }, 1000); |
| 14 | + } else { |
| 15 | + alert('An error occured re-hosting this image.'); |
| 16 | + } |
| 17 | + }; |
| 18 | + var formData = new FormData(); |
| 19 | + formData.append('url', info.srcUrl); |
| 20 | + xhr.send(formData); |
| 21 | +} |
| 22 | + |
| 23 | +function checkStatus(hash, notification) { |
| 24 | + var xhr = new XMLHttpRequest(); |
| 25 | + xhr.open('GET', 'https://mediacru.sh/api/' + hash + '/status'); |
| 26 | + xhr.onload = function() { |
| 27 | + var result = JSON.parse(this.responseText); |
| 28 | + if (result.status == "error") { |
| 29 | + notification.cancel(); |
| 30 | + notification = webkitNotifications.createNotification('icon48.png', 'MediaCrush', 'There was an error while processing this file.'); |
| 31 | + } else if (result.status == "timeout") { |
| 32 | + notification.cancel(); |
| 33 | + notification = webkitNotifications.createNotification('icon48.png', 'MediaCrush', 'This file took too long to process.'); |
| 34 | + } else if (result.status == "done") { |
| 35 | + notification.cancel(); |
| 36 | + window.open('https://mediacru.sh/' + result.hash + '#fromExtension', '_blank'); |
| 37 | + } else { |
| 38 | + setTimeout(function() { checkStatus(hash, notification); }, 1000); |
| 39 | + } |
| 40 | + }; |
| 41 | + xhr.send(); |
| 42 | +} |
| 43 | + |
| 44 | +chrome.contextMenus.create({ title: 'Rehost on MediaCrush', contexts: [ 'image' ], onclick: rehostImage }); |
0 commit comments