-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathimgur.js
More file actions
122 lines (112 loc) · 4.38 KB
/
imgur.js
File metadata and controls
122 lines (112 loc) · 4.38 KB
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
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
(function () {
function uploader() {
console.log("Object created!");
this._started = false;
uploader.prototype.start();
}
uploader.prototype = {
/**
* Start to handle screenshot events.
* @memberof Screenshot.prototype
*/
start: function () {
console.log("IMGUR Running");
if (this._started) {
throw 'Instance should not be start()\'ed twice.';
}
this._started = true;
window.addEventListener('mozChromeEvent', this);
},
/**
* Stop handling screenshot events.
* @memberof Screenshot.prototype
*/
stop: function () {
if (!this._started) {
throw 'Instance was never start()\'ed but stop() is called.';
}
this._started = false;
window.removeEventListener('mozChromeEvent', this.handleEvent);
},
/**
* Handle screenshot events.
* @param {DOMEvent} evt DOM Event to handle.
* @memberof Screenshot.prototype
*/
handleEvent: function (evt) {
switch (evt.type) {
case 'mozChromeEvent':
if (evt.detail.type === 'take-screenshot-success') {
console.log("There is an screenshot available.");
if(navigator.onLine){
this.handleTakeScreenshotSuccess(evt.detail.file);
}
} else if (evt.detail.type === 'take-screenshot-error') {
this.notify('screenshotFailed', evt.detail.error);
}
break;
default:
console.debug('Unhandled event: ' + evt.type);
break;
}
},
/**
* Handle the take-screenshot-success mozChromeEvent.
* @param {Blob} file Blob object received from the event.
* @memberof Screenshot.prototype
*/
handleTakeScreenshotSuccess: function (file) {
try {
var self = this;
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 440d44a2a741339');
xhr.onload = function() {
var data = JSON.parse(xhr.responseText).data;
var imgurURL = data.link;
console.log(imgurURL);
self.notify('Screenshot uploaded: ', imgurURL, null, true);
//const gClipboardHelper = Components.classes["@mozilla.org/widget/clipboardhelper;1"].getService(Components.interfaces.nsIClipboardHelper);
//gClipboardHelper.copyString(imgurURL);
};
xhr.send(fd);
} catch (e) {
console.log('exception in screenshot handler', e);
this.notify('screenshotFailed', e.toString());
}
},
/**
* Display a screenshot success or failure notification.
* Localize the first argument, and localize the third if the second is null
* @param {String} titleid l10n ID of the string to show.
* @param {String} body Label to show as body, or null.
* @param {String} bodyid l10n ID of the label to show as body.
* @param {String} onClick Optional handler if the notification is clicked
* @memberof Screenshot.prototype
*/
//TODO: l10n
notify: function (titleid, body, bodyid, onClick) {
console.log("A notification would be send: " + titleid);
var notification = new window.Notification(titleid, {
body: body,
icon: '/style/icons/Gallery.png'
});
notification.onclick = function () {
notification.close();
if (onClick) {
new MozActivity({
name: "view",
data: {
type: "url",
url: body
}
});
}
};
}
};
console.log("Lets start!");
var uploader = new uploader();
}());