这是我对此类功能的实现。它通过 JSONP 使用MediaWiki API,非常灵活。我想一个 jQuery 解决方案很好。我创造了一个小小提琴。
searchWiki(站点,搜索,[回调],[选项])
function searchWiki(site, search, callback, opts) {
if(typeof callback == 'object') {
opts = callback;
callback = null;
} else {
opts = opts || {};
}
// Build the required URLs
var siteUrl = (opts.ssl ? 'https' : 'http') + '://' + site;
var apiUrl = siteUrl + (opts.apiBase || '/w/') + 'api.php';
var queryUrl = apiUrl + '?action=query&list=search&srsearch=' + encodeURIComponent(search) + '&srlimit=' + (opts.maxResults || 1) + '&format=json';
// Issue the JSONP request
$.ajax(queryUrl + '&callback=?', {
dataType: 'jsonp',
// This prevents warnings about the unrecognized parameter "_"
cache: true,
success: function(data) {
// Get all returned pages
var titles = [], links = [];
for(var i = 0; i < data.query.search.length; i++) {
var title = data.query.search[i].title,
link = siteUrl + (opts.wikiBase || '/wiki/') + encodeURIComponent(title);
titles.push(title);
links.push(link);
}
if(!opts.maxResults) {
// Single result requested
if(data.query.search.length == 0) {
titles = links = null;
} else {
titles = titles[0];
links = links[0];
}
}
// Call the callback
(callback || opts.success || function(){})(titles, links);
}
});
}
示例 1:单个维基百科搜索
searchWiki('en.wikipedia.org', 'banana fruit', {
ssl: true,
success: function(title, link) {
// link is now "https://en.wikipedia.org/wiki/Banana"
if(title === null) {
$('#search-msg').text('Not found');
} else {
var anchor = $('<a>').text(title).attr('href', link);
$('#search-msg').append(anchor);
}
}
});
这个例子显示了一个带有相关标题的维基百科页面的链接。
示例 2:多个结果
searchWiki('www.mediawiki.org', 'Release notes', {
ssl: true,
maxResults: 5,
success: function(titles, links) {
for(var i = 0; i < titles.length; i++) {
alert('MediaWiki ' + titles[i] + ' at ' + links[i]);
}
}
});
此示例最多显示五个指向与查询“发布说明”匹配的 MediaWiki 页面的链接。
选项:
ssl
: 使用 HTTPS 而不是 HTTP
maxResults
: 返回多个(最多 n 个)结果
apiBase
: 目标站点上的 API 目录(默认为/w/
)
wikiBase
: 目标站点上的 Wiki 目录(默认为/wiki/
)
success
: 检索结果列表后调用的函数
您可以将回调作为函数参数(在选项之前)或作为success
选项传递。
更新:这是纯 JS 解决方案(不需要 jQuery)。还有另一个小提琴,这次没有 jQuery。
function searchWiki(site, search, callback, opts) {
if(typeof callback == 'object') {
opts = callback;
callback = null;
} else {
opts = opts || {};
}
// Build the required URLs
var siteUrl = (opts.ssl ? 'https' : 'http') + '://' + site;
var apiUrl = siteUrl + (opts.apiBase || '/w/') + 'api.php';
var queryUrl = apiUrl + '?action=query&list=search&srsearch=' + encodeURIComponent(search) + '&srlimit=' + (opts.maxResults || 1) + '&format=json';
var fnName = '_cb_' + Math.floor(Math.random() * 4294967296);
window[fnName] = function(data) {
// Clear references to this function
window[fnName] = null;
// Get all returned pages
var titles = [], links = [];
for(var i = 0; i < data.query.search.length; i++) {
var title = data.query.search[i].title,
link = siteUrl + (opts.wikiBase || '/wiki/') + encodeURIComponent(title);
titles.push(title);
links.push(link);
}
if(!opts.maxResults) {
// Single result requested
if(data.query.search.length == 0) {
titles = links = null;
} else {
titles = titles[0];
links = links[0];
}
}
// Call the callback
(callback || opts.success || function(){})(titles, links);
}
// Issue the JSONP request
var scriptTag = document.createElement('script');
scriptTag.setAttribute('src', queryUrl + '&callback=' + fnName);
document.head.appendChild(scriptTag);
}
更新 2:最后是 node.js 的解决方案。API 仍然是相同的,但它提供了一些额外的选项:
error
: 错误回调(这在基于浏览器的 JS 中是不可能的)
userAgent
:文档中建议的自定义用户代理字符串
port
:目标端口(默认为 80/443)
encoding
: 响应编码(默认为 utf8)
我没有测试这么多,但示例(见上文)应该仍然有效。
var http = require('http'),
https = require('https');
function searchWiki(site, search, callback, opts) {
if(typeof callback == 'object') {
opts = callback;
callback = null;
} else {
opts = opts || {};
}
// Build the required paths
var apiPath = (opts.apiBase || '/w/') + 'api.php';
var queryPath = apiPath + '?action=query&list=search&srsearch=' + encodeURIComponent(search) + '&srlimit=' + (opts.maxResults || 1) + '&format=json';
// Request options
var httpOpts = {
hostname: site,
port: (opts.port ? opts.port : (opts.ssl ? 443 : 80)),
method: 'GET',
path: queryPath,
agent: false
};
// Custom user agent
if(opts.userAgent) {
httpOpts.headers = {
'User-Agent': opts.userAgent
};
}
// Make the request
var req = (opts.ssl ? https : http).request(httpOpts, function(res) {
var msgBody = '';
res.setEncoding(opts.encoding || 'utf8');
res.on('data', function(chunk) {
msgBody += chunk;
});
res.on('end', function() {
// Parse response as JSON
var data;
try {
data = JSON.parse(msgBody);
} catch(err) {
(opts.error || function(){})(err);
return;
}
// Get all returned pages
var siteUrl = (opts.ssl ? 'https' : 'http') + '://' + site;
var titles = [], links = [];
for(var i = 0; i < data.query.search.length; i++) {
var title = data.query.search[i].title,
link = siteUrl + (opts.wikiBase || '/wiki/') + encodeURIComponent(title);
titles.push(title);
links.push(link);
}
if(!opts.maxResults) {
// Single result requested
if(data.query.search.length == 0) {
titles = links = null;
} else {
titles = titles[0];
links = links[0];
}
}
// Call the callback
(callback || opts.success || function(){})(titles, links);
});
});
req.on('error', function(err) {
(opts.error || function(){})(err);
});
req.end();
}