-
Notifications
You must be signed in to change notification settings - Fork 0
/
popup.js
124 lines (105 loc) · 4.27 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
'use strict';
let listeners = [];
chrome.runtime.sendMessage({
data: {
request: internalRequestKinds.sendLogs
}
});
chrome.runtime.onMessage.addListener(
function (request, sender, sendResponse) {
if (request.msg === tsSapCallFinishedMsg) {
const messagesElement = document.getElementById('messages');
messagesElement.innerHTML = formatServerResponse(request.data.serverResponse) + messagesElement.innerHTML;
const date = request.data.serverResponse.date;
retainOnlyNewestOfSubsequentLogEntries(messagesElement);
initializeLinkListeners(date);
makeObjectInspectionCollapsible(date);
listeners.forEach(listenerEntry => {
document.getElementById(listenerEntry.elementId).addEventListener('click', listenerEntry.listener);
});
}
}
);
function formatServerResponse(serverResponse) {
let textParts = serverResponse.text.split('ManualSapTestInfo');
let text = textParts[0];
if (textParts.length > 1) {
text += '<div id="' + serverResponse.date + '-teaser">ManualSapTestInfo [<span style="color: darkgray">click' +
' to inspect</span>]</div>'+
'<div id="' + serverResponse.date + '-info" style="display: none">' +
'ManualSapTestInfo' + textParts[1].replace('[', '[<br> ').replace(/, /g, '<br> ') +
'</div>';
}
text = text.replace(/(?:\r\n|\r|\n)/g, '<br>');
const resetUserStateUrl = 'api/projects/{project}/sap-test-event/reset/{sap-user}';
text = text.replace(resetUserStateUrl, resetUserStateUrl + ' (<span class="reset-link" id="' + serverResponse.date + '-reset">click' +
' here to send request now)</span>');
const testKeyPrefix = 'Test identifier: ';
const testKeyPostfix = ' get log via api/projects/{project}/sap-test-event/log/{sap-test-key}.';
text = text.replace(testKeyPrefix, '<span class="log-link" id="' + serverResponse.date + '-log" data-test-key="');
text = text.replace(testKeyPostfix, '">Click to retrieve/update log.</span>');
text.replace(':/CQSE/MSG_TIA:000 ', '');
let labelsClasses = 'labels';
if (serverResponse.status !== httpOkStatus) {
labelsClasses += ' error';
}
return "<div class='" + labelsClasses + "'><div class='label'>" + serverResponse.action + "</div><div class='label'" +
" style='float:" +
" right'>" + serverResponse.date + "</div></div><p class='message'>" + text + "</p>";
}
function initializeLinkListeners(date) {
const logLink = document.getElementById(date + '-log');
if (logLink) {
listeners.push(constructSimpleRequestSendingListener(logLink, {
request: internalRequestKinds.getLog,
sapTestKey: logLink.getAttribute('data-test-key')
}));
}
const resetLink = document.getElementById(date + '-reset');
if (resetLink) {
listeners.push(constructSimpleRequestSendingListener(resetLink, {request: internalRequestKinds.resetUser}));
}
}
function constructSimpleRequestSendingListener(link, requestData) {
return {
elementId: link.id, listener: () => {
chrome.runtime.sendMessage({
data: requestData
});
}
};
}
function makeObjectInspectionCollapsible(date) {
const infoElement = document.getElementById(date + '-info');
if (infoElement) {
listeners.push({elementId: infoElement.id, listener: () => triggerMoreInfo(date)});
}
const teaserElement = document.getElementById(date + '-teaser');
if (teaserElement) {
listeners.push({elementId: teaserElement.id, listener: () => triggerMoreInfo(date)});
}
}
function triggerMoreInfo(elementIdPrefix) {
const infoElement = document.getElementById(elementIdPrefix + '-info');
const teaserElement = document.getElementById(elementIdPrefix + '-teaser');
if (infoElement.style.display === 'none') {
infoElement.style.display = 'block';
teaserElement.style.display = 'none';
} else {
infoElement.style.display = 'none';
teaserElement.style.display = 'block';
}
}
function retainOnlyNewestOfSubsequentLogEntries(messagesElement) {
const messagesChildren = messagesElement.children;
// each message renders to two elements (<div> + <p>); only remove superfluous logs starting from 2 messages
if (messagesChildren.length >= 4) {
if (isReportLogEntry(messagesChildren[0]) && isReportLogEntry(messagesChildren[2])) {
messagesElement.removeChild(messagesChildren[3]);
messagesElement.removeChild(messagesChildren[2]);
}
}
}
function isReportLogEntry(element) {
return element.innerHTML.includes('Log</');
}