-
Notifications
You must be signed in to change notification settings - Fork 0
/
background.js
47 lines (43 loc) · 1.74 KB
/
background.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
var requestsData = [];
var filterURL = ['https://edsstaging.bsstag.com/send_event', 'https://eds.browserstack.com/send_event'];
chrome.webRequest.onBeforeRequest.addListener(
function (details) {
// var filterURL = 'https://edsstaging.bsstag.com/send_event';
if (filterURL.includes(details.url)) {
console.log('Request intercepted:', details.method, details.url);
var postedString = decodeURIComponent(String.fromCharCode.apply(null, new Uint8Array(details.requestBody.raw[0].bytes)));
var decodedString = decodeURIComponent(postedString);
var jsonStartIndex = decodedString.indexOf('{');
var jsonEndIndex = decodedString.lastIndexOf('}');
var jsonSubstring = decodedString.substring(jsonStartIndex, jsonEndIndex + 1);
try {
var jsonObject = JSON.parse(jsonSubstring);
requestsData.push({
method: details.method,
url: details.url,
jsonObject: jsonObject,
eventName: jsonObject.data.event_name
});
console.log(jsonObject);
} catch (error) {
console.error('Error parsing JSON:', error);
}
}
},
{ urls: ['<all_urls>'] },
['blocking', 'requestBody']
);
chrome.runtime.onConnect.addListener(function (port) {
port.onMessage.addListener(function (msg) {
if (msg.action === 'getRequests') {
port.postMessage({ action: 'updateRequests', data: requestsData });
} else if (msg.action === 'clearRequests') {
requestsData = [];
port.postMessage({ action: 'updateRequests', data: requestsData });
} else if (msg.action === 'getRequestDetails') {
var index = msg.index;
var details = requestsData[index];
port.postMessage({ action: 'updateDetails', data: details });
}
});
});