搜索:修订间差异
更多操作
Testtesttesttest(留言 | 贡献) 创建页面,内容为“== 站内搜索 == <div style="max-width: 600px; margin: 2rem auto; padding: 1rem; border: 1px solid #eee; border-radius: 8px;"> <form action="{{SERVER}}{{SCRIPTPATH}}/index.php" method="get"> <input type="hidden" name="title" value="特殊:搜索" /> <div style="display: flex; gap: 8px;"> <input type="text" name="search" placeholder="输入关键词搜索本站..." style="flex: 1; padding: 8px; border: 1px solid #ddd; border-radius: 4px;" /> <butto…” |
Testtesttesttest(留言 | 贡献) |
||
| 第1行: | 第1行: | ||
<nowiki> | |||
<div style="max-width: | <!-- 自定义全库条件搜索框 --> | ||
< | <div id="custom-wiki-search" style="max-width: 800px; margin: 2rem auto; padding: 1rem; border: 1px solid #ddd; border-radius: 8px;"> | ||
< | <h3>维基全站条件搜索</h3> | ||
<form id="wiki-search-form" onsubmit="return doWikiSearch()"> | |||
<div style="margin: 1rem 0;"> | |||
<button type="submit" style="padding: | <label>搜索关键词:</label> | ||
<input type="text" id="search-keyword" placeholder="输入要匹配的内容..." required style="width: 70%; padding: 0.5rem; margin-left: 0.5rem;"> | |||
</div> | |||
<div style="margin: 1rem 0;"> | |||
<label>限定命名空间:</label> | |||
<select id="search-namespace" style="padding: 0.5rem; margin-left: 0.5rem;"> | |||
<option value="0">主命名空间(文章)</option> | |||
<option value="14">分类</option> | |||
<option value="10">模板</option> | |||
<option value="-1">所有命名空间</option> | |||
</select> | |||
</div> | |||
<button type="submit" style="padding: 0.5rem 2rem; background: # 0645ad; color: white; border: none; border-radius: 4px; cursor: pointer;">开始搜索</button> | |||
</form> | |||
<!-- 搜索结果展示区 --> | |||
<div id="search-results" style="margin-top: 2rem;"> | |||
<ul id="results-list" style="list-style: none; padding: 0;"></ul> | |||
</div> | </div> | ||
</div> | </div> | ||
<script> | |||
function doWikiSearch() { | |||
// 获取搜索参数 | |||
const keyword = document.getElementById('search-keyword').value.trim(); | |||
const namespace = document.getElementById('search-namespace').value; | |||
const resultsList = document.getElementById('results-list'); | |||
resultsList.innerHTML = '<li>正在搜索中,请稍候...</li>'; | |||
// 调用MediaWiki原生API | |||
const apiUrl = mw.config.get('wgScriptPath') + '/api.php'; | |||
const params = new URLSearchParams({ | |||
action: 'query', | |||
list: 'search', | |||
srsearch: keyword, | |||
srnamespace: namespace, | |||
srlimit: 50, // 最多返回50条结果,可自行修改 | |||
format: 'json' | |||
}); | |||
fetch(`${apiUrl}?${params.toString()}`) | |||
.then(response => response.json()) | |||
.then(data => { | |||
resultsList.innerHTML = ''; | |||
const results = data.query.search; | |||
if (results.length === 0) { | |||
resultsList.innerHTML = '<li>未找到符合条件的页面</li>'; | |||
return; | |||
} | |||
// 渲染搜索结果 | |||
results.forEach(page => { | |||
const li = document.createElement('li'); | |||
li.style = 'margin: 0.8rem 0; padding-bottom: 0.8rem; border-bottom: 1px solid #eee;'; | |||
li.innerHTML = ` | |||
<a href="${mw.config.get('wgArticlePath').replace('$1', encodeURIComponent(page.title))}" target="_blank"> | |||
<strong>${page.title}</strong> | |||
</a> | |||
<p style="margin: 0.3rem 0 0 0; color: # 0666; font-size: 0.9em;">${page.snippet}...</p> | |||
`; | |||
resultsList.appendChild(li); | |||
}); | |||
}) | |||
.catch(error => { | |||
resultsList.innerHTML = `<li>搜索出错:${error.message}</li>`; | |||
}); | |||
return false; // 阻止表单默认跳转 | |||
} | |||
</script> | |||
</nowiki> | |||
2026年5月30日 (六) 18:42的最新版本
<!-- 自定义全库条件搜索框 --> <div id="custom-wiki-search" style="max-width: 800px; margin: 2rem auto; padding: 1rem; border: 1px solid #ddd; border-radius: 8px;"> <h3>维基全站条件搜索</h3> <form id="wiki-search-form" onsubmit="return doWikiSearch()"> <div style="margin: 1rem 0;"> <label>搜索关键词:</label> <input type="text" id="search-keyword" placeholder="输入要匹配的内容..." required style="width: 70%; padding: 0.5rem; margin-left: 0.5rem;"> </div> <div style="margin: 1rem 0;"> <label>限定命名空间:</label> <select id="search-namespace" style="padding: 0.5rem; margin-left: 0.5rem;"> <option value="0">主命名空间(文章)</option> <option value="14">分类</option> <option value="10">模板</option> <option value="-1">所有命名空间</option> </select> </div> <button type="submit" style="padding: 0.5rem 2rem; background: # 0645ad; color: white; border: none; border-radius: 4px; cursor: pointer;">开始搜索</button> </form> <!-- 搜索结果展示区 --> <div id="search-results" style="margin-top: 2rem;"> <ul id="results-list" style="list-style: none; padding: 0;"></ul> </div> </div> <script> function doWikiSearch() { // 获取搜索参数 const keyword = document.getElementById('search-keyword').value.trim(); const namespace = document.getElementById('search-namespace').value; const resultsList = document.getElementById('results-list'); resultsList.innerHTML = '<li>正在搜索中,请稍候...</li>'; // 调用MediaWiki原生API const apiUrl = mw.config.get('wgScriptPath') + '/api.php'; const params = new URLSearchParams({ action: 'query', list: 'search', srsearch: keyword, srnamespace: namespace, srlimit: 50, // 最多返回50条结果,可自行修改 format: 'json' }); fetch(`${apiUrl}?${params.toString()}`) .then(response => response.json()) .then(data => { resultsList.innerHTML = ''; const results = data.query.search; if (results.length === 0) { resultsList.innerHTML = '<li>未找到符合条件的页面</li>'; return; } // 渲染搜索结果 results.forEach(page => { const li = document.createElement('li'); li.style = 'margin: 0.8rem 0; padding-bottom: 0.8rem; border-bottom: 1px solid #eee;'; li.innerHTML = ` <a href="${mw.config.get('wgArticlePath').replace('$1', encodeURIComponent(page.title))}" target="_blank"> <strong>${page.title}</strong> </a> <p style="margin: 0.3rem 0 0 0; color: # 0666; font-size: 0.9em;">${page.snippet}...</p> `; resultsList.appendChild(li); }); }) .catch(error => { resultsList.innerHTML = `<li>搜索出错:${error.message}</li>`; }); return false; // 阻止表单默认跳转 } </script>