Mayx's Home Page
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

57 lines
1.9 KiB

  1. ---
  2. layout: default
  3. title: 搜索
  4. ---
  5. <h1>搜索</h1>
  6. <form action="https://www.google.com/search">
  7. <input type="hidden" name="as_sitesearch" value="mabbs.github.io" />
  8. <p><center><input name="q" type="text" id="search-input" placeholder="Search blog posts.." /> <img src="/images/loading.svg" alt="Loading..." id="search-loading" style="width:22px;vertical-align: bottom" /> <input type="submit" value="谷歌搜索" style="margin-left: 5px;" /></center></p>
  9. </form>
  10. <ul id="results-container"></ul>
  11. <script>
  12. function getQueryVariable(variable){
  13. var query = window.location.search.substring(1);
  14. var vars = query.split("&");
  15. for (var i=0;i<vars.length;i++) {
  16. var pair = vars[i].split("=");
  17. if(pair[0] == variable){return pair[1];}
  18. }
  19. return('');
  20. }
  21. var mykeyword = decodeURI(getQueryVariable("keyword"));
  22. var sbox = document.getElementById('search-input');
  23. if (mykeyword) {
  24. sbox.value = mykeyword;
  25. }
  26. </script>
  27. <script src="/assets/js/simple-jekyll-search.min.js"></script>
  28. <script>
  29. function _doSearch(json) {
  30. var sjs = SimpleJekyllSearch({
  31. searchInput: sbox,
  32. resultsContainer: document.getElementById('results-container'),
  33. json: json,
  34. searchResultTemplate: '<li><p>{date} - <a href="{url}?kw={query}">{title}</a></p></li>',
  35. limit: 20
  36. });
  37. sjs.search(mykeyword);
  38. document.getElementById('search-loading').style.display = "none";
  39. }
  40. getSearchJSON(function(json) {
  41. if (typeof SimpleJekyllSearch !== 'undefined') {
  42. _doSearch(json);
  43. } else {
  44. // PJAX 场景:外部脚本通过 pjax 库异步加载,需要等待加载完成
  45. var _poll = 0;
  46. var _waitSJS = setInterval(function() {
  47. if (typeof SimpleJekyllSearch !== 'undefined') {
  48. clearInterval(_waitSJS);
  49. _doSearch(json);
  50. } else if (++_poll > 100) {
  51. clearInterval(_waitSJS);
  52. document.getElementById('search-loading').style.display = "none";
  53. }
  54. }, 50);
  55. }
  56. });
  57. </script>