-
Notifications
You must be signed in to change notification settings - Fork 2
/
extension.js
57 lines (54 loc) · 2.3 KB
/
extension.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
function getNotificationOptions(message) {
return {
type: "basic",
message: message,
title: "MediaCrush",
iconUrl: "icon48.png"
};
}
function rehostImage(info, tab) {
var xhr = new XMLHttpRequest();
xhr.open('POST', 'https://mediacru.sh/api/upload/url');
var notificationId = -1;
chrome.notifications.create("", getNotificationOptions("Processing, please wait..."), function(id) {
notificationId = id;
});
xhr.onload = function() {
var result = JSON.parse(this.responseText);
if (this.status == 409) {
chrome.notifications.clear(notificationId, function (ok) {});
window.open('https://mediacru.sh/' + result.hash, '_blank');
} else if (this.status == 200) {
setTimeout(function() {
checkStatus(result.hash, notificationId);
}, 1000);
} else {
chrome.notifications.clear(notificationId, function (ok) {});
alert('An error occured re-hosting this image.');
}
};
var formData = new FormData();
formData.append('url', info.srcUrl);
xhr.send(formData);
}
function checkStatus(hash, notificationId) {
var xhr = new XMLHttpRequest();
xhr.open('GET', 'https://mediacru.sh/api/' + hash + '/status');
xhr.onload = function() {
var result = JSON.parse(this.responseText);
if (result.status == "error") {
chrome.notifications.clear(notificationId, function (ok) {});
chrome.notifications.create("", getNotificationOptions('There was an error while processing this file.'), function(id) {});
} else if (result.status == "timeout") {
chrome.notifications.clear(notificationId, function (ok) {});
chrome.notifications.create("", getNotificationOptions('This file took too long to process.'), function(id) {});
} else if (result.status == "done") {
chrome.notifications.clear(notificationId, function (ok) {});
window.open('https://mediacru.sh/' + result.hash + '#fromExtension', '_blank');
} else {
setTimeout(function() { checkStatus(hash, notificationId); }, 1000);
}
};
xhr.send();
}
chrome.contextMenus.create({ title: 'Rehost on MediaCrush', contexts: [ 'image' ], onclick: rehostImage });