-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.js
More file actions
360 lines (336 loc) · 12.3 KB
/
main.js
File metadata and controls
360 lines (336 loc) · 12.3 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
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
// 插入侧栏与页面
const extensionShopPage = ExtensionFunctions.insertNavigationItem({
pageId: "extensionShopPage",
icon: "EA44",
text: "商店",
});
ExtensionFunctions.insertStyle(`
#extensionShopPage #extensionShopContainer{
position: absolute;
z-index: 1;
padding: 70px 27.5px 100px 27.5px;
padding-top: 90px !important;
width: 100%;
height: 100%;
overflow-y: scroll;
}
#extensionShopPage #extensionShopContainer>div {
background: white;
width: 100%;
border-radius: 5px;
margin-bottom: 5px;
padding: 10px 15px;
display: flex;
align-items: center;
}
#extensionShopPage #extensionShopContainer>div section {
width: 100%;
margin-right: 10px;
}
#extensionShopPage #extensionShopContainer>div section div {
font-size: 1em;
}
#extensionShopPage #extensionShopContainer>div section span {
display: block;
font-size: .9em;
opacity: .8;
word-break: break-all;
line-height: 1.1em;
margin-top: 3px;
}
#extensionShopPage #extensionShopContainer button {
white-space: nowrap;
}
#extensionShopPage #extensionShopHeader {
display: flex;
align-items: center;
}
#extensionShopPage #extensionSearchForm {
font-size: medium;
max-width: 230px;
margin-left: auto;
margin-top: 6px;
margin-bottom: 0;
display: flex;
flex-direction: row;
gap: 5px;
}
#extensionShopPage #extensionSearchInput {
font-size: 12px;
margin: 0;
padding: 5px;
}
#extensionShopPage #extensionSearchButton {
width: 32px;
height: 26px;
padding: 0px;
font-size: 16px;
padding-left: 5px;
}
`);
extensionShopPage.pageDiv.classList.add("page");
extensionShopPage.pageDiv.innerHTML = `<div class="header" id="extensionShopHeader">
<i></i> 扩展商店
<form class="inputGroup" id="extensionSearchForm">
<input id="extensionSearchInput" placeholder="搜索扩展" spellcheck="false">
<button id="extensionSearchButton"><i></i></button>
</form>
</div>
<div>
<div id="extensionShopContainer">列表加载中...</div>`;
extensionShopPage.navbarDiv.addEventListener("click", (event) => {
loadStoreData();
});
async function downloadAndInstallPlugin(url, buttonElement) {
const tempDir = require("os").tmpdir();
const filename = url.substring(url.lastIndexOf("/") + 1);
const filePath = path.join(tempDir, filename);
const xhr = new XMLHttpRequest();
xhr.onprogress = function (event) {
if (event.lengthComputable) {
const percentComplete = (event.loaded / event.total) * 100;
console.log(`下载进度: ${percentComplete.toFixed(2)}%`);
}
};
xhr.onload = function () {
if (xhr.status === 200) {
const blob = xhr.response;
const fileReader = new FileReader();
fileReader.onloadend = function () {
const buffer = Buffer.from(fileReader.result);
fs.writeFile(filePath, buffer, async function (err) {
if (err) {
console.error(`写入文件失败: ${err}`);
} else {
console.log(`文件已保存至: ${filePath}`);
const zipFileBuffer = await fs.promises.readFile(filePath);
const file = new File([zipFileBuffer], path.basename(filePath), {
type: "application/zip",
});
ExtensionRuntime.install(file);
}
});
};
fileReader.readAsArrayBuffer(blob); // Ensure xhr.response is a Blob
} else {
console.error(`下载失败,状态码: ${xhr.status}`);
}
};
xhr.onerror = function () {
console.error(`下载出错`);
};
xhr.open("GET", url, true);
xhr.responseType = "blob"; // Ensure xhr.responseType is set to 'blob'
xhr.send();
}
async function fetchPluginList() {
const response = await fetch(
`https://api.3r60.top/v2/ghproxy/?url=` +
encodeURI(
"https://raw.githubusercontent.com/PYLXU/pluginStore/main/pluginList.json"
)
);
if (!response.ok) {
throw new Error(
`插件列表载入失败: ${response.status} ${response.statusText}(请确保您能够连接到Github)`
);
}
return await response.json();
}
async function fetchLatestRelease(repoName) {
const [owner, repo] = repoName.split("/");
const url = `https://proxies.3r60.top/https://api.github.com/repos/${owner}/${repo}/releases/latest`;
const response = await fetch(url);
if (!response.ok) {
throw new Error(
`无法获取插件构建,存储库: ${repoName}: ${response.status} ${response.statusText}`
);
}
return await response.json();
}
async function fetchManifest(repoName, tag) {
const url =
`https://api.3r60.top/v2/ghproxy/?url=` +
encodeURI(
`https://raw.githubusercontent.com/${repoName}/${tag}/manifest.json`
);
const response = await fetch(url);
if (!response.ok) {
throw new Error(
`清单获取失败|${repoName}: ${response.status} ${response.statusText}`
);
}
return await response.json();
}
async function fetchPackageJson(repoName, tag) {
const url =
`https://api.3r60.top/v2/ghproxy/?url=` +
encodeURI(
`https://raw.githubusercontent.com/${repoName}/${tag}/package.json`
);
const response = await fetch(url);
if (!response.ok) {
throw new Error(
`清单获取失败|${repoName}: ${response.status} ${response.statusText}`
);
}
return await response.json();
}
function insertAfter(newElement, targetElement) {
var parent = targetElement.parentNode;
if (parent.lastChild == targetElement) {
parent.appendChild(newElement);
} else {
parent.insertBefore(newElement, targetElement.nextSibling);
}
}
async function loadStoreData() {
const extensionContainer = document.getElementById("extensionShopContainer");
extensionContainer.innerHTML = `<div style="gap: 10px;">
<span>扩展分类</span>
<button class="extensionCategoryButton" data-category="">全部</button>
<button class="extensionCategoryButton sub" data-category="[音源]">音源</button>
<button class="extensionCategoryButton sub" data-category="[门户]">门户</button>
<button class="extensionCategoryButton sub" data-category="[功能]">功能</button>
<button class="extensionCategoryButton sub" data-category="[美化]">美化</button>
<button class="extensionCategoryButton sub" data-category="[补丁]">补丁</button>
</div>`;
document.querySelectorAll(".extensionCategoryButton").forEach((button) => {
button.addEventListener("click", (event) => {
const category = event.target.getAttribute("data-category");
search(category);
document.querySelectorAll(".extensionCategoryButton").forEach((btn) => {
btn.classList.add("sub");
});
event.target.classList.remove("sub");
});
});
try {
const plugins = await fetchPluginList();
for (const plugin of plugins) {
const repoName = plugin.name;
try {
const release = await fetchLatestRelease(repoName);
const owner = repoName.split("/")[0];
const packageName = plugin.id;
const version =
plugin.version == "releases"
? release.tag_name
: (await fetchPackageJson(repoName, release.tag_name)).version;
const onlinePluginList = document.createElement("div");
const extData = await ExtensionRuntime.getExtData();
let buttonText;
let buttonDisabled = false;
if (extData.hasOwnProperty(packageName)) {
if (extData[packageName].version == version) {
buttonText = "已安装";
buttonDisabled = true;
} else {
buttonText = "更新";
}
} else {
buttonText = "安装";
}
onlinePluginList.innerHTML += `
<section>
<div>${plugin.uiName}<small> - ${owner}</small></div>
<span>
<i></i> 扩展包名: ${packageName}<br>
<i></i> 扩展版本: ${version}<br>
</span>
</section>
<button class="sub installButton" data-package-name="${packageName}" data-repo-name="${repoName}" data-tag-name="${
release.tag_name
}" ${buttonDisabled ? "disabled" : ""}>${buttonText}</button>
`;
onlinePluginList.className = "onlineEtensionCard";
extensionContainer.appendChild(onlinePluginList);
onlinePluginList
.querySelector(`.installButton`)
.addEventListener("click", async (event) => {
const button = event.target;
const repoName = button.getAttribute("data-repo-name");
const tagName = button.getAttribute("data-tag-name");
const packageName = button.getAttribute("data-package-name");
button.disabled = true;
button.textContent = "安装中...";
downloadAndInstallPlugin(
`https://api.3r60.top/v2/ghproxy/?url=` +
encodeURI(
`https://github.com/${repoName}/releases/latest/download/extension.zip`
),
button
);
});
} catch (error) {
const onlinePluginList = document.createElement("div");
const owner = repoName.split("/")[0];
const packageName = plugin.id;
const version = "请求失败,请稍后再试";
let buttonText = "重装";
let buttonDisabled = false;
onlinePluginList.innerHTML += `
<section>
<div>${plugin.uiName}<small> - ${owner}</small></div>
<span>
<i></i> 扩展包名: ${packageName}<br>
<i></i> 扩展版本: ${version}<br>
</span>
</section>
<button class="sub installButton" data-package-name="${packageName}" data-repo-name="${repoName}" data-tag-name="latest" ${
buttonDisabled ? "disabled" : ""
}>${buttonText}</button>
`;
onlinePluginList.className = "onlineEtensionCard";
extensionContainer.appendChild(onlinePluginList);
onlinePluginList
.querySelector(`.installButton`)
.addEventListener("click", async (event) => {
const button = event.target;
const repoName = button.getAttribute("data-repo-name");
const tagName = button.getAttribute("data-tag-name");
const packageName = button.getAttribute("data-package-name");
button.disabled = true;
button.textContent = "安装中...";
downloadAndInstallPlugin(
`https://api.3r60.top/v2/ghproxy/?url=` +
encodeURI(
`https://github.com/${repoName}/releases/latest/download/extension.zip`
),
button
);
});
}
}
} catch (error) {
const errorDisplay = document.createElement("div");
errorDisplay.innerHTML += `
<section>
<div>载入在线列表时出现了错误</div>
<span>
<i></i> 错误详情: ${error}<br>
</span>
</section>
<button class="sub" disabled>安装</button>
`;
extensionContainer.appendChild(errorDisplay);
}
}
document
.getElementById("extensionSearchForm")
.addEventListener("submit", (event) => {
event.preventDefault();
search(document.getElementById("extensionSearchInput").value);
});
function search(keyword) {
const extensionContainer = document.getElementById("extensionShopContainer");
const cards = extensionContainer.querySelectorAll(".onlineEtensionCard");
cards.forEach((card) => {
const textContent = card.textContent || card.innerText;
if (textContent.toLowerCase().includes(keyword.toLowerCase())) {
card.style.display = "";
} else {
card.style.display = "none";
}
});
}