diff --git a/src/content.js b/src/content.js index 643bcc3..bf40f3b 100644 --- a/src/content.js +++ b/src/content.js @@ -1,19 +1,17 @@ - function modify_query() { + var words = query.split(' '); + query = words.join('+'); var keyword_list = query.split('+'); - var new_keyword_list = []; var allKeys=["buy","price","cheap", "product", "shop", "sale","amazon","flipkart","myntra","purchase","ecological"]; - for(var i=0;i
"; qnum=1; httpGet(url); } - function search_2() { nop = 8; @@ -45,17 +42,15 @@ function search_2() allLinks = []; combined = 0; document.getElementById("web").innerHTML = ""; - query = document.getElementById("inp").value; - url = 'https://www.wudbox.in/?s=' + query + '&post_type=product'; - modify_query(); + url = 'https://www.wudbox.in/?s=' + query + '&post_type=product'; + document.getElementById("text").innerHTML = "

" + good_query + " alternatives on Wudbox:" + "


"; qnum=2; httpGet(url); } - function search_3() { nop = 8; @@ -66,15 +61,14 @@ function search_3() combined = 0; document.getElementById("web").innerHTML = ""; query = document.getElementById("inp").value; - url ='http://www.saveglobe.in/shop/?s=' + query; - modify_query(); + url ='http://www.saveglobe.in/shop/?s=' + query; + document.getElementById("text").innerHTML = "

" + good_query + " alternatives on SaveGlobe:" + "


"; qnum=3; httpGet(url); } - function search_4() { nop = 8; @@ -87,10 +81,9 @@ function search_4() combined = 0; document.getElementById("web").innerHTML = ""; query = document.getElementById("inp").value; + modify_query(); url ='https://geosmin.in/?s=' + query; - keyword_list = query.split('+'); - good_query = keyword_list.join(' '); document.getElementById("text").innerHTML = "

" + good_query + " alternatives on Geosmin:" + "


"; qnum=4; httpGet(url); @@ -108,9 +101,9 @@ function search_all() combined_products_counter = 0; document.getElementById("web").innerHTML = ""; query = document.getElementById("inp").value; - url ='https://www.ecohoy.com/catalogsearch/result/?q=' + query; - modify_query(); + url ='https://www.ecohoy.com/catalogsearch/result/?q=' + query; + document.getElementById("text").innerHTML = "

Eco-friendly alternatives for " + good_query + ":


"; qnum=1; httpGet(url); @@ -121,7 +114,6 @@ function search_all() function httpGet(theUrl) { - xmlHttp = new XMLHttpRequest(); xmlHttp.open( "GET", theUrl, true ); xmlHttp.send( null ); @@ -129,15 +121,12 @@ function httpGet(theUrl) if(qnum==1){ xmlHttp.onreadystatechange = ProcessRequest_Ecohoy; } - else if(qnum==2){ xmlHttp.onreadystatechange = ProcessRequest_Wudbox; } - else if(qnum==3){ xmlHttp.onreadystatechange = ProcessRequest_SG; } - else if(qnum==4){ xmlHttp.onreadystatechange = ProcessRequest_Geosmin; } @@ -150,34 +139,27 @@ function CreateLink(url) { var a = document.createElement('a'); // Create the text node for anchor element. - var link = document.createTextNode(url + "\n\n"); // Append the text node to anchor element. - - a.appendChild(link); + a.appendChild(link); // Set the title. - a.title = url; // Set the href property. - a.href = url; a.target = "_blank"; // Append the anchor element to the body. - document.getElementById("text").appendChild(a); - document.getElementById("text").innerHTML += "

"; } + //--------------------------------------------------------------- //ProcessRequests------------------------------------------------------ function ProcessRequest_Ecohoy() { - if ( xmlHttp.readyState == 4 && xmlHttp.status == 200 ) { - var container = document.createElement("p"); container.innerHTML = xmlHttp.responseText; var anchors = container.getElementsByTagName("a"); @@ -204,7 +186,6 @@ function ProcessRequest_Ecohoy() if (exists == 0) { allLinks.push(href); - if(href.substring(23, 50) == "catalogsearch/result/index/") { counter++; @@ -214,58 +195,48 @@ function ProcessRequest_Ecohoy() if(counter>0) { if(product_counter%2==0 && actual_product_counter"; } - else { document.getElementById("text").innerHTML += actual_product_counter + "." + "
"; } - var title = "Name: " + anchors[i].title + "
"; - document.getElementById("text").innerHTML += title; var price = "Price: " + allPrices[actual_product_counter-1].textContent + "
"; - document.getElementById("text").innerHTML += price; - var imgObj = anchors[i].getElementsByTagName('img')[0]; var img = document.createElement('img'); img.src = imgObj.getAttribute('data-src'); img.height = 100; img.width = 100; - document.getElementById("text").appendChild(img); CreateLink(href); + } - product_counter++; } } } } - if (combined==1) { allPrices = []; allNames = []; query = document.getElementById("inp").value; url = 'https://www.wudbox.in/?s=' + query + '&post_type=product'; - qnum=2; httpGet(url); } - else { end_search("Ecohoy", 'https://www.ecohoy.com/catalogsearch/result/?q=' + query); @@ -310,9 +281,9 @@ function ProcessRequest_Wudbox() if (exists == 0) { allLinks.push(href); - if(href.substring(21, 30)=="/product/") { + if(actual_product_counter"; } @@ -330,27 +300,22 @@ function ProcessRequest_Wudbox() document.getElementById("text").innerHTML += actual_product_counter + "." + "
"; } var name = "Name: " + allNames[actual_product_counter-1].textContent + "
"; - document.getElementById("text").innerHTML += name; var price_elements = allPrices[actual_product_counter-1].textContent.split(' '); - if (price_elements.length==2) { allPrices[actual_product_counter-1].textContent = ""+price_elements[0]+" " + price_elements[1]; } var price = "Price: " + allPrices[actual_product_counter-1].textContent + "
"; - document.getElementById("text").innerHTML += price; var img = document.createElement('img'); img.src = imgObj.getAttribute('data-lazy-src'); img.height = 100; img.width = 100; - document.getElementById("text").appendChild(img); - CreateLink(href); - + } } @@ -359,7 +324,6 @@ function ProcessRequest_Wudbox() } } - if (combined==1) { allPrices = []; allNames = []; @@ -367,11 +331,9 @@ function ProcessRequest_Wudbox() combined_products_counter_sg = combined_products_counter; query = document.getElementById("inp").value; url ='http://www.saveglobe.in/shop/?s=' + query; - qnum=3; httpGet(url); } - else { end_search("Wudbox", 'https://www.wudbox.in/?s=' + query + '&post_type=product'); @@ -400,7 +362,6 @@ function ProcessRequest_SG() if (exists == 0) { allLinks.push(href); - if(href.substring(0, 37) == "http://www.saveglobe.in/shop/product/") { if(actual_product_counter"; } @@ -516,12 +473,9 @@ function go_to_price_SG(produrl,image) { document.getElementById("text").innerHTML += (runnedsg+1) + "." + "
"; } - var name = "Name: " + allNames[runnedsg] + "
"; - document.getElementById("text").innerHTML += name; var price = "Price: " + allPrices[runnedsg] + "
"; - document.getElementById("text").innerHTML += price; runnedsg++; document.getElementById("text").appendChild(image); @@ -529,7 +483,6 @@ function go_to_price_SG(produrl,image) } } } - function go_to_price_GM(produrl,image) { var xmls=null; @@ -545,9 +498,9 @@ function go_to_price_GM(produrl,image) container.innerHTML = xmls.responseText; var para = container.getElementsByClassName("woocommerce-Price-amount amount"); var name = container.getElementsByClassName("entry-title"); - allPrices_Geosmin.push(para.item(0).textContent); allNames_Geosmin.push(name.item(0).textContent); + if (combined==1) { document.getElementById("text").innerHTML += (combined_products_counter_gm+runnedgm+1) + "." + "
"; } @@ -556,9 +509,7 @@ function go_to_price_GM(produrl,image) document.getElementById("text").innerHTML += (runnedgm+1) + "." + "
"; } var name = "Name: " + allNames_Geosmin[runnedgm] + "
"; - document.getElementById("text").innerHTML += name; - if (para.length>=2 && para.item(0).textContent!=para.item(1).textContent) { var price = "Price: " + para.item(0).textContent + " " + para.item(1).textContent + "
"; } @@ -566,7 +517,6 @@ function go_to_price_GM(produrl,image) { var price = "Price: " + para.item(0).textContent + "
"; } - document.getElementById("text").innerHTML += price; runnedgm++; document.getElementById("text").appendChild(image); @@ -578,7 +528,6 @@ function go_to_price_GM(produrl,image) function end_search(title, href) { - // document.getElementById("text").innerHTML += "

Open website to see all products:

"; var a = document.createElement('a'); // Create the text node for anchor element. @@ -601,7 +550,6 @@ function end_search(title, href) } - //------------------------------------------------------------------ //maincode---------------------------------------------------------- //Event Listeners @@ -611,7 +559,6 @@ document.getElementById("s3").addEventListener("click", search_3); document.getElementById("s4").addEventListener("click", search_4); document.getElementById("sn").addEventListener("click", search_all); document.getElementById("inp").addEventListener("change", search_all); - //Window and vars window.resizeBy(1200,900); var query; @@ -636,7 +583,6 @@ chrome.tabs.query({active: true, lastFocusedWindow: true}, tabs => { document.getElementById("inp").value = query; search_all(); }); - //---------------------------------------------------------------------