-
Notifications
You must be signed in to change notification settings - Fork 2
/
util.js
75 lines (64 loc) · 1.7 KB
/
util.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
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
/**
* Run multiple files download
* @param {Array} urls Array of urls (pointing to files)
* @param {Integer} duration time between each HTTP call
*/
function processMultipleFilesDownload(urls, duration) { // Not used anymore
for(var i = 0; i < urls.length; i++) {
setTimeout(
(function(url) {
downloadAttachment(urls[i]);
})(urls[i]), duration);
}
}
/**
* Run attachment download
* @param {string} url Url to download the attachment
*/
function downloadAttachment(url) {
var stripped = stripUrl(url);
if(stripped) {
var a = document.createElement('a');
a.href = stripped;
var dispatchMouseEvent = function(type) {
var event = new MouseEvent(type, {
bubbles: true,
cancelable: true,
view : window,
detail: 0,
screenX: 0,
screenY: 0,
clientX: 0,
clientY: 0,
ctrlKey: true,
shiftKey: false,
altKey : false,
metaKey: false,
button: 0,
relatedTarget: null
});
dispatchEvent(a, event, type);
}
// Trigger
dispatchMouseEvent('click');
dispatchMouseEvent('mousedown');
dispatchMouseEvent('mouseenter');
}
}
/**
* Parse a string containing a url
* @param {string} Url Surrounded url
* @return {string} The url extracted from the given string
*/
function stripUrl(url) {
var re = /(http|ftp|https):\/\/[\w-]+(\.[\w-]+)+([\w.,@?^=%&:\/~+#-]*[\w@?^=%&\/~+#-])?/;
// Return the full url
return re.exec(url)[0];
}
function dispatchEvent (element, event, type) {
if (element.dispatchEvent) {
element.dispatchEvent(event);
} else if (element.fireEvent) {
element.fireEvent('on' + event.eventType, event);
}
}