PageRenderTime 62ms CodeModel.GetById 34ms RepoModel.GetById 1ms app.codeStats 0ms

/startpage/js/main.js

https://gitlab.com/haliax/dotfiles
JavaScript | 195 lines | 170 code | 20 blank | 5 comment | 26 complexity | f40a1b21221581eb41a7759942b5f71f MD5 | raw file
  1. VERSION = "v1.4.0";
  2. function version(){
  3. var msg;
  4. var responseobj;
  5. var request = new XMLHttpRequest();
  6. request.onload = function(){
  7. responseobj = JSON.parse(this.responseText);
  8. if(responseobj.tag_name != VERSION && this.status == 200){
  9. msg = responseobj.tag_name;
  10. }
  11. };
  12. request.open("get",
  13. "http://api.github.com/repos/fuyuneko/startpage/releases/latest",
  14. false);
  15. request.send();
  16. return msg;
  17. }
  18. function fixJitter(container){
  19. container.style.height = window.innerHeight - 0.5 + "px";
  20. }
  21. function popup(obj, msg){
  22. var popuphandler = function(){
  23. popup(this, msg);
  24. }
  25. // add event listener when it's going to be visible
  26. if(!visibility){
  27. obj.addEventListener("click", popuphandler);
  28. obj.innerHTML = msg;
  29. obj.style.bottom = "-" + cfg[9];
  30. }else{
  31. obj.removeEventListener("click", popuphandler);
  32. obj.style.bottom = "-200px";
  33. }
  34. visibility = !visibility;
  35. }
  36. // expanding and contracting squares
  37. function expand(){
  38. if(this.acount > 0){
  39. this.style.height = 300 + 25*this.acount + "px";
  40. }else{
  41. this.style.height = "337px";
  42. }
  43. if(cfg_bool[0]){
  44. this.style.borderTop = cfg[9] + " solid " + cfg[8];
  45. this.style.borderBottom = cfg[9] + " solid " + cfg[8];
  46. }
  47. }
  48. function contract(){
  49. this.style.height = "150px";
  50. this.style.borderTop = "0 solid" + cfg[8];
  51. this.style.borderBottom = "0 solid" + cfg[8];
  52. }
  53. String.prototype.replaceChars = function(character, replacement){
  54. var str = this;
  55. var a;
  56. var b;
  57. for(var i=0; i < str.length; i++){
  58. if(str.charAt(i) == character){
  59. a = str.substr(0, i) + replacement;
  60. b = str.substr(i + 1);
  61. str = a + b;
  62. if(replacement == ""){
  63. i--;
  64. }
  65. }
  66. }
  67. return str;
  68. }
  69. function searchCase(url, query, replacement){
  70. query = query.substr(3);
  71. window.location = url + query.replaceChars(" ", replacement);
  72. }
  73. function search(query){
  74. switch(query.substr(0, 2)){
  75. case "-h":
  76. popup(popupDiv, HelpText);
  77. break;
  78. case "-c":
  79. configmenuInit(undefined);
  80. break;
  81. case "-g":
  82. searchCase("https://www.google.com/#q=", query, "+");
  83. break;
  84. case "-w":
  85. searchCase("https://en.wikipedia.org/w/index.php?search=", query, "+");
  86. break;
  87. case "-a":
  88. searchCase("https://wiki.archlinux.org/index.php?search=", query, "+");
  89. break;
  90. break;
  91. case "-y":
  92. searchCase("https://www.youtube.com/results?search_query=", query, "+");
  93. break;
  94. case "-d":
  95. searchCase("https://duckduckgo.com/?q=", query, "+");
  96. break;
  97. case "-s":
  98. searchCase("https://startpage.com/do/metasearch.pl?q=", query, "+");
  99. break;
  100. default:
  101. window.location = "https://startpage.com/do/metasearch.pl?q=" + query.replaceChars(" ", "+");
  102. }
  103. }
  104. window.onresize = function(){
  105. fixJitter(container);
  106. }
  107. function main(){
  108. if(cfg_bool[3]){
  109. var ver = version();
  110. if(ver){
  111. var versionDiv = document.createElement("div");
  112. versionDiv.setAttribute("id", "version");
  113. var versionAnchor = document.createElement("a");
  114. versionAnchor.href = "https://github.com/fuyuneko/startpage/releases";
  115. versionAnchor.appendChild(document.createTextNode("A new version is available: " + ver));
  116. versionDiv.appendChild(versionAnchor);
  117. document.body.appendChild(versionDiv);
  118. }
  119. }
  120. HelpText = "-h Shows this list<br>-s Startpage (default)<br>-g Google<br>\
  121. -w Wikipedia<br>-a ArchWiki<br>-y YouTube<br>-d DuckDuckGo";
  122. visibility = false;
  123. container = document.getElementById("container");
  124. fixJitter(container);
  125. popupDiv = document.getElementById("popup");
  126. // search
  127. var searchinput = document.getElementById("searchinput");
  128. if(!!searchinput){
  129. searchinput.addEventListener("keypress", function(a){
  130. var key = a.keyCode;
  131. if(key == 13){
  132. var query = this.value;
  133. search(query);
  134. }
  135. });
  136. }
  137. // jump to search when tab is pressed
  138. var search_sqr = document.getElementById("search_sqr");
  139. document.addEventListener("keypress", function(a){
  140. var key = a.keyCode;
  141. if(key == 9){
  142. search_sqr.style.height = "337px";
  143. search_sqr.style.borderTop = cfg[9] + " solid " + cfg[8];
  144. search_sqr.style.borderBottom = cfg[9] + " solid " + cfg[8];
  145. document.getElementById("searchinput").focus();
  146. }
  147. if([9].indexOf(key) > -1){
  148. a.preventDefault();
  149. }
  150. });
  151. // adding event listeners to squares or expanding them onload
  152. var sqr = document.querySelectorAll(".sqr");
  153. if(!cfg_bool[1]){
  154. for(var i = 0; i < sqr.length; ++i){
  155. sqr[i].acount = sqr[i].getElementsByTagName("a").length;
  156. sqr[i].addEventListener("mouseover", expand, false);
  157. sqr[i].addEventListener("mouseout", contract, false);
  158. }
  159. }else{
  160. for(var i = 0; i < sqr.length; ++i){
  161. var a = 0;
  162. for(var x = 0; x < sqr.length; ++x){
  163. if(a < sqr[x].getElementsByTagName("a").length){
  164. a = sqr[x].getElementsByTagName("a").length;
  165. }
  166. }
  167. sqr[i].style.height = 225 + 25*a + "px";
  168. if(cfg_bool[0]){
  169. sqr[i].style.borderTop = cfg[9] + " solid " + cfg[8];
  170. sqr[i].style.borderBottom = cfg[9] + " solid " + cfg[8];
  171. }
  172. }
  173. }
  174. }
  175. configmenuInit(main);