-
Notifications
You must be signed in to change notification settings - Fork 2
/
app.js
81 lines (66 loc) · 2.68 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
Promise.all([
InboxSDK.load('1.0', 'sdk_mlazzje-dlgmail_43a7d41655')
])
.then(function(results){
var service = analytics.getService('better_download_all_attachments_app');
var tracker = service.getTracker('UA-67984409-1');
var sdk = results[0];
var registerHandlers = function() {
sdk.Conversations.registerMessageViewHandler(messageViewHandler);
sdk.Conversations.registerThreadViewHandler(threadViewHandler);
var properties = {
title: chrome.i18n.getMessage('tooltip'),
iconUrl: chrome.runtime.getURL('img/save_toolbar.png'),
section: "OTHER",
onClick: handleToolbarButtonClick
}
sdk.Toolbars.registerToolbarButtonForThreadView(properties);
};
var threadViewHandler = function(threadRowView) {
// Send to Analytics that the threadView has been loaded
tracker.sendAppView('ThreadView');
}
var handleToolbarButtonClick = function(event) {
var messages = event.threadView.getMessageViewsAll();
var numberFiles = 0;
for(var i = 0; i < messages.length ; i++) {
var fileAttachmentsCardView = messages[i].getFileAttachmentCardViews();
fileAttachmentsCardView.forEach(handleAttachmentsDownload);
numberFiles += fileAttachmentsCardView.length;
}
// Send to Analytics that the Button has been clicked
tracker.sendEvent('Button', 'DownloadAllAttachments', 'Conversation', numberFiles);
}
var messageViewHandler = function(messageView) {
if(messageView.isLoaded()) {
// Add CustomAttachmentsToolbarButton to the given message view.
addCustomAttachmentsToolbarButton(messageView);
// Send to Analytics that the Message View has been loaded
tracker.sendAppView('MessageView');
}
};
var addCustomAttachmentsToolbarButton = function(messageView) {
var options = {
tooltip: chrome.i18n.getMessage('tooltip'),
iconUrl: chrome.runtime.getURL('img/save.png'),
onClick: handleAttachmentsButtonClick
};
messageView.addAttachmentsToolbarButton(options);
};
var handleAttachmentsButtonClick = function(event) {
var downloadUrls = [];
var fileAttachmentsCardView = event.attachmentCardViews;
// Send to Analytics that the Button has been clicked
tracker.sendEvent('Button', 'DownloadAllAttachments', 'Message', fileAttachmentsCardView.length);
// Iterate over attachmentCardViews array to get URL's.
fileAttachmentsCardView.forEach(handleAttachmentsDownload);
}
var handleAttachmentsDownload = function(attachmentCardView, index) {
if(typeof attachmentCardView !== 'undefined') {
// Download the attachment
attachmentCardView.getDownloadURL().then(downloadAttachment);
}
}
// Run.
registerHandlers();
});