-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscript.js
More file actions
119 lines (107 loc) · 5.36 KB
/
script.js
File metadata and controls
119 lines (107 loc) · 5.36 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
document.addEventListener("DOMContentLoaded", () => {
const gridContainer = document.getElementById("module-grid");
const pageTitleElement = document.getElementById("page-title");
fetch("index.json")
.then(response => {
if (!response.ok) {
throw new Error(`HTTP 错误! 状态: ${response.status}`);
}
return response.json();
})
.then(data => {
const modules = data.modules; // 获取 "modules" 数组
const baseUrl = data.base_url || ''; // 获取 base_url
// 使用JSON中的"site"字段更新页面标题
if (data.site) {
document.title = data.site;
pageTitleElement.textContent = data.site;
}
if (!modules || !Array.isArray(modules)) {
throw new Error("JSON数据中未找到有效的 'modules' 数组。");
}
modules.forEach(module => {
const moduleDiv = document.createElement("div");
moduleDiv.className = "module";
const nameH2 = document.createElement("h2");
if (module.homepage) {
const link = document.createElement("a");
link.href = module.homepage;
link.textContent = module.name;
link.target = "_blank";
link.rel = "noopener noreferrer";
nameH2.appendChild(link);
} else {
nameH2.textContent = module.name;
}
const idSpan = document.createElement("span");
idSpan.className = "module-id";
idSpan.textContent = module.id;
nameH2.appendChild(idSpan);
moduleDiv.appendChild(nameH2);
if (module.description) {
const description = document.createElement("p");
description.className = "description";
description.textContent = module.description;
moduleDiv.appendChild(description);
} else {
const placeholder = document.createElement("p");
placeholder.className = "description";
moduleDiv.appendChild(placeholder);
}
const dependenciesDiv = document.createElement("div");
dependenciesDiv.className = "module-dependencies";
const depTypes = [
{ title: "模块依赖", key: "dependencies" },
{ title: "Pip 依赖", key: "pip_dependencies" },
{ title: "Linux 依赖", key: "linux_dependencies" }
];
const hasDependencies = depTypes.some(dep => module[dep.key] && module[dep.key].length > 0);
if (hasDependencies) {
const table = document.createElement("table");
depTypes.forEach(depType => {
const deps = module[depType.key];
if (deps && deps.length > 0) {
const row = table.insertRow();
const cellType = row.insertCell();
cellType.className = "dep-type";
cellType.textContent = depType.title + ":";
const cellList = row.insertCell();
cellList.className = "dep-list-cell";
deps.forEach(dep => {
const tag = document.createElement("span");
tag.className = "dep-tag";
tag.textContent = dep;
cellList.appendChild(tag);
});
}
});
dependenciesDiv.appendChild(table);
}
moduleDiv.appendChild(dependenciesDiv);
const cardFooter = document.createElement("div");
cardFooter.className = "card-footer";
const typeSpan = document.createElement("span");
typeSpan.className = "module-type";
typeSpan.textContent = module.type || "未知类型";
cardFooter.appendChild(typeSpan);
const footerRight = document.createElement("div");
footerRight.className = "footer-right";
const metaSpan = document.createElement("span");
metaSpan.className = "module-meta";
metaSpan.innerHTML = `<span>作者: ${module.author}</span> | <span>版本: ${module.version}</span>`;
footerRight.appendChild(metaSpan);
const downloadBtn = document.createElement("a");
downloadBtn.className = "download-btn";
downloadBtn.href = new URL(module.url, baseUrl).href;
downloadBtn.textContent = "下载";
footerRight.appendChild(downloadBtn);
cardFooter.appendChild(footerRight);
moduleDiv.appendChild(cardFooter);
gridContainer.appendChild(moduleDiv);
});
})
.catch(error => {
console.error("获取或解析模块时出错:", error);
gridContainer.innerHTML = `<p style='color: red;'>${error.message}</p>`;
});
});