-
-
Notifications
You must be signed in to change notification settings - Fork 10
/
Copy pathblogger-quick-search.js
85 lines (79 loc) · 3.73 KB
/
blogger-quick-search.js
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
/**
* Blogger Quick Search Result JSON
* Author: Taufik Nurrohman
* URL: https://plus.google.com/108949996304093815163/about
* See: http://www.dte.web.id/2012/09/membangun-aplikasi-quick-search-dengan.html
*/
// Just a shortcut for document.getElementById();
function getId(id) {
return document.getElementById(id);
}
var config = searchFormConfig,
input = getId('feed-q-input'),
resultContainer = getId('search-result-container'),
resultLoader = getId('search-result-loader'),
skeleton = '';
// The Most Basic :: JSON caller function to display the list of posts in the container
function showResult(json) {
var entry = json.feed.entry ? json.feed.entry : "", url, summary, img;
skeleton = '<h4>' + config.resultTitle + ' "' + input.value + '"</h4>';
skeleton += '<a title="Close" style="display:block;position:absolute;top:10px;right:12px;line-height:normal;text-decoration:none;color:inherit;font-size:150%;" href="#close" onclick="resultContainer.style.display=\'none\';return false;">×</a><ol>';
if (entry === "") {
skeleton += '<li>' + config.noResult + '</li>';
}
for (var i = 0; i < config.numPost; i++) {
if (i == entry.length) break;
var mark = new RegExp(input.value, "ig"), entries = entry[i], title = entries.title.$t.replace(mark, "<mark>"+input.value+"</mark>");
for (var j = 0; j < entries.link.length; j++) {
if (entries.link[j].rel == 'alternate') {
url = entries.link[j].href;
break;
}
}
summary = ("summary" in entries && config.summaryPost === true) ? entries.summary.$t : "";
if (config.resultThumbnail === true) {
img = ("media$thumbnail" in entries) ? entries.media$thumbnail.url.replace(/\/s[0-9]+\-c/g, "/s"+config.thumbSize+"-c") : config.fallbackThumb;
}
summary = summary.replace(/<br ?\/?>/ig, " ").replace(/<.*?>/g, "").replace(/[<>]/g, "");
if (summary.length > config.summaryLength) {
summary = summary.substring(0, config.summaryLength) + '...';
}
summary = summary.replace(mark, "<mark>"+input.value+"</mark>");
skeleton += '<li><img style="width:'+config.thumbSize+'px;height:'+config.thumbSize+'px;" src="'+img+'" alt="" /><a href="'+url+'" target="_blank">'+title+'</a>'+summary+'</li>';
}
skeleton += '</ol>';
resultContainer.innerHTML = skeleton;
resultLoader.style.display = "none";
resultContainer.style.display = "block";
}
// Insert an empty <script> tag with ID of 'search-feed-script'
(function() {
var s = document.createElement('script');
s.type = "text/javascript";
s.id = "search-feed-script";
document.getElementsByTagName('head')[0].appendChild(s);
})();
// Used to manipulate the 'q' parameter value in the 'search-feed-script' based on keywords that written in the search input
function updateScript() {
resultContainer.style.display = "none";
resultLoader.style.display = "block";
var script = getId('search-feed-script'),
newScript = document.createElement('script'),
val = input.value;
newScript.id = "search-feed-script";
newScript.type = "text/javascript";
newScript.src = config.url+"/feeds/posts/summary?alt=json-in-script&q="+val+"&max-results="+config.numPost+"&callback=showResult";
// Remove the empty script that we crated before...
script.parentNode.removeChild(script);
// Then, insert a new script with the callback of showResult() fuunction based on the 'q' parameter value of input.value
// So, the result will be like this => http://blog_name.blogspot.com/feeds/posts/summary?alt=json-in-script&q=QUERIES&max-results=XXXX&callback=showResult
document.getElementsByTagName('head')[0].appendChild(newScript);
return false;
}
// Used to hide the search result container when the search input value is empty
function resetField() {
if (input.value === "") {
resultContainer.style.display = "none";
resultLoader.style.display = "none";
}
}