/startpage/js/main.js
JavaScript | 195 lines | 170 code | 20 blank | 5 comment | 26 complexity | f40a1b21221581eb41a7759942b5f71f MD5 | raw file
- VERSION = "v1.4.0";
- function version(){
- var msg;
- var responseobj;
- var request = new XMLHttpRequest();
- request.onload = function(){
- responseobj = JSON.parse(this.responseText);
- if(responseobj.tag_name != VERSION && this.status == 200){
- msg = responseobj.tag_name;
- }
- };
- request.open("get",
- "http://api.github.com/repos/fuyuneko/startpage/releases/latest",
- false);
- request.send();
- return msg;
- }
- function fixJitter(container){
- container.style.height = window.innerHeight - 0.5 + "px";
- }
- function popup(obj, msg){
- var popuphandler = function(){
- popup(this, msg);
- }
- // add event listener when it's going to be visible
- if(!visibility){
- obj.addEventListener("click", popuphandler);
- obj.innerHTML = msg;
- obj.style.bottom = "-" + cfg[9];
- }else{
- obj.removeEventListener("click", popuphandler);
- obj.style.bottom = "-200px";
- }
- visibility = !visibility;
- }
- // expanding and contracting squares
- function expand(){
- if(this.acount > 0){
- this.style.height = 300 + 25*this.acount + "px";
- }else{
- this.style.height = "337px";
- }
- if(cfg_bool[0]){
- this.style.borderTop = cfg[9] + " solid " + cfg[8];
- this.style.borderBottom = cfg[9] + " solid " + cfg[8];
- }
- }
- function contract(){
- this.style.height = "150px";
- this.style.borderTop = "0 solid" + cfg[8];
- this.style.borderBottom = "0 solid" + cfg[8];
- }
- String.prototype.replaceChars = function(character, replacement){
- var str = this;
- var a;
- var b;
- for(var i=0; i < str.length; i++){
- if(str.charAt(i) == character){
- a = str.substr(0, i) + replacement;
- b = str.substr(i + 1);
- str = a + b;
- if(replacement == ""){
- i--;
- }
- }
- }
- return str;
- }
- function searchCase(url, query, replacement){
- query = query.substr(3);
- window.location = url + query.replaceChars(" ", replacement);
- }
- function search(query){
- switch(query.substr(0, 2)){
- case "-h":
- popup(popupDiv, HelpText);
- break;
- case "-c":
- configmenuInit(undefined);
- break;
- case "-g":
- searchCase("https://www.google.com/#q=", query, "+");
- break;
- case "-w":
- searchCase("https://en.wikipedia.org/w/index.php?search=", query, "+");
- break;
- case "-a":
- searchCase("https://wiki.archlinux.org/index.php?search=", query, "+");
- break;
- break;
- case "-y":
- searchCase("https://www.youtube.com/results?search_query=", query, "+");
- break;
- case "-d":
- searchCase("https://duckduckgo.com/?q=", query, "+");
- break;
- case "-s":
- searchCase("https://startpage.com/do/metasearch.pl?q=", query, "+");
- break;
- default:
- window.location = "https://startpage.com/do/metasearch.pl?q=" + query.replaceChars(" ", "+");
- }
- }
- window.onresize = function(){
- fixJitter(container);
- }
- function main(){
- if(cfg_bool[3]){
- var ver = version();
- if(ver){
- var versionDiv = document.createElement("div");
- versionDiv.setAttribute("id", "version");
- var versionAnchor = document.createElement("a");
- versionAnchor.href = "https://github.com/fuyuneko/startpage/releases";
- versionAnchor.appendChild(document.createTextNode("A new version is available: " + ver));
- versionDiv.appendChild(versionAnchor);
- document.body.appendChild(versionDiv);
- }
- }
- HelpText = "-h Shows this list<br>-s Startpage (default)<br>-g Google<br>\
- -w Wikipedia<br>-a ArchWiki<br>-y YouTube<br>-d DuckDuckGo";
- visibility = false;
- container = document.getElementById("container");
- fixJitter(container);
- popupDiv = document.getElementById("popup");
- // search
- var searchinput = document.getElementById("searchinput");
- if(!!searchinput){
- searchinput.addEventListener("keypress", function(a){
- var key = a.keyCode;
- if(key == 13){
- var query = this.value;
- search(query);
- }
- });
- }
- // jump to search when tab is pressed
- var search_sqr = document.getElementById("search_sqr");
- document.addEventListener("keypress", function(a){
- var key = a.keyCode;
- if(key == 9){
- search_sqr.style.height = "337px";
- search_sqr.style.borderTop = cfg[9] + " solid " + cfg[8];
- search_sqr.style.borderBottom = cfg[9] + " solid " + cfg[8];
- document.getElementById("searchinput").focus();
- }
- if([9].indexOf(key) > -1){
- a.preventDefault();
- }
- });
- // adding event listeners to squares or expanding them onload
- var sqr = document.querySelectorAll(".sqr");
- if(!cfg_bool[1]){
- for(var i = 0; i < sqr.length; ++i){
- sqr[i].acount = sqr[i].getElementsByTagName("a").length;
- sqr[i].addEventListener("mouseover", expand, false);
- sqr[i].addEventListener("mouseout", contract, false);
- }
- }else{
- for(var i = 0; i < sqr.length; ++i){
- var a = 0;
- for(var x = 0; x < sqr.length; ++x){
- if(a < sqr[x].getElementsByTagName("a").length){
- a = sqr[x].getElementsByTagName("a").length;
- }
- }
- sqr[i].style.height = 225 + 25*a + "px";
- if(cfg_bool[0]){
- sqr[i].style.borderTop = cfg[9] + " solid " + cfg[8];
- sqr[i].style.borderBottom = cfg[9] + " solid " + cfg[8];
- }
- }
- }
- }
- configmenuInit(main);