-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpopup.js
More file actions
175 lines (155 loc) · 5.02 KB
/
Copy pathpopup.js
File metadata and controls
175 lines (155 loc) · 5.02 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
// 配置常量
const CONFIG = {
storageKey: 'mk_apps'
};
function loadApps() {
chrome.storage.sync.get([CONFIG.storageKey], (data) => {
let apps = Array.isArray(data[CONFIG.storageKey]) ? data[CONFIG.storageKey] : [];
if (apps.length === 0) apps = [getDefaultApp()];
renderApps(apps);
window._mk_apps = apps;
});
}
function getDefaultApp() {
return {
name: '',
baseUrl: '',
apiKey: ''
};
}
function renderApps(apps) {
const list = document.getElementById('apps-list');
list.innerHTML = '';
apps.forEach((app, idx) => {
const card = document.createElement('div');
card.className = 'app-card';
// header
const header = document.createElement('div');
header.className = 'app-card-header';
// 应用图标
const icon = document.createElement('div');
icon.className = 'app-icon';
icon.innerHTML = '📦';
header.appendChild(icon);
// 应用名称输入框
const nameInput = document.createElement('input');
nameInput.className = 'app-title-input';
nameInput.type = 'text';
nameInput.required = true;
nameInput.placeholder = '输入应用名称';
nameInput.value = app.name || '';
nameInput.maxLength = 20;
nameInput.oninput = (e) => {
app.name = e.target.value;
nameInput.classList.toggle('is-invalid', !app.name);
};
header.appendChild(nameInput);
// 操作按钮
const actions = document.createElement('div');
actions.className = 'app-card-actions';
// 展开按钮
const toggleBtn = document.createElement('button');
toggleBtn.className = 'action-btn toggle-btn';
toggleBtn.innerHTML = '▼';
toggleBtn.setAttribute('data-tooltip', '展开/折叠');
actions.appendChild(toggleBtn);
// 删除按钮
const delBtn = document.createElement('button');
delBtn.className = 'action-btn delete-btn';
delBtn.innerHTML = '🗑';
delBtn.setAttribute('data-tooltip', '删除应用');
delBtn.onclick = (e) => {
e.stopPropagation();
apps.splice(idx, 1);
renderApps(apps);
};
actions.appendChild(delBtn);
header.appendChild(actions);
card.appendChild(header);
// 内容区域
const content = document.createElement('div');
content.className = 'app-card-content';
content.innerHTML = `
<div class="form-group">
<label class="form-label">
<span>🔗</span>
<span>MaxKB 地址</span>
</label>
<div class="form-input-wrapper">
<input class="form-input app-baseurl" type="text" placeholder="如:http://ip:8080" value="${app.baseUrl || ''}" data-idx="${idx}" />
</div>
</div>
<div class="form-group">
<label class="form-label">
<span>🔑</span>
<span>应用 API 密钥</span>
</label>
<div class="form-input-wrapper">
<input class="form-input app-apikey" type="text" placeholder="请输入 API_KEY" value="${app.apiKey || ''}" data-idx="${idx}" />
</div>
</div>
`;
card.appendChild(content);
// 折叠/展开逻辑
const toggleCard = () => {
card.classList.toggle('expanded');
toggleBtn.innerHTML = card.classList.contains('expanded')
? '▲'
: '▼';
};
header.onclick = toggleCard;
list.appendChild(card);
});
// 监听URL和KEY输入
document.querySelectorAll('.app-baseurl').forEach(input => {
input.oninput = (e) => {
const idx = e.target.dataset.idx;
apps[idx].baseUrl = e.target.value;
};
});
document.querySelectorAll('.app-apikey').forEach(input => {
input.oninput = (e) => {
const idx = e.target.dataset.idx;
apps[idx].apiKey = e.target.value;
};
});
}
// 初始化事件监听
document.addEventListener('DOMContentLoaded', () => {
loadApps();
// 添加新应用
document.getElementById('add-app-btn').onclick = () => {
window._mk_apps.push(getDefaultApp());
renderApps(window._mk_apps);
};
// 保存配置
document.getElementById('save-all-btn').onclick = () => {
// 校验必填字段
if (window._mk_apps.some(app => !app.name)) {
alert('请填写所有应用的名称');
return;
}
chrome.storage.sync.set({
[CONFIG.storageKey]: window._mk_apps
}, () => {
// 显示保存成功的提示
const toast = document.createElement('div');
toast.className = 'toast';
toast.innerHTML = '<span>✓</span><span>配置已保存</span>';
document.body.appendChild(toast);
// 显示动画
setTimeout(() => {
toast.classList.add('show');
}, 10);
// 2秒后移除
setTimeout(() => {
toast.classList.remove('show');
setTimeout(() => {
if (toast.parentNode) {
toast.parentNode.removeChild(toast);
}
}, 300);
}, 2000);
});
};
});