-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathoptions.js
153 lines (143 loc) · 6.16 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
/*
* chrome.storage.sync与chrome.storage.local的区别:
* chrome.storage.sync:同步存储,用户在不同的设备上登录时,数据会同步
* chrome.storage.local:本地存储,用户在不同的设备上登录时,数据不会同步
*/
let notificationToggle = document.getElementById('blockNotification');
let menuToggle = document.getElementById('blockMenuItem');
let markerInput = document.getElementById('markerInput');
// 获取blockNotification的值
chrome.storage.sync.get('blockNotification', function (data) {
let blockNotification = data.blockNotification ? data.blockNotification : true;
// 如果showNotification为true,则将开关打开
if (blockNotification) {
notificationToggle.checked = true;
} else {
notificationToggle.checked = false;
}
});
// 绑定开关的change事件
notificationToggle.addEventListener('change', function () {
let isChecked = notificationToggle.checked;
chrome.storage.sync.set({ blockNotification: isChecked });
console.log('blockNotification: ' + isChecked);
});
// 右键菜单开关
chrome.storage.sync.get('blockMenuItem', function (data) {
let blockMenuItem = data.blockMenuItem;
// 如果showNotification为true,则将开关打开
if (blockMenuItem) {
menuToggle.checked = true;
} else {
menuToggle.checked = false;
}
});
// 绑定开关的change事件
menuToggle.addEventListener('change', function () {
let isChecked = menuToggle.checked;
chrome.storage.sync.set({ blockMenuItem: isChecked });
console.log('blockMenuItem: ' + isChecked);
// chrome.runtime.reload(); // 重新加载插件
// 一秒后重新加载插件();
setTimeout(function () {
chrome.runtime.reload();
}, 1000);
});
// 获取默认Marker
chrome.storage.sync.get('BiliTimer_defaultMarker', function (data) {
markerInput.value = data.BiliTimer_defaultMarker ? data.BiliTimer_defaultMarker : '✨';
});
// 默认Marker设置修改事件
markerInput.addEventListener('blur', function () {
let marker = markerInput.value;
console.log('New default marker: ' + marker);
chrome.storage.sync.set({ BiliTimer_defaultMarker: marker });
});
// Marker模态框
document.getElementById('openModalButton').addEventListener('click', function () {
let myModal = new bootstrap.Modal(document.getElementById('myModal'), {
backdrop: true,
keyboard: false
});
myModal.show();
showMarkerList();
// 清除marker按钮
document.getElementById('clearBtn').addEventListener('click', function () {
// 获取勾选的checkbox
let checkedBvids = document.querySelectorAll('.checkBvid:checked');
if (checkedBvids.length == 0) return;
if (confirm('确认清空选中视频的所有标记?')) {
console.log(checkedBvids);
chrome.storage.sync.get('BiliTimer_marker', function (data) {
let markers = data.BiliTimer_marker;
for (let node of checkedBvids) {
let bvid = node.value;
console.log('Clear marker of ' + bvid + ' in ' + markers);
delete markers[bvid];
}
chrome.storage.sync.set({ BiliTimer_marker: markers });
showMarkerList();
});
}
});
// 保存marker按钮
document.getElementById('saveBtn').addEventListener('click', function () {
// 遍历表单中的input,将更新的marker存入本地存储
// 目标数据结构:{bvid: [[p:1, marker:'✨'], [p:2, marker:'✨']]}
let allcards = document.querySelectorAll('.modal .card-body');
let markers = {};
for (let card of allcards) {
let bvid = card.id;
let inputs = card.querySelectorAll('.input-group');
let markerList = Array.from(inputs).reduce((acc, cur) => {
const p = cur.querySelector('span').innerText;
const marker = cur.querySelector('input').value;
if (marker) acc.push([p, marker]);
return acc;
}, [])
if (markerList.length > 0) markers[bvid] = markerList;
}
chrome.storage.sync.set({ BiliTimer_marker: markers });
console.log('Saved markers: ', markers);
myModal.hide();
});
// 关闭dialog按钮
document.getElementById('closeBtn').addEventListener('click', function () {
myModal.hide();
});
});
function showMarkerList() {
chrome.storage.sync.get(null, function (allData) {
let allMarkers = allData.BiliTimer_marker;
let accordion = document.getElementById('accordionEx');
if (!allMarkers || JSON.stringify(allMarkers) === '{}') { accordion.innerHTML = `<div class="text-center">似乎啥也没标记过呢(_ _)。゜zzZ</div>`; return; }
console.log('All markers: ', allMarkers);
let html = '';
for (let [key, value] of Object.entries(allMarkers)) {
value.sort((a, b) => a[0] - b[0]);
let markerList = value.reduce((acc, cur, idx) => {
return acc + `<div class="input-group input-group-sm">
<span class="input-group-text" id="basic-addon${idx}">${cur[0]}</span>
<input type="text" class="form-control" value="${cur[1]}" aria-label="${cur[1]}">
</div>`
}, '')
html += `<div class="card mb-2">
<div class="card-header" style="padding:0">
<label class="btn btn-secondary" style="width:100%; border-bottom-left-radius:0;border-bottom-right-radius:0;" >
<input type="checkbox" autocomplete="off" class="checkBvid" value="${key}"> ${key}
</label>
</div>
<div class="collapse show" data-parent="#accordionEx">
<div class="card-body" id="${key}">
${markerList}
</div>
</div>
</div>`
}
accordion.innerHTML = html;
});
}
// 获取版本号
const manifest = chrome.runtime.getManifest();
const version = manifest.version;
document.getElementById('version').innerHTML = 'v' + version;