blob: 4b32003dcf3696c344b6aded50b36ea89aaa3602 (
plain)
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
|
/* Any copyright is dedicated to the Public Domain.
* http://creativecommons.org/publicdomain/zero/1.0/ */
/**
* Dynamically create an OpenSearch search engine offering search suggestions
* via searchSuggestions.sjs.
*
* The engine is constructed by passing a JSON object with engine details as the query string.
*/
function handleRequest(request, response) {
let engineData = JSON.parse(unescape(request.queryString).replace("+", " "));
if (!engineData.baseURL) {
response.setStatusLine(request.httpVersion, 500, "baseURL required");
return;
}
engineData.name = engineData.name || "Generated test engine";
engineData.description =
engineData.description || "Generated test engine description";
engineData.method = engineData.method || "GET";
response.setStatusLine(request.httpVersion, 200, "OK");
createOpenSearchEngine(response, engineData);
}
/**
* Create an OpenSearch engine for the given base URL.
*
* @param {Response} response
* The response object to write the engine to.
* @param {object} engineData
* Information about the search engine to write to the response.
*/
function createOpenSearchEngine(response, engineData) {
let params = "";
let queryString = "";
if (engineData.method == "POST") {
params = "<Param name='q' value='{searchTerms}'/>";
} else {
queryString = "?q={searchTerms}";
}
let type = "type='application/x-suggestions+json'";
if (engineData.alternativeJSONType) {
type = "type='application/json' rel='suggestions'";
}
let image = "";
if (engineData.image) {
image = `<Image width="16" height="16">${engineData.baseURL}${engineData.image}</Image>`;
}
let updateFile = "";
if (engineData.updateFile) {
updateFile = `<Url type="application/opensearchdescription+xml"
rel="self"
template="${engineData.baseURL}${engineData.updateFile}" />
`;
}
let result = `<?xml version='1.0' encoding='utf-8'?>
<OpenSearchDescription xmlns='http://a9.com/-/spec/opensearch/1.1/'>
<ShortName>${engineData.name}</ShortName>
<Description>${engineData.description}</Description>
<InputEncoding>UTF-8</InputEncoding>
<LongName>${engineData.name}</LongName>
${image}
<Url ${type} method='${engineData.method}'
template='${engineData.baseURL}searchSuggestions.sjs${queryString}'>
${params}
</Url>
<Url type='text/html' method='${engineData.method}'
template='${engineData.baseURL}${queryString}'/>
${updateFile}
</OpenSearchDescription>
`;
response.write(result);
}
|