-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpopup.js
143 lines (125 loc) · 4.42 KB
/
popup.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
(function() {
var EMAIL_REGEX = /^(([^<>()\[\]\\.,;:\s@"]+(\.[^<>()\[\]\\.,;:\s@"]+)*)|(".+"))@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}])|(([a-zA-Z\-0-9]+\.)+[a-zA-Z]{2,}))$/;
function handleResult(result, status) {
if (!result) {
// Something is not right.
$('#submit').attr('disabled', 'disabled');
if (!status || status == -1) {
// this page is probably not an email
$('#status').text(chrome.i18n.getMessage('popupNoEmailDeteceted'));
} else {
// http error?
if (status === '0') {
$('#status').text(chrome.i18n.getMessage('popupDownloadFailed'));
} else if (status >= 500) {
$('#status').text(chrome.i18n.getMessage('popupEmailServerError'));
}
}
} else { // got a result
$('#status').hide();
$('#form').show();
$('#submit').removeAttr('disabled');
$('#data').text(result);
var config = SESConfig.getSelectedConfiguration();
$('#destination .name').text(config.name);
$('#destination .logo').attr('src', config.logo);
function updateTextField(e) {
if ($('#followupEnable').is(':checked')) {
$('#followupEmail').removeAttr('disabled').focus();
} else {
$('#followupEmail').attr('disabled', 'disabled');
}
}
$('#followupEnable').change(updateTextField);
updateTextField();
document.forms["form"].addEventListener('submit', function(event) {
event.preventDefault();
var followupRequested = $('#followupEnable').is(':checked');
var config = SESConfig.getSelectedConfiguration();
var serverUrl = config.serverUrl;
var authToken = config.authToken;
if (typeof serverUrl !== 'string' || serverUrl.length === 0) {
//TODO: handle invalid server url
} else {
if (serverUrl.match(EMAIL_REGEX)) {
// Email endpoint: construct and click a mailto link
let href = [
"mailto:",
serverUrl,
"?subject=Suspicious%20Email%20Submission&body=",
encodeURIComponent(result)
].join('');
chrome.tabs.update({ url: href });
} else {
$('#submit').attr('disabled', 'disabled');
var options = {};
if (followupRequested) {
var followupEmail = $('#followupEmail').val().trim();
if (followupEmail) {
options.annotations = ["Follow up with " + followupEmail];
}
}
window.mailToMisp(serverUrl, authToken, result, options).then(function(response) {
return response.json();
}).then(function(object) {
if (object.Event) {
var eventId = object.Event.id;
console.log("Created event", eventId);
$('#submit').hide();
$('#thanks').show();
} else {
$('#submit').hide();
$('#error').show();
console.log("Failed to create event");
console.log(object);
}
}).catch(function(error) {
console.log(error);
});
}
}
});
}
}
function executeContentScript() {
return new Promise(function(resolve, reject) {
// Listen for message from the content script
try {
chrome.tabs.executeScript(null, {
file: 'bundle.js'
}, function(results) {
var error = chrome.runtime.lastError;
if (!error && results.length && !results[0]) {
console.log(results[0]);
resolve(results[0]);
} else {
reject(error);
}
});
} catch(error) {
reject(error);
}
});
}
$('.openSettings').click(function(event) {
event.preventDefault();
window.open('/options.html');
});
try {
var config = SESConfig.getSelectedConfiguration();
} catch(error) {
$('#status').text(chrome.i18n.getMessage('popupNotConfigured'));
$('#notConfigured').show();
return; // bail!
}
chrome.runtime.onMessage.addListener(function(message, sender, sendResponse) {
if (typeof message === 'object' && sender.tab) {
console.log(message);
handleResult(message.result, message.status);
}
});
executeContentScript().catch(function(error) {
console.log(error);
handleResult(null, -1);
})
})();