-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.js
139 lines (122 loc) · 5.71 KB
/
main.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
// populate the popup with the existing stashes
const stashList = document.querySelector("#stashList");
const noStashes = document.querySelector("#noStashes");
displayStashes();
// Inject our content scripts
chrome.tabs.executeScript({ file: 'contentScript.js' });
document.querySelector("#stashInfo").addEventListener("submit", e => {
e.preventDefault();
createNewStash();
});
function createNewStash(e) {
chrome.tabs.query({ active: true, currentWindow: true }, tabs => // get the current tab
chrome.tabs.sendMessage(tabs[0].id, { action: "retrieveFormData" }, response => { // Tell the content script to collect all the inputs
// For stashes un-named by user
let keyShortName;
let keyName = tabs[0].url + "|";
let stashName = document.querySelector("#stashName").value;
if (!stashName) {
// go through the existing stashes and figure out how many we already have for this page
chrome.storage.sync.get(null, stashes => {
let stashIndex = 1 + Object.keys(stashes).filter(key => key.includes(stripURL(tabs[0].url))).length;
const now = new Date();
keyName += `Stash ${stashIndex} ${now.toLocaleDateString().substring(0, now.toLocaleDateString().length - 4)}${now.getFullYear().toString().substr(-2)} ${now.getHours().toString().padStart(2, '0')}:${now.getMinutes().toString().padStart(2, '0')}`;
chrome.storage.sync.set({ [keyName]: response });
displayStashes();
});
} else {
keyName += stashName;
chrome.storage.sync.set({ [keyName]: response });
displayStashes();
}
// set the stash to be live update
chrome.tabs.sendMessage(tabs[0].id, { action: "setCheckedStash", newStash: keyName });
chrome.tabs.sendMessage(tabs[0].id, { action: "enableLiveUpdate" });
document.querySelector("#stashName").value = "";
}));
}
function displayStashes() {
stashList.innerHTML = ""; // clear the existing ones
noStashes.hidden = false;
chrome.tabs.query({ active: true, currentWindow: true }, tabs => {
// get all the stashes and filter them if they're from this page.
chrome.storage.sync.get(null, stashes => Object.keys(stashes).filter(
stash => stash.split("|")[0].includes(stripURL(tabs[0].url)) || tabs[0].url.includes(stripURL(stash.split("|")[0]))).forEach(stash => {
noStashes.hidden = true;
let li = document.createElement("li");
let a = document.createElement("a");
a.id = stash;
a.text = stash.split("|")[1];
a.href = "";
a.className = "stashItemName";
a.addEventListener("click", e => {
e.preventDefault();
// populate the web page with the stash data
chrome.tabs.sendMessage(tabs[0].id, { action: "fillFormData", elements: stashes[stash] });
// set the stash to be live update
chrome.tabs.sendMessage(tabs[0].id, { action: "setCheckedStash", newStash: stash });
chrome.tabs.sendMessage(tabs[0].id, { action: "enableLiveUpdate" });
document.querySelectorAll("label").forEach(label => label.classList.remove("accented")); //un check the other labels
lblCheck.classList.add("accented");
})
li.appendChild(a);
const controls = document.createElement("div");
controls.className = "stashControls";
const lblCheck = document.createElement("label");
lblCheck.htmlFor = "chk" + stash;
lblCheck.innerHTML = '<i class="fas fa-sync"></i>';
lblCheck.title = "Auto Update Stash"
controls.appendChild(lblCheck);
const chkEnableLive = document.createElement("input");
chkEnableLive.type = "checkbox";
chkEnableLive.class = "chkEnLive";
chkEnableLive.id = "chk" + stash;
// check if the current stash is the selected one (this runs when the popup is opened only)
chrome.tabs.query({ active: true, currentWindow: true }, tabs =>
chrome.tabs.sendMessage(tabs[0].id, { action: "getCheckedStash" }, response => {
if (response == stash) {
chkEnableLive.checked = true;
lblCheck.className = "accented";
}
}));
chkEnableLive.addEventListener("change", e =>
chrome.tabs.query({ active: true, currentWindow: true }, tabs => {
if (chkEnableLive.checked) { // if we check the box, set the content script's stash to this one, and add the event listeners.
chrome.tabs.sendMessage(tabs[0].id, { action: "setCheckedStash", newStash: stash });
chrome.tabs.sendMessage(tabs[0].id, { action: "enableLiveUpdate" });
document.querySelectorAll("label").forEach(label => label.classList.remove("accented")); //un check the other labels
lblCheck.classList.add("accented");
} else { // if we uncheck, stop updating.
chrome.tabs.sendMessage(tabs[0].id, { action: "disableLiveUpdate" });
lblCheck.classList.remove("accented");
}
}));
controls.appendChild(chkEnableLive);
const btnUpdate = document.createElement("button");
btnUpdate.innerHTML = '<i class="fas fa-edit"></i>';
btnUpdate.className = "btnUpdateStash";
btnUpdate.addEventListener("click", () => updateStashData(stash));
btnUpdate.title = "Update This Stash";
controls.appendChild(btnUpdate);
const btnDelete = document.createElement("button");
btnDelete.innerHTML = '<i class="fas fa-trash"></i>';;
btnDelete.className = "btnStashDelete";
btnDelete.title = "Delete This Stash";
btnDelete.addEventListener("click", e => {
chrome.storage.sync.remove(stash, result => displayStashes());
});
controls.appendChild(btnDelete);
li.appendChild(controls);
stashList.appendChild(li);
}));
});
}
function updateStashData(stash) {
chrome.tabs.query({ active: true, currentWindow: true }, tabs =>
chrome.tabs.sendMessage(tabs[0].id, { action: "retrieveFormData" }, response =>
chrome.storage.sync.set({ [stash]: response })
));
}
function stripURL(url) {
return url.split("://")[1].split("?")[0];
}