-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscript.js
More file actions
157 lines (136 loc) · 4.92 KB
/
Copy pathscript.js
File metadata and controls
157 lines (136 loc) · 4.92 KB
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
150
151
152
153
154
155
156
157
let myLeads = [];
const inputEl = document.getElementById("input-el");
const saveBtn = document.getElementById("input-btn");
const downloadBtn = document.getElementById("download-btn");
const ulEl = document.getElementById("ul-el");
const delBtn = document.getElementById("delete-btn");
const tabBtn = document.getElementById("save-btn");
const formatBtn = document.getElementById("format-select");
// Load leads from local storage
const loadLeadsFromLocalStorage = () => {
const leadsFromLocalStorage = JSON.parse(localStorage.getItem("myLeads"));
if (leadsFromLocalStorage) {
myLeads = leadsFromLocalStorage;
renderLeads();
}
};
// Render leads
const renderLeads = () => {
const listItems = myLeads
.map((lead) => {
const shortenedLead = lead.startsWith("http")
? lead.replace(/(^\w+:|^)\/\//, "").split("/")[0]
: lead;
if (lead.startsWith("http")) {
return `<li><a target="_blank" href="${lead}">${shortenedLead}</a></li>`;
} else {
return `<li>${shortenedLead}</li>`;
}
})
.join("");
ulEl.innerHTML = listItems;
};
// Clear local storage and leads
const clearLocalStorage = () => {
localStorage.clear();
myLeads = [];
renderLeads();
};
// Save lead from input
const saveLead = () => {
const lead = inputEl.value.trim(); // Trim whitespace from the input
// Check if the lead is empty
if (lead === "") {
return; // Exit the function without saving
}
myLeads.push(lead);
inputEl.value = "";
localStorage.setItem("myLeads", JSON.stringify(myLeads));
renderLeads();
};
// Save lead from current tab
const saveTabLead = () => {
chrome.tabs.query({ active: true, currentWindow: true }, (tabs) => {
myLeads.push(tabs[0].url);
localStorage.setItem("myLeads", JSON.stringify(myLeads));
renderLeads();
});
};
const downloadLeads = (fileFormat) => {
if (fileFormat === "text") {
let documentContent = "";
for (let i = 0; i < myLeads.length; i++) {
const lead = myLeads[i];
const shortenedLead = lead.startsWith("http")
? lead.replace(/(^\w+:|^)\/\//, "").split("/")[0]
: lead;
if (lead.startsWith("http")) {
// For links, include both the link and its shortened form
documentContent += `Link: ${shortenedLead}\nURL: ${lead}\n\n`;
} else {
// For non-link content, include only the content
documentContent += `Content: ${lead}\n\n`;
}
}
const blob = new Blob([documentContent], { type: "text/plain" });
const url = URL.createObjectURL(blob);
const a = document.createElement("a");
a.href = url;
a.download = "leads.txt";
document.body.appendChild(a);
a.click();
document.body.removeChild(a);
} else if (fileFormat === "html") {
let tableContent =
"<table style='border-collapse: collapse; margin: 20px auto;'>\n"; // Added margin and centering style
for (let i = 0; i < myLeads.length; i++) {
const lead = myLeads[i];
const shortenedLead = lead.startsWith("http")
? lead.replace(/(^\w+:|^)\/\//, "").split("/")[0]
: lead;
if (lead.startsWith("http")) {
// For links, include both the link and its shortened form in a table row
tableContent += `<tr>
<td style='padding: 5px; border: 1px solid black; font-weight: bold; color: #333;'>Link:</td> <!-- Added font-weight and color -->
<td style='padding: 5px; border: 1px solid black; color: #666;'>${shortenedLead}</td> <!-- Added color -->
</tr>\n`;
tableContent += `<tr>
<td style='padding: 5px; border: 1px solid black; font-weight: bold; color: #333;'>URL:</td> <!-- Added font-weight and color -->
<td style='padding: 5px; border: 1px solid black;'><a href='${lead}' style='color: #009688;'>${lead}</a></td> <!-- Added link color -->
</tr>\n\n`;
} else {
// For non-link content, include only the content in a table row
tableContent += `<tr>
<td style='padding: 5px; border: 1px solid black; font-weight: bold; color: #333;'>Content:</td> <!-- Added font-weight and color -->
<td style='padding: 5px; border: 1px solid black; color: #666;'>${lead}</td> <!-- Added color -->
</tr>\n\n`;
}
}
tableContent += "</table>";
const blob = new Blob([tableContent], { type: "text/html" });
const url = URL.createObjectURL(blob);
const a = document.createElement("a");
a.href = url;
a.download = "leads.html";
document.body.appendChild(a);
a.click();
document.body.removeChild(a);
}
};
// Event listeners
delBtn.addEventListener("dblclick", () => {
console.log("double clicked!");
clearLocalStorage();
});
saveBtn.addEventListener("click", () => {
saveLead();
});
tabBtn.addEventListener("click", () => {
saveTabLead();
});
downloadBtn.addEventListener("click", () => {
const fileFormat = formatBtn.value;
downloadLeads(fileFormat);
});
// Load leads from local storage on page load
loadLeadsFromLocalStorage();