search.js 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  1. var sindex = 0;
  2. var cycle = false;
  3. function start() {
  4. var query = getParameterByName('q');
  5. if (query) search(query.replaceAll("+", "%2B"));
  6. document.getElementById('keywords').focus();
  7. window.setInterval(function () {
  8. updatetime();
  9. }, 200);
  10. }
  11. function handleKeyPress(e) {
  12. var key = e.keyCode || e.which;
  13. var text = document.getElementById("keywords").value.replaceAll("+", "%2B");
  14. var option = text.substr(1, text.indexOf(' ') - 1) || text.substr(1);
  15. var subtext = text.substr(2 + option.length);
  16. if (key == 13) { // Search functions
  17. search(text);
  18. }
  19. if (key == 9) { // Tab Completion Functions
  20. e.preventDefault();
  21. e.stopPropagation();
  22. if (text[0] === ';') {
  23. switch (option) {
  24. case 't':
  25. var streamers = ['admiralbahroo', 'moonmoon_ow', 'witwix'];
  26. if (!subtext || cycle) {
  27. cycle = true;
  28. if (sindex > streamers.length - 1) sindex = 0;
  29. document.getElementById("keywords").value = ';t ' + streamers[sindex++];
  30. return;
  31. }
  32. for (var streamer of streamers) {
  33. if (subtext === streamer.substr(0, subtext.length)) {
  34. document.getElementById("keywords").value = ';t ' + streamer;
  35. return;
  36. }
  37. }
  38. break;
  39. }
  40. }
  41. }
  42. if(key == 32){ //Space to go to search
  43. document.getElementById("keywords").focus();
  44. }
  45. sindex = 0;
  46. cycle = false;
  47. }
  48. function search(text) {
  49. var option = text.substr(1, text.indexOf(' ') - 1) || text.substr(1);
  50. var subtext = text.substr(2 + option.length);
  51. if (text[0] === '/') {
  52. if (text.indexOf(' ') > -1) {
  53. switch (option) {
  54. case "am":
  55. window.location = "https://www.allmusic.com/search/all/" + subtext;
  56. break;
  57. case "d":
  58. window.location = "https://duckduckgo.com/?q=" + subtext;
  59. break;
  60. case "di":
  61. window.location = "https://www.discogs.com/search/?q=" + subtext;
  62. break;
  63. case "i":
  64. window.location = "https://www.imdb.com/find?q=" + subtext;
  65. break;
  66. case "m":
  67. window.location = "https://www.themoviedb.org/search?query=" + subtext;
  68. break;
  69. case "r":
  70. window.location = "https://www.reddit.com/search?q=" + subtext;
  71. break;
  72. case "q":
  73. window.location = "https://www.qwant.com/?q=" + subtext;
  74. break;
  75. case "so":
  76. window.location = "https://soundcloud.com/search?q=" + subtext;
  77. break;
  78. case "s":
  79. window.location = "https://open.spotify.com/search/results/" + subtext;
  80. break;
  81. case "t":
  82. window.location = "https://trakt.tv/search?query=" + subtext;
  83. break;
  84. case "tv":
  85. window.location = "https://www.thetvdb.com/search?query=" + subtext;
  86. break;
  87. case "y":
  88. window.location = "https://www.youtube.com/results?search_query=" + subtext;
  89. break;
  90. }
  91. } else {
  92. var option = text.substr(1);
  93. switch (option) {
  94. case "d":
  95. window.location = "https://www.dukduckgo.com";
  96. break;
  97. case "y":
  98. window.location = "https://www.youtube.com";
  99. break;
  100. case "r":
  101. window.location = "https://reddit.com";
  102. break;
  103. case "s":
  104. window.location = "https://open.spotify.com";
  105. break;
  106. }
  107. }
  108. } else if (validURL(text)) {
  109. if (containsProtocol(text))
  110. window.location = text;
  111. else
  112. window.location = "https://" + text;
  113. } else {
  114. window.location = "https://www.google.com/search?q=" + text;
  115. }
  116. }
  117. // Source: https://stackoverflow.com/questions/5717093/check-if-a-javascript-string-is-a-url
  118. function validURL(str) {
  119. var pattern = new RegExp('^(https?:\\/\\/)?' + // protocol
  120. '((([a-z\\d]([a-z\\d-]*[a-z\\d])*)\\.)+[a-z]{2,}|' + // domain name
  121. '((\\d{1,3}\\.){3}\\d{1,3}))' + // OR ip (v4) address
  122. '(\\:\\d+)?(\\/[-a-z\\d%_.~+]*)*' + // port and path
  123. '(\\?[;&a-z\\d%_.~+=-]*)?' + // query string
  124. '(\\#[-a-z\\d_]*)?$', 'i'); // fragment locator
  125. return !!pattern.test(str);
  126. }
  127. function containsProtocol(str) {
  128. var pattern = new RegExp('^(https?:\\/\\/){1}.*', 'i');
  129. return !!pattern.test(str);
  130. }
  131. String.prototype.replaceAll = function(search, replacement) {
  132. var target = this;
  133. return target.split(search).join(replacement);
  134. };