-
Notifications
You must be signed in to change notification settings - Fork 0
/
logwindow.js
149 lines (125 loc) · 4.86 KB
/
logwindow.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
146
147
148
149
document.addEventListener("DOMContentLoaded", () => {
const logDiv1 = document.getElementById("log");
const logDiv2 = document.getElementById("log2");
let activeTabId1 = null;
let activeTabId2 = null;
const tabDropdown1 = document.getElementById("tabDropdown1");
const tabDropdown2 = document.getElementById("tabDropdown2");
// notify the background script that the popup is open
chrome.runtime.sendMessage({ action: "popupOpened" });
// populate dropdowns with available tabs
const populateTabDropdowns = () => {
chrome.tabs.query({}, (tabs) => {
console.log("Tabs fetched:", tabs);
tabs.forEach((tab) => {
const option1 = document.createElement("option");
const option2 = document.createElement("option");
option1.value = tab.id;
option1.textContent = `Tab ${tab.id}: ${tab.title}`;
// check tab url of option1
if (tab.url.startsWith("https://wayback.archive-it.org/")) {
option1.style.backgroundColor = "lightgreen";
}
tabDropdown1.appendChild(option1);
option2.value = tab.id;
option2.textContent = `Tab ${tab.id}: ${tab.title}`;
// check tab url of option2
if (tab.url.startsWith("https://wayback.archive-it.org/")) {
option2.style.backgroundColor = "lightgreen";
}
tabDropdown2.appendChild(option2);
});
});
};
// create and append log entries to the specified log div
const appendLogEntry = (log, logDiv) => {
const logEntry = document.createElement("div");
logEntry.textContent = log.message;
// red for "error" messages
if (log.message.toLowerCase().includes("error")) {
logEntry.style.color = "red";
}
// orange for "warning" messages
else if (log.message.toLowerCase().includes("warning")) {
logEntry.style.color = "orange";
}
logDiv.appendChild(logEntry);
// line break after each log entry
const lineBreak = document.createElement("br");
logDiv.appendChild(lineBreak);
};
// request logs of the selected tab from background.js
const requestLogs = (tabId, logDiv) => {
console.log(`Requesting logs for tab ${tabId}`);
chrome.runtime.sendMessage(
{ action: "getLogs", tabId },
(response = { messages: [] }) => {
console.log("Logs received:", response);
logDiv.innerHTML = "";
// append logs if there are any messages
if (Array.isArray(response.messages) && response.messages.length > 0) {
response.messages.forEach((log) => {
appendLogEntry(log, logDiv);
});
}
}
);
};
// handle the "Load Console Logs" button click for tab 1
document.getElementById("openTabButton1").addEventListener("click", () => {
activeTabId1 = parseInt(tabDropdown1.value, 10);
console.log(`Selected tab for logDiv1: ${activeTabId1}`);
// sends message to background script to attach debugger
chrome.runtime.sendMessage(
{ action: "attachDebugger", tabId: activeTabId1 },
(response) => {
if (response.success) {
console.log(`Debugger attached to tab ${activeTabId1}`);
// refresh selected tab
chrome.tabs.reload(activeTabId1, () => {
console.log(`Tab ${activeTabId1} refreshed`);
requestLogs(activeTabId1, logDiv1);
});
} else {
console.warn(`Failed to attach debugger: ${response.error}`);
}
}
);
});
// handle the "Load Console Logs" button click for tab 2
document.getElementById("openTabButton2").addEventListener("click", () => {
activeTabId2 = parseInt(tabDropdown2.value, 10);
console.log(`Selected tab for logDiv2: ${activeTabId2}`);
// sends message to background script to attach debugger
chrome.runtime.sendMessage(
{ action: "attachDebugger", tabId: activeTabId2 },
(response) => {
if (response.success) {
console.log(`Debugger attached to tab ${activeTabId2}`);
// refresh selected tab
chrome.tabs.reload(activeTabId2, () => {
console.log(`Tab ${activeTabId2} refreshed`);
requestLogs(activeTabId2, logDiv2);
});
} else {
console.warn(`Failed to attach debugger: ${response.error}`);
}
}
);
});
// listens for new log messages from the background script
chrome.runtime.onMessage.addListener((message) => {
console.log("Message received:", message);
if (message.tabId === activeTabId1 && message.log) {
appendLogEntry(message.log, logDiv1);
} else if (message.tabId === activeTabId2 && message.log) {
appendLogEntry(message.log, logDiv2);
}
});
// notifies background script when the popup is closed
window.addEventListener("unload", () => {
chrome.runtime.sendMessage({ action: "popupClosed" });
});
// populate dropdowns upon initialization
populateTabDropdowns();
});