-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathpopup.js
61 lines (55 loc) · 1.53 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
document.getElementById("extractBtn").addEventListener("click", async () => {
try {
const [tab] = await chrome.tabs.query({
active: true,
currentWindow: true,
});
// Execute the content script if it's not already injected
await chrome.scripting
.executeScript({
target: { tabId: tab.id },
files: ["content.js"],
})
.catch(() => {
// Script might already be injected, continue
});
// Send message to content script
chrome.tabs.sendMessage(tab.id, { action: "extract" }, async (response) => {
if (chrome.runtime.lastError) {
alert("Please refresh the page and try again");
return;
}
if (response && response.data) {
displayData(response.data);
await copyData(response.data);
}
});
} catch (error) {
console.error("Error:", error);
alert("An error occurred. Please try again.");
}
});
async function copyData(data) {
const text = data
.map(({ title, address }) => `${address} ${title}`)
.join("\n");
try {
await navigator.clipboard.writeText(text);
} catch (err) {
console.error("Failed to copy:", err);
alert("Failed to copy to clipboard");
}
}
function displayData(data) {
const dataDisplay = document.getElementById("dataDisplay");
dataDisplay.innerHTML = data
.map(
({ title, address }) => `
<div class="item">
<div class="address">${address}</div>
<div class="title">${title}</div>
</div>
`
)
.join("");
}