-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpopup.js
145 lines (121 loc) · 4.33 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
var links = document.getElementById("link");
let array = []; // array = ['0', 1', '2', '3', '4', '5', ....];
let len;
const cutoffLength = 55;
// get the how many tabs were open last time
chrome.storage.sync.get("no_of_tabs", function (data) {
if (!chrome.runtime.lastError) {
len = data["no_of_tabs"];
if (len != undefined) {
for (let index = 0; index < len; index++)
array.push(index.toString());
// get the last saved urls
chrome.storage.sync.get(array, function (data) {
if (!chrome.runtime.error) {
// console.log(data);
for (let i = 0; i < len; i++) {
//console.log(data[array[i]]);
let url = data[array[i]].url;
let title = data[array[i]].title;
let link = document.createElement("a");
let tr = document.createElement("tr");
let td = document.createElement("td");
link.setAttribute("href", url);
link.setAttribute("target", "blank");
if (title.length > cutoffLength)
link.textContent = `${title.slice(0, cutoffLength)}...`;
else
link.textContent = title;
td.appendChild(link);
tr.appendChild(td);
links.appendChild(tr);
}
}
})
}
} else {
console.log('error');
}
});
// get all the tabs and save
document.getElementById("save").onclick = () => {
//console.log("save clicked");
chrome.storage.sync.clear(function () {
if (!chrome.runtime.lastError) {
chrome.tabs.query({}, function (tabs) {
len = tabs.length;
chrome.storage.sync.set({ "no_of_tabs": len, }, () => {
if (!chrome.runtime.error)
console.log("saved it !!");
});
//tabs.forEach(function (tab)
for (let i = 0; i < len; i++) {
let tab = tabs[i];
let key = i.toString();
let obj = {
[key]: {
url: tab.url,
title: tab.title,
},
}
// console.log(obj);
chrome.storage.sync.set(obj, () => {
if (!chrome.runtime.error)
console.log("saved it !!");
});
}
});
}
});
};
document.getElementById("show").onclick = () => {
console.log("show clicked");
chrome.tabs.query({}, function (tabs) {
tabs.forEach(function (tab) {
let title = tab.title;
let url = tab.url;
let link = document.createElement("a");
let tr = document.createElement("tr");
let td = document.createElement("td");
link.setAttribute("href", url);
link.setAttribute("target", "blank");
if (title.length > cutoffLength)
link.textContent = `${title.slice(0, cutoffLength)}...`;
else
link.textContent = title;
td.appendChild(link);
tr.appendChild(td);
links.appendChild(tr);
});
});
}
document.getElementById("remove").onclick = () => {
console.log("remove clicked");
chrome.storage.sync.clear(function () {
var error = chrome.runtime.lastError;
if (error) {
console.error(error);
}
});
links.setAttribute("style", "display: none;");
}
// chrome.windows.getAll({ populate: true }, function (windows) {
// windows.forEach(function (window) {
// window.tabs.forEach(function (tab) {
// //collect all of the urls here, I will just log them instead
// console.log(tab.url);
// });
// });
// });
//TO-DO 1
/*
1. parse the url to get a proper name
2. save in local chrome storage
3. clear storage before save
4. save button
5. maybe a table
6. link not clicking
7. front-end fix
// TO-DO 2
3. checkbox to select which ones to save
*/