-
Notifications
You must be signed in to change notification settings - Fork 2
/
popup.js
379 lines (278 loc) · 10.9 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
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
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
const buttonSearchCookies = document.querySelector("#btn-show-cookie");
const buttonDeleteCookies = document.querySelector("#btn-del-cookie");
const buttonDeleteLocalStorage = document.querySelector("#btn-del-localstorage");
const inputTextSearchFilterCookie = document.querySelector("#input-search-filter-cookie");
const inputTextDomainByUserInput = document.querySelector("#input-domaintext")
const ulContentCookieList = document.querySelector("#cookie-list");
const ulContentCookieListPtKeyPtPin = document.querySelector("#cookie-pt-key-pt-pin");
const spanContentBingCookie = document.querySelector("#bing_cookie");
const spanContentPtKeyAndPin = document.querySelector("#jd_pt_key_pin");
const spanContentPtKey = document.querySelector("#jd_pt_key");
const spanContentPtPin = document.querySelector("#jd_pt_pin");
const divElementContentList = document.querySelectorAll("[class*=site_intro]");;
const inputElementContentList = document.querySelectorAll("[class*=site_input]");;
const isDebug = false;
let allCookies = {
stringCookie: '',
objectCookie: {},
arrayCookie: []
}
// Chrome API Function
async function chromeGetCurrentTab() {
let queryOptions = { active: true, currentWindow: true }
let tabList = await chrome.tabs.query(queryOptions)
let [tab] = tabList
// console.log("currentTabList: ", tabList);
// console.log("currentTab: ", tab);
return tab;
}
async function chromeGetAllCookies(domain) {
let tempCookieList = await chrome.cookies.getAll({ domain: domain })
console.log("===== chromeGetAllCookies :", tempCookieList)
let stringCookie = ''
let objectCookie = {}
if (tempCookieList.length > 0) {
for (var i = 0; i < tempCookieList.length; i++) {
let tempOneCookie = tempCookieList[i]
if (tempOneCookie.name) {
stringCookie = stringCookie + tempOneCookie.name + "=" + tempOneCookie.value + "; ";
objectCookie[tempOneCookie.name] = tempOneCookie.value
}
}
}
return {
stringCookie,
objectCookie,
arrayCookie: tempCookieList
}
}
async function chromeRemoveAllCookies(domain){
chrome.cookies.getAll({ domain: domain }, (cookies) => {
// Loop through each cookie and remove it
for (const cookie of cookies) {
chrome.cookies.remove({ url: `http${cookie.secure ? 's' : ''}://${cookie.domain}${cookie.path}`, name: cookie.name });
}
alert("All Cookies removed");
});
}
async function chromeRemoveLocalStorage(domain){
chrome.storage.local.clear(function() {
var error = chrome.runtime.lastError;
if (error) {
console.error(error);
}
alert("All LocalStorage removed");
});
chrome.storage.sync.clear(); // callback is optional
}
// Chrome Storage function
function saveUserFavoriteLinkListToSyncStorage(linkList) {
if (linkList && Array.isArray(linkList)) {
const linkListTemp = linkList.map(link => {
return link.threadId
})
chrome.storage.sync.set({ tgfcerUserFavoriteLinkIdList: linkListTemp }, function() {
console.log('Chrome Sync storage saved data: ', linkListTemp)
})
}
}
// Tools function
function getRandomInt(min, max) {
return Math.floor(Math.random() * (max - min + 1)) + min
}
function fallbackCopyTextToClipboard(text) {
var copyFrom = document.createElement("textarea");
copyFrom.textContent = text;
var body = document.getElementsByTagName('body')[0];
body.appendChild(copyFrom);
copyFrom.select();
document.execCommand('copy');
body.removeChild(copyFrom);
}
function copyTextToClipboard(text) {
if (!navigator.clipboard) {
fallbackCopyTextToClipboard(text);
return;
}
navigator.clipboard.writeText(text).then(function() {
console.log('Async: Copying to clipboard was successful!');
alert("Already copy To Clipboard! ")
}, function(err) {
console.error('Async: Could not copy text: ', err);
});
}
async function getDomainFromUrl() {
let currentTab = await chromeGetCurrentTab();
let url = currentTab.url;
let domain = {
topDomainId: '',
topDomain: '',
subDomain: '',
topDomainWithHttps: '',
subDomainWithHttps: ''
}
let urlWithoutHttpOrWWW = url.replace(/(https?:\/\/)?(www.)?/i, '');
// console.log("urlWithoutHttpOrWWW: ", urlWithoutHttpOrWWW)
let urlArray = url.split("/");
// console.log("urlArray: ", urlArray)
let domainArray = urlArray[2].split('.');
// console.log("domainArray : ", domainArray);
domain.topDomainId = domainArray.slice(-2, -1).join('');
domain.topDomain = domainArray.slice(-2).join('.');
domain.subDomain = urlArray[2]
domain.topDomainWithHttps = urlArray[0] + '//' + domain.topDomain
domain.subDomainWithHttps = urlArray[0] + '//' + urlArray[2]
// console.log("currentTabDomain : ", domain);
return domain
}
// Main Feature Function
async function showCurrentBox(siteIdString) {
if (!siteIdString) {
let domain = await getDomainFromUrl();
siteIdString = domain.topDomainId;
}
console.log("===== currentSiteId: ", siteIdString)
// 根据不同网站显示不同的介绍说明
let counterDiv = 0;
let tempIntroDiv = null;
let counterInput = 0;
let tempSpan = null;
divElementContentList.forEach((tempDiv) => {
if (tempDiv.className.indexOf(siteIdString) > -1) {
tempDiv.classList.remove("d-none");
counterDiv = counterDiv + 1;
} else {
tempDiv.classList.add("d-none");
}
if (tempDiv.className.indexOf('other') > -1) {
tempIntroDiv = tempDiv
}
})
if (counterDiv === 0) {
tempIntroDiv.classList.remove("d-none");
}
// 根据不同网站显示不同的输入框
inputElementContentList.forEach((tempDiv2) => {
if (tempDiv2.className.indexOf(siteIdString) > -1) {
tempDiv2.classList.remove("d-none");
counterInput = counterInput + 1;
} else {
tempDiv2.classList.add("d-none");
}
if (tempDiv2.className.indexOf('other') > -1) {
tempSpan = tempDiv2
}
})
if (counterInput === 0) {
tempSpan.classList.remove("d-none");
}
if (siteIdString.indexOf('jd') > -1 ) {
inputTextDomainByUserInput.placeholder='https://home.m.jd.com';
} else if (siteIdString.indexOf('bing') > -1 ) {
inputTextDomainByUserInput.placeholder='https://www.bing.com/images/create';
}
// 根据不同网站显示不同的Cookie列表
if (siteIdString.indexOf('jd') > -1 ) {
spanContentPtKeyAndPin.classList.remove("d-none");
spanContentPtKey.classList.remove("d-none");
spanContentPtPin.classList.remove("d-none");
spanContentBingCookie.classList.add("d-none");
} else {
spanContentBingCookie.classList.remove("d-none");
spanContentPtKeyAndPin.classList.add("d-none");
spanContentPtKey.classList.add("d-none");
spanContentPtPin.classList.add("d-none");
}
}
function htmlClearList() {
ulContentCookieList.innerHTML = '';
}
let liCounter = 1;
function htmlAppendList(text) {
var li = document.createElement("li");
li.setAttribute("id", "element_" + liCounter);
li.appendChild(document.createTextNode(text));
li.classList.add("list-group-item");
ulContentCookieList.appendChild(li);
liCounter = liCounter + 1
}
function modifyText(node, text) {
node.textContent = text;
}
function delegateSelectLi() {
ulContentCookieList.addEventListener("click", function(e) {
if (e.target && e.target.nodeName == "LI") {
// List item found! Output the ID!
console.log("List item ", e.target.id, " was clicked!");
copyTextToClipboard(e.target.innerHTML)
}
});
ulContentCookieListPtKeyPtPin.addEventListener("click", function(e) {
if (e.target && e.target.nodeName == "LI") {
// List item found! Output the ID!
console.log("List item ", e.target.id, " was clicked!");
copyTextToClipboard(e.target.innerHTML)
}
});
// buttonSearchCookies.forEach((tempButton) => {
// tempButton.addEventListener('click', onClickButtonSearchCookie, false);
// })
}
async function showFilterCookieList(list) {
let inputFilterText = document.querySelector("#input-search-filter-cookie").value
if (list.arrayCookie.length > 0) {
htmlClearList();
if (inputFilterText) {
list.arrayCookie.forEach((tempOneCookie) => {
if (tempOneCookie.name && tempOneCookie.name.indexOf(inputFilterText) > -1) {
htmlAppendList(tempOneCookie.name + "=" + tempOneCookie.value + "; ")
}
})
} else {
list.arrayCookie.forEach((tempOneCookie2) => {
if (tempOneCookie2.name) {
htmlAppendList(tempOneCookie2.name + "=" + tempOneCookie2.value + "; ")
}
})
}
}
}
async function onClickButtonSearchCookie(event3) {
event3.preventDefault()
let domain = await getDomainFromUrl();
let inputCurrentDomain = inputTextDomainByUserInput.value
// console.log("===== inputTextDomainByUserInput value: ", inputTextDomainByUserInput.value)
if (!inputCurrentDomain) {
inputTextDomainByUserInput.value = domain.topDomain
inputCurrentDomain = domain.topDomain
console.log("===== inputCurrentDomain: ", inputCurrentDomain)
}
allCookies = await chromeGetAllCookies(inputCurrentDomain)
await showFilterCookieList(allCookies)
if (domain.topDomainId.indexOf('jd') > -1 ) {
modifyText(spanContentPtKey, "pt_key=" + allCookies.objectCookie.pt_key + ";")
modifyText(spanContentPtPin, "pt_pin=" + allCookies.objectCookie.pt_pin + ";")
modifyText(spanContentPtKeyAndPin, "pt_key=" + allCookies.objectCookie.pt_key + ";" + "pt_pin=" + allCookies.objectCookie.pt_pin + ";")
} else {
modifyText(spanContentBingCookie, allCookies.stringCookie)
}
}
async function onClickInputFilterCookies(event3) {
await showFilterCookieList(allCookies)
}
async function onClickButtonDeleteCookie(event5) {
event5.preventDefault()
let domain = await getDomainFromUrl();
chromeRemoveAllCookies(domain.topDomain)
}
async function onClickButtonDeleteLocalStorage(event5) {
event5.preventDefault()
// let domain = await getDomainFromUrl();
chromeRemoveLocalStorage()
}
buttonSearchCookies.addEventListener('click', onClickButtonSearchCookie, false);
buttonDeleteCookies.addEventListener('click', onClickButtonDeleteCookie, false);
buttonDeleteLocalStorage.addEventListener('click', onClickButtonDeleteLocalStorage, false);
inputTextSearchFilterCookie.addEventListener('input', onClickInputFilterCookies, false);
delegateSelectLi()
showCurrentBox()