-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcontentBack.js
213 lines (175 loc) · 6.29 KB
/
contentBack.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
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
if(typeof init == 'undefined'){
const toPromise = async (callback) => {
const promise = new Promise((resolve, reject) => {
try {
callback(resolve, reject);
}
catch (err) {
reject(err);
}
});
return promise;
}
const getData = async (url) => {
return toPromise((resolve, reject) => {
chrome.storage.local.get([url], (result) => {
//console.log(result)
if (chrome.runtime.lastError)
reject(chrome.runtime.lastError);
//console.log(result);
//console.log(result[url]);
//const researches = result.pages ?? [];
//console.log(researches);
//resolve(researches);
resolve(result[url] ? result[url] : []);
});
});
};
const saveNewData = async (url, name, dataText) => {
//const pages = await getData(url);
//const updatedPages = [...pages, { url, name }];
const newData = {
'name' : name,
'dataText' : dataText
};
return toPromise((resolve, reject) => {
//chrome.storage.local.set({ [url]: updatedPages }, () => {
chrome.storage.local.set({ [url]: newData}, () => {
if (chrome.runtime.lastError)
reject(chrome.runtime.lastError);
resolve(newData);
});
});
};
// const clearData = () => {
// return toPromise((resolve, reject) => {
// chrome.storage.local.remove([PAGES_KEY], () => {
// if (chrome.runtime.lastError)
// reject(chrome.runtime.lastError);
// resolve();
// });
// });
// };
const removeDatapoint = async (url) =>{
return toPromise((resolve, reject) => {
chrome.storage.local.remove([url], (result) => {
//console.log(result);
if (chrome.runtime.lastError)
reject(chrome.runtime.lastError);
resolve();
});
});
};
const allDatapoint = async () =>{
return toPromise((resolve, reject) => {
chrome.storage.local.get(null, (items)=>{
var allKeys = Object.keys(items);
//console.log(items);
// for (const key in items) {
// if (items.hasOwnProperty(key)) {
// //console.log(`${key}: ${items[key]}`);
// //works
// console.log(items[key].name);
// }
// }
if (chrome.runtime.lastError)
reject(chrome.runtime.lastError);
resolve(items);
});
});
};
// const fetchData = async (url) =>{
// return toPromise((resolve, reject) => {
// chrome.storage.local.get([url], (result) => {
// //console.log(result)
// if (chrome.runtime.lastError)
// reject(chrome.runtime.lastError);
// // for (const [key, val] of Object.entries(result)) {
// // // do something
// // console.log(key);
// // console.log(val);
// // for (let dataPoint in val){
// // console.log(val.0);
// // }
// // }
// const researches = result.pages ?? [];
// //console.log(researches);
// //var result = result.url.filter(function(e){return e.code == url})
// //console.log(result);
// resolve(result);
// });
// });
// }
(async function(){
const Lname = document.querySelector("h1");
//console.log(Lname);
if (Lname) {
const text = Lname.textContent;
//console.log(text);
const newName = document.createElement("p");
newName.classList.add("pratyush-custom-newname");
newName.textContent = `${text}`;
//create button for action
button = document.createElement("button");
button.textContent = "Save";
button.classList.add("save-profile-button");
textbox = document.createElement('input');
textbox.type = 'text/css';
textbox.className = "add-keywords-to-save";
textbox.textContent = "Add";
//check if data exists
//check tablink in the already stored data as it is unique
//if the data is already present then button color would be blue
//else button color would be grey
tabLink = window.location.toString();
//foo(tabLink, text);
//console.log("stage1");
const isPresent = await getData(tabLink);
//console.log(isPresent.name);
//const isPresent = await getData(tabLink);
if(isPresent.name === text){
//if the datapoint already exists
//highlight the tab
//console.log("Saved Name == Current Name")
button.classList.add("highlight");
textbox.placeholder = isPresent.dataText;
}else{
//do nothing
}
button.onclick = async function (e) {
//e.stopPropagation();
if (button.classList.contains("highlight")) {
//console.log("stage2");
//await removeDatapoint(text);
//clearData();
//console.log("Data Removed!");
//remove the data from the database
await removeDatapoint(tabLink);
await getData(tabLink);
await allDatapoint();
//await removeDatapoint(tabLink);
textbox.value = "";
textbox.placeholder = "";
button.classList.remove("highlight");
} else {
//console.log("stage3");
//console.log(textbox.value);
//add the data to the database
await saveNewData(tabLink, text, textbox.value);
await getData(tabLink);
//await saveNewData(url, text);
textbox.placeholder = textbox.value;
button.classList.add("highlight");
}
};
var titleContent = document.querySelector(".ph5");
//console.log(titleContent);
//since this code would run once the DOM updates, we need to remove the last button first
//other wise the buttons would keep stacking
titleContent.removeChild(titleContent.lastChild);
titleContent.appendChild(button);
document.querySelector(".pv-top-card-v2-ctas").removeChild(document.querySelector(".pv-top-card-v2-ctas").lastChild);
document.querySelector(".pv-top-card-v2-ctas").appendChild(textbox);
}
})();
}