-
Notifications
You must be signed in to change notification settings - Fork 0
/
options.js
196 lines (177 loc) · 5.05 KB
/
options.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
document.addEventListener('DOMContentLoaded', () =>{
//Pages list
const days_of_week = {
0 : "Sun",
1 : "Mon",
2 : "Tue",
3 : "Wed",
4 : "Thr",
5 : "Fri",
6 : "Sat"
};
const reverse_days_of_Week = {
"Sun": 0,
"Mon": 1,
"Tue": 2,
"Wed": 3,
"Thr": 4,
"Fri": 5,
"Sat": 6
};
let selected_row = null;
let origin_url = null;
const table = new Tabulator("#grid-view", {
columns: [{
title: "URL",
field: "url",
editor:"input",
width:"30%"
},
{
title: "Title",
field: "title",
editor:"input",
width:"30%"
},
{
title: "Interval",
field: "interval",
sorter: "number",
editor:"number",
width:"7%"
},
{
title: "Day of week",
field: "day_of_week",
sorter: (day1, day2) => (dayOfWeek[day1] - dayOfWeek[day2]),
editor:"select", editorParams:{values:["Sun", "Mon", "Tue", "Wed", "Thr", "Fri", "Sat"]},
width:"8%"
},
{
title: "Last shown time",
field: "last_shown_time",
sorter: "datetime",
width: "25%"
},
],
cellVertAlign: "middle",
maxHeight: "60%",
//layout: "fitData",
addRowPos: "top",
selectable: 1,
rowSelectionChanged: function(data, rows) {
if(data[0]){
selected_row = rows[0];
origin_url = data[0].url;
}
else{
selected_row = null;
origin_url = null;
}
},
rowDblClick:function(e, row){
//e - the click event object
const url = row.getData().url;
chrome.tabs.create({url:url})
selected_row = row;
origin_url = url;
},
cellEdited:function(cell){
const data = cell.getData();
//if(data.url && !data.title) cell.getRow()
if(!data.url || !data.interval) return;
chrome.storage.local.get("pages", (result) => {
if(origin_url && origin_url!=data.url){
delete result.pages[origin_url]
}
const day_of_week_int = data.day_of_week ? reverse_days_of_Week[data.day_of_week] : null;
const last_shown_time = result.pages[data.url] ? result.pages[data.url].last_shown_time : new Date().getTime();
result.pages[data.url] = {
interval:data.interval,
last_shown_time:last_shown_time,
day_of_week:day_of_week_int,
title:data.title
}
chrome.storage.local.set({"pages":result.pages}, () => {})
})
},
});
chrome.storage.local.get("pages", (result) => {
for(const [url, detail] of Object.entries(result.pages)){
const day_of_week_name = detail.interval%7==0 ? days_of_week[detail.day_of_week] : null;
table.addData({url:url, title:detail.title, interval: detail.interval,
day_of_week:day_of_week_name, last_shown_time:new Date(detail.last_shown_time)})
}
table.setSort("last_shown_time", "desc");
});
document.getElementById("delete").addEventListener("click", function(){
if(!selected_row) return;
const ret = window.confirm("Do you really delete?");
if(!ret) return;
chrome.storage.local.get("pages", (result) => {
delete result.pages[origin_url]
chrome.storage.local.set(
{"pages":result.pages}, () => {
selected_row.delete();
}
)
})
});
document.getElementById("add").addEventListener("click", function(){
table.addRow({});
});
//Interval menu list
const interval_list = document.getElementById('interval_list');
let menu;
chrome.storage.local.get("menu", (result) => {
menu = result.menu;
if(!menu){
chrome.storage.local.set({"menu": [1,3,7,14,30,90,180,365,1000]});
menu = [1,3,7,14,30,90,180,365,1000];
}
for(const interval of menu){
const opt = document.createElement("option");
opt.value = interval;
opt.innerText = interval;
interval_list.appendChild(opt);
}
});
document.getElementById("delete_interval").addEventListener("click", function(){
if(interval_list.selectedIndex==-1) return;
menu.splice(interval_list.selectedIndex, 1)
chrome.storage.local.set(
{"menu":menu}, () => {
interval_list.remove(interval_list.selectedIndex);
}
)
});
function add_interval(){
const interval_input = document.getElementById("interval_input");
const new_interval = parseInt(interval_input.value);
if(!Number.isInteger(new_interval) || new_interval<=0){
alert("Please set integer over zero");
return;
}
let insertIndex = -1;
for(const [index, interval] of menu.entries()){
if(new_interval<interval){
insertIndex = index;
break;
}
}
if(insertIndex==-1) insertIndex = menu.length;
menu.splice(insertIndex, 0, new_interval);
chrome.storage.local.set({"menu":menu})
const opt = document.createElement("option");
opt.value = new_interval;
opt.innerText = new_interval;
if(insertIndex<menu.length)interval_list.insertBefore(opt, interval_list.children[insertIndex]);
else interval_list.appendChild(opt);
interval_input.value = "";
}
document.getElementById("add_interval").addEventListener("click", add_interval);
document.getElementById("interval_input").addEventListener("keydown", (e)=>{
if(e.code == 'Enter') add_interval();
});
return true;
});