forked from turbowarp-cn/cn-extensions
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSearch_extensions.html
91 lines (88 loc) · 4.47 KB
/
Search_extensions.html
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
<!DOCTYPE html>
<html lang="zh-hans">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<link rel="stylesheet" href="./assets/css/Search_extensions_style.css" />
<script type="text/javascript" src="assets/js/extensionList.js"></script>
<title>搜索扩展</title>
<script type="module">
var texts = {
"noResults": "无结果",
"noInput": "输入积木关键词来搜索哦~"
}
const searchResultsContainer = document.querySelector('#search-results');
searchResultsContainer.textContent = texts.noInput; // 若尚未搜索,则显示文本
import scratchblocks from "./assets/js/scratchblocks.min.es.js";
import loadTranslations from "./assets/js/translations-all-es.js";
loadTranslations(scratchblocks);
// 监听搜索框中的输入事件
const searchInput = document.querySelector('#search-input');
searchInput.addEventListener('input', () => {
// 获取搜索条件并转换为小写
const searchTerm = searchInput.value.trim().toLowerCase();
// 过滤数据并呈现结果
if (searchInput.value) { //判定输入框内是否有内容
const searchResults = extensionsList.filter(item => {
// 将扩展名称、中括号小括号内的内容以及后面带的参数删除
const regex = /(\[.*?\]|\(.*?\)|::.*?$|^\w+?:)/g;
const filteredItem = item.replace(regex, '');
// 将积木名称和功能描述合并为一个字符串
const combinedString = filteredItem.toLowerCase().replace(/\s+/g, '');
return combinedString.includes(searchTerm);
});
searchResultsContainer.innerHTML = '';
if (searchResults.length === 0) {
searchResultsContainer.textContent = texts.noResults;
} else {
const extensions = {};
searchResults.forEach(item => {
const extensionName = item.match(/^(.*?)\:/)[1].trim().toLowerCase();
const extensionResult = item.replace(/^(.*?)\:/, '').trim(); // 删除扩展名
if (!extensions[extensionName]) {
extensions[extensionName] = [];
}
extensions[extensionName].push(extensionResult);
});
Object.keys(extensions).forEach(extensionName => {
const extensionResults = extensions[extensionName];
const extensionContainer = document.createElement('div');
extensionContainer.classList.add('extension-container');
const extensionTitle = document.createElement('h2');
extensionTitle.textContent = extensionName.replace(/\b\w/g, c => c.toUpperCase()); // 自动大小写
extensionContainer.appendChild(extensionTitle);
extensionResults.forEach(result => {
const preElement = document.createElement('pre');
preElement.classList.add('blocks');
preElement.textContent = result;
extensionContainer.appendChild(preElement);
});
searchResultsContainer.appendChild(extensionContainer);
});
scratchblocks.renderMatching('pre.blocks', { style: 'scratch3', languages: ['en', 'zh_cn'], scale: 1 });
}
}
else {
searchResultsContainer.innerHTML = texts.noInput;//此时仍判定为尚未搜索,显示结果为空。
}
});
// 获取 URL 参数
const urlsearch = new URLSearchParams(window.location.search);
const search = urlsearch.get('search');
if (search) {
// 将参数填入搜索框
searchInput.value = search;
// 模拟输入事件
const event = new Event('input');
searchInput.dispatchEvent(event);
}
</script>
</head>
<body>
<!-- JavaScript -->
<div class="background">
<input type="text" id="search-input" placeholder="在此输入以搜索扩展...">
<div id="search-results"></div>
</div>
</body>
</html>