-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpopup.js
More file actions
313 lines (289 loc) · 12 KB
/
popup.js
File metadata and controls
313 lines (289 loc) · 12 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
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
// 函数:切换参数的启用/禁用状态
function toggleParamState(key, currentState) {
chrome.storage.local.get(key, function (result) {
if (chrome.runtime.lastError) {
console.error(
"self Error fetching item for toggle:",
chrome.runtime.lastError
);
return;
}
const item = result[key];
if (item && typeof item === "object") {
item.enabled = !currentState;
chrome.storage.local.set({ [key]: item }, function () {
if (chrome.runtime.lastError) {
console.error(
"self Error updating item state:",
chrome.runtime.lastError
);
return;
}
console.log(`self ${key} state toggled to ${item.enabled}`);
loadSavedParams(); // 重新加载列表以反映更改
});
}
});
}
// 函数:删除已保存的参数
function deleteParam(key) {
chrome.storage.local.remove(key, function () {
if (chrome.runtime.lastError) {
console.error("self Error deleting item:", chrome.runtime.lastError);
return;
}
console.log(`self ${key} deleted`);
loadSavedParams(); // 重新加载列表以反映更改
});
}
// 函数:加载并显示已保存的参数
function loadSavedParams(searchTerm = "") {
// 添加 searchTerm 参数,默认为空字符串
console.log("self loadSavedParams called with searchTerm:", searchTerm); // 调试信息
const savedParamsListDiv = document.getElementById("savedParamsList");
savedParamsListDiv.innerHTML = ""; // 清空现有列表
chrome.storage.local.get(null, function (items) {
console.log("self all items from storage", items); // 调试信息
// 检查 items 是否为空对象,如果是,则表示没有保存任何参数
if (Object.keys(items).length === 0) {
savedParamsListDiv.textContent = "No parameters saved yet.";
return;
}
if (chrome.runtime.lastError) {
console.error(
"self Error retrieving saved params:",
chrome.runtime.lastError
);
savedParamsListDiv.textContent = "Error loading parameters.";
return;
}
let keys = Object.keys(items);
// 如果 searchTerm 有值,则过滤 keys
if (searchTerm) {
const lowerCaseSearchTerm = searchTerm.toLowerCase();
keys = keys.filter((key) =>
key.toLowerCase().includes(lowerCaseSearchTerm)
);
console.log("self filtered keys:", keys); // 调试信息
}
const ul = document.createElement("ul");
// ul 的样式现在通过 popup.html 中的 CSS 控制
keys.forEach(function (key) {
const itemData = items[key];
// 现在我们存储的是对象,包含 params 和 enabled 状态
if (
itemData &&
typeof itemData === "object" &&
typeof itemData.params === "string"
) {
const li = document.createElement("li");
const itemHeaderDiv = document.createElement("div"); // 新的父div,用于包裹路径和按钮
itemHeaderDiv.className = "param-item-header"; // 为这个新的div添加class,方便CSS控制
const pathStrong = document.createElement("strong");
pathStrong.textContent = key;
itemHeaderDiv.appendChild(pathStrong); // 将路径添加到新的父div中
// 创建操作按钮区域
const actionsDiv = document.createElement("div");
actionsDiv.className = "param-actions";
// 创建禁用/启用按钮
const toggleButton = document.createElement("button");
toggleButton.textContent = itemData.enabled ? "Disable" : "Enable";
toggleButton.classList.add("action-button", "toggle-btn");
toggleButton.classList.toggle("enabled", itemData.enabled);
toggleButton.classList.toggle("disabled", !itemData.enabled);
toggleButton.addEventListener("click", function () {
toggleParamState(key, itemData.enabled);
});
actionsDiv.appendChild(toggleButton);
// 创建删除按钮
const deleteButton = document.createElement("button");
deleteButton.textContent = "Delete";
deleteButton.classList.add("action-button", "delete-btn");
deleteButton.addEventListener("click", function () {
// 添加确认步骤,防止误删
if (
confirm(`Are you sure you want to delete parameters for ${key}?`)
) {
deleteParam(key);
}
});
actionsDiv.appendChild(deleteButton);
// 创建Goto按钮
const gotoButton = document.createElement("button");
gotoButton.textContent = "Goto";
gotoButton.classList.add("action-button", "goto-btn");
gotoButton.addEventListener("click", function () {
let urlToOpen = key;
// 确保 key (域名+路径) 是一个有效的 URL 开头
if (!key.startsWith("http://") && !key.startsWith("https://")) {
// 尝试从当前标签页获取协议,或者默认使用 https
// 注意:在 popup 中直接获取当前标签页协议可能复杂,简单起见,我们先假定一个
// 更健壮的做法可能是在保存时也保存协议,或者尝试两种协议
urlToOpen = "https://" + key;
}
const params = itemData.params.startsWith("?")
? itemData.params
: "?" + itemData.params;
if (itemData.params) {
// 只有当存在参数时才添加
urlToOpen += params;
}
console.log("self urlToOpen", urlToOpen); // 调试信息
chrome.tabs.create({ url: urlToOpen });
});
actionsDiv.appendChild(gotoButton);
itemHeaderDiv.appendChild(actionsDiv); // 将按钮区域添加到新的父div中
li.appendChild(itemHeaderDiv); // 将新的父div添加到li中
const paramsString = itemData.params.startsWith("?")
? itemData.params.substring(1)
: itemData.params;
if (paramsString) {
const paramsArray = paramsString.split("&");
const paramsUl = document.createElement("ul");
paramsUl.className = "params-list"; // 使用CSS类进行样式化
paramsArray.forEach((param) => {
const paramLi = document.createElement("li");
paramLi.textContent = param;
paramsUl.appendChild(paramLi);
});
li.appendChild(paramsUl); // 参数列表直接添加到li中,位于新的父div之后
}
// itemHeaderDiv.appendChild(actionsDiv); // 这行已移到上面
// li.appendChild(itemHeaderDiv); // 这行已移到上面
// const paramsString = ... // 这部分逻辑也已移到上面,并调整了paramsUl的添加位置
// if (paramsString) { ... }
ul.appendChild(li);
}
});
if (ul.childNodes.length > 0) {
savedParamsListDiv.appendChild(ul);
} else {
savedParamsListDiv.textContent = "No parameters saved yet.";
}
});
}
document.addEventListener("DOMContentLoaded", function () {
const searchInput = document.getElementById("searchInput"); // 定义 searchInput 一次
// 尝试从本地存储加载上一次的搜索关键词
chrome.storage.local.get(["lastSearchTerm"], function (result) {
if (chrome.runtime.lastError) {
console.error(
"self Error loading lastSearchTerm:",
chrome.runtime.lastError
);
loadSavedParams(); // 加载所有参数
if (searchInput) {
searchInput.focus(); // 聚焦搜索框
console.log(
"self searchInput focused after error loading lastSearchTerm."
); // 调试信息
}
} else if (result.lastSearchTerm && searchInput) {
// 确保 searchInput 存在
searchInput.value = result.lastSearchTerm;
console.log("self Restored last search term:", result.lastSearchTerm); // 调试信息
loadSavedParams(result.lastSearchTerm); // 使用恢复的关键词加载列表
searchInput.focus(); // 聚焦搜索框
console.log("self searchInput focused after restoring lastSearchTerm."); // 调试信息
} else {
// 如果没有保存的关键词,或者 searchInput 为 null
loadSavedParams(); // 页面加载时加载参数列表
if (searchInput) {
searchInput.focus(); // 聚焦搜索框
console.log(
"self searchInput focused (no lastSearchTerm or searchInput initially null)."
); // 调试信息
}
}
});
// 后续的 if (searchInput) { ... } 中的 searchInput 会使用上面定义的实例
if (searchInput) {
searchInput.addEventListener("keyup", function (event) {
console.log("self searchInput keyup event, key:", event.key); // 调试信息
if (event.key === "Enter") {
const searchTerm = searchInput.value.trim();
loadSavedParams(searchTerm);
// 保存当前搜索关键词到本地存储
chrome.storage.local.set({ lastSearchTerm: searchTerm }, function () {
if (chrome.runtime.lastError) {
console.error(
"self Error saving lastSearchTerm:",
chrome.runtime.lastError
);
}
console.log("self Saved last search term:", searchTerm); // 调试信息
});
}
});
// 可选:当搜索框清空时也重新加载所有参数
searchInput.addEventListener("input", function () {
const searchTerm = searchInput.value.trim();
if (searchTerm === "") {
loadSavedParams(); // 传入空字符串以加载所有
// 当搜索框清空时,也清除保存的搜索关键词
chrome.storage.local.remove("lastSearchTerm", function () {
if (chrome.runtime.lastError) {
console.error(
"self Error removing lastSearchTerm:",
chrome.runtime.lastError
);
}
console.log("self Cleared last search term as input is empty."); // 调试信息
});
}
});
}
// loadSavedParams(); // 这行被移到 chrome.storage.local.get 的回调中,以确保在尝试恢复搜索词后才加载
const saveButton = document.getElementById("saveParams");
saveButton.addEventListener("click", function () {
chrome.tabs.query({ active: true, currentWindow: true }, function (tabs) {
const currentTab = tabs[0];
if (currentTab && currentTab.url) {
try {
const url = new URL(currentTab.url);
const params = url.search;
const key = `${url.hostname}${url.pathname}`;
if (params) {
// 存储包含参数和启用状态的对象
const dataToStore = { params: params, enabled: true };
chrome.storage.local.set({ [key]: dataToStore }, function () {
if (chrome.runtime.lastError) {
console.error(
"self Error saving params:",
chrome.runtime.lastError
);
saveButton.textContent = "Save failed!";
setTimeout(() => {
saveButton.textContent = "Save Parameters";
}, 2000);
return;
}
console.log("self params saved", params);
// 可以选择性地给用户一个反馈,比如改变按钮文字或显示一条消息
saveButton.textContent = "Parameters saved!";
setTimeout(() => {
saveButton.textContent = "Save Parameters";
}, 2000);
loadSavedParams(); // 保存成功后重新加载列表
});
} else {
// 如果没有参数,也可以提示用户
saveButton.textContent = "No parameters to save";
setTimeout(() => {
saveButton.textContent = "Save Parameters";
}, 2000);
}
} catch (e) {
console.error("Error processing URL: ", e);
// 处理无效URL的情况
saveButton.textContent = "Invalid URL";
setTimeout(() => {
saveButton.textContent = "Save Parameters";
}, 2000);
}
} else {
console.error("Could not get current tab URL");
}
});
});
});