-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathapp.js
145 lines (112 loc) · 3.55 KB
/
app.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
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
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
/*
** GA for anonymously tracking eDimension downloads
** (to show school usefulness of extension)
*/
_gaq.push(['_setAccount', 'UA-84709766-1']);
_gaq.push(['_setAllowLinker', true]);
_gaq.push(['_trackPageview']);
/*
** CONSTANTS
*/
const EDIM_URL = 'https://edimension.sutd.edu.sg';
const FOLDER_DL_ICON_SRC = chrome.extension.getURL('/images/download.png');
const INDIV_DL_ICON_SRC = chrome.extension.getURL('/images/download.png');
const SUTD_SQ_LOGO_SRC = chrome.extension.getURL('/images/sutd.png');
const TYPE_DOWNLOADABILITY = {
document: true,
file: true,
folder: false,
link: false
};
/*
** HELPER FUNCTIONS
*/
const getDownloadable = function(els) {
return $(els).filter(function() {
return TYPE_DOWNLOADABILITY[getType(this)];
});
}
const getFolders = function(els) {
return $(els).filter(function() {
return getType(this) === 'folder';
});
}
// types: document, file, folder, link
const getType = (el) => {
el = $(el);
const imgSrc = el.closest('li[id *=contentListItem]').children('img').attr('src');
if (!imgSrc) return null; // (in)sanity check
// extract type from imgSrc
return imgSrc.replace(/\/.+\//, '').replace(/_on.+/, '');
}
const getLinkInfo = (el) => {
el = $(el);
const url = EDIM_URL + el.attr('href');
const title = el.children('span').text() || el.text();
return { url, title };
}
/*
** START (FURREAL)
*/
$('head link[type="image/x-icon"]').attr('href', SUTD_SQ_LOGO_SRC);
console.log
const getAllLinks = (el = $('body')) => {
return $(el).find('#content_listContainer li a');
}
const allLinks = getAllLinks();
const downloadableLinks = getDownloadable(allLinks);
const folderLinks = getFolders(allLinks);
// Download All Btn
if ( downloadableLinks.length ) {
const dlAllBtn = $(`<a id="yp-dl-all"><img src=${FOLDER_DL_ICON_SRC}></img></a>`);
$('#pageTitleBar').append(dlAllBtn);
dlAllBtn.click( (e) => {
e.preventDefault();
_gaq.push(['_trackEvent', 'Download', 'Request Page Download'])
// handle links on current page
downloadableLinks.each(function() {
const {url, title} = getLinkInfo(this);
_gaq.push(['_trackEvent', 'Download', 'from Page', title])
chrome.runtime.sendMessage(url);
});
// handle folders on current page
folderLinks.each(function() {
const { url } = getLinkInfo(this);
downloadFolder(url);
});
})
}
// add download icons for files/documents
downloadableLinks.each(function() {
const {url, title} = getLinkInfo(this);
const dlBtn = $(`<a href=${url} class='yp-dl' download><img src=${INDIV_DL_ICON_SRC}></img></a>`);
dlBtn.click(() => { _gaq.push(['_trackEvent', 'Download', 'from File', title]) });
$(this).prepend(dlBtn);
})
const downloadFolder = (url) => {
$.get(url).then( (data) => {
_gaq.push(['_trackEvent', 'Download', 'Request Folder Download'])
const allLinks = getAllLinks(data);
const dlLinks = getDownloadable(allLinks);
dlLinks.each(function() {
const {url, title} = getLinkInfo(this);
_gaq.push(['_trackEvent', 'Download', 'from Folder', title])
chrome.runtime.sendMessage(url);
});
const folderLinks = getFolders(allLinks);
folderLinks.each(function() {
const { url } = getLinkInfo(this);
downloadFolder(url);
});
});
}
// add download icons for folders
folderLinks.each(function() {
const { url } = getLinkInfo(this);
const dlBtn = $(`<a href=${url} class='yp-dl' download><img src=${FOLDER_DL_ICON_SRC}></img></a>`);
dlBtn.click((e) => {
e.preventDefault();
downloadFolder(url);
});
$(this).prepend(dlBtn);
})