-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpopup.js
More file actions
173 lines (153 loc) · 5.39 KB
/
Copy pathpopup.js
File metadata and controls
173 lines (153 loc) · 5.39 KB
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
document.addEventListener('DOMContentLoaded', function () {
const addInput = document.getElementById('addInput');
const addButton = document.getElementById('addButton');
const removeButton = document.getElementById('removeButton');
const urlSelector = document.getElementById('urlSelector');
const currentUrl = document.getElementById('currentUrl');
const viewButton = document.getElementById('viewButton');
const viewIframe = document.getElementById('viewIframe');
// 加载保存的URL数组
chrome.storage.sync.get(['urlArray', 'selectedUrlIndex'], function (data) {
if (data.urlArray && Array.isArray(data.urlArray) && data.urlArray.length > 0) {
updateUrlSelector(data.urlArray, data.selectedUrlIndex);
}
});
// 导出URL按钮
getButton.addEventListener('click', function () {
chrome.storage.sync.get(['urlArray'], function (data) {
if (data.urlArray && Array.isArray(data.urlArray)) {
const urlString = data.urlArray.map(item => `${item.name}__${item.url}`).join(',');
addInput.value = urlString;
console.log("导出URL:", urlString);
}
});
});
// 添加URL按钮
addButton.addEventListener('click', function () {
const inputText = addInput.value.trim();
if (!inputText) {
alert('请输入URL信息');
return;
}
const urls = inputText.split(',');
chrome.storage.sync.get(['urlArray'], function (data) {
let urlArray = data.urlArray || [];
urls.forEach(url1 => {
const parts = url1.trim().split('__');
if (parts.length !== 2) {
alert('请输入正确的格式:名称__URL,例如:翻译__https://www.example.com');
return;
}
const name = parts[0].trim();
const url = parts[1].trim();
// 验证URL格式
if (!isValidUrl(url)) {
alert('请输入有效的URL (例如: 翻译__https://www.example.com)');
return;
}
// 检查是否已存在相同名称的URL
const existingIndex = urlArray.findIndex(item => item.name === name);
if (existingIndex !== -1) {
// 更新现有URL
urlArray[existingIndex] = { name, url };
} else {
// 添加新URL
urlArray.push({ name, url });
}
});
// 保存更新后的数组
chrome.storage.sync.set({ 'urlArray': urlArray }, function () {
// 更新选择器
updateUrlSelector(urlArray);
alert('URL已添加!');
});
})
});
// 移除URL按钮
removeButton.addEventListener('click', function () {
const selectedIndex = urlSelector.value;
if (selectedIndex === '') {
alert('请先选择要移除的URL');
return;
}
chrome.storage.sync.get(['urlArray'], function (data) {
if (data.urlArray && Array.isArray(data.urlArray)) {
// 移除选中的URL
data.urlArray.splice(selectedIndex, 1);
// 如果数组为空,清除选中索引
const selectedUrlIndex = data.urlArray.length > 0 ? 0 : null;
let savedUrl = null;
if (selectedUrlIndex != null) {
savedUrl = data.urlArray[selectedUrlIndex].url;
}
// 保存更新后的数组
chrome.storage.sync.set({
'urlArray': data.urlArray,
'selectedUrlIndex': selectedUrlIndex,
'savedUrl': savedUrl
}, function () {
// 更新选择器
updateUrlSelector(data.urlArray, selectedUrlIndex);
alert('URL已移除!');
});
}
});
});
// 查看按钮
viewButton.addEventListener('click', function () {
const selectedIndex = urlSelector.value;
if (selectedIndex === '') {
alert('请先选择要查看的URL');
return;
}
chrome.storage.sync.get('urlArray', function (data) {
if (data.urlArray && Array.isArray(data.urlArray) && data.urlArray[selectedIndex]) {
viewIframe.src = data.urlArray[selectedIndex].url;
}
});
});
// 更新URL选择器
function updateUrlSelector(urlArray, selectedIndex = 0) {
// 清空选择器
urlSelector.innerHTML = '';
if (urlArray.length === 0) {
// 如果没有URL,显示提示
currentUrl.textContent = '当前未选择URL';
return;
}
// 添加选项
urlArray.forEach((item, index) => {
const option = document.createElement('option');
option.value = index;
option.textContent = `${item.name}: ${item.url}`;
if (index === selectedIndex) {
option.selected = true;
}
urlSelector.appendChild(option);
});
// 更新当前URL显示
currentUrl.textContent = `当前选中: ${urlArray[selectedIndex].name} - ${urlArray[selectedIndex].url}`;
// 保存选中的索引 和 url
chrome.storage.sync.set({ 'selectedUrlIndex': selectedIndex });
chrome.storage.sync.set({ 'savedName': urlArray[selectedIndex].name });
chrome.storage.sync.set({ 'savedUrl': urlArray[selectedIndex].url });
}
// 选择器变化事件
urlSelector.addEventListener('change', function () {
const selectedIndex = parseInt(this.value);
chrome.storage.sync.get('urlArray', function (data) {
if (data.urlArray && Array.isArray(data.urlArray)) {
updateUrlSelector(data.urlArray, selectedIndex);
}
});
});
// 验证URL格式
function isValidUrl(url) {
try {
new URL(url);
return true;
} catch (e) {
return false;
}
}
});