Commit 87ea5e1d authored by Clara Stoddart's avatar Clara Stoddart

Writing ajax request

parent 52aa5267
hello this is ajax
// Unique ID for the className. // Unique ID for the className.
var MOUSE_VISITED_CLASSNAME = 'crx_mouse_visited'; var MOUSE_VISITED_CLASSNAME = 'crx_mouse_visited_true';
var MOUSE_VISITED_CLASSNAME_FALSE = 'crx_mouse_visited_false';
var srcName; var srcName;
var dietNum = 0; var dietNum = 0;
var requestResult;
// var currName; // var currName;
//Querying database on heroku //Querying database on heroku
...@@ -16,6 +18,49 @@ var dietNum = 0; ...@@ -16,6 +18,49 @@ var dietNum = 0;
// } // }
// }); // });
//https://www.w3schools.com/nodejs/nodejs_mysql_select.asp //https://www.w3schools.com/nodejs/nodejs_mysql_select.asp
function callBack(canEat) {
if (canEat == undefined) {
requestResult = true;
} else if (canEat == "0") {
requestResult = true;
} else if (canEat == "1") {
requestResult = true;
} else if (canEat == "Could not connect") {
result = true;
} else if (canEat == 1) {
requestResult = true;
} else if (canEat == '1') {
requestResult = true;
}
}
function checkAllergen(product) {
request(product);
return requestResult;
}
function checkAllergen(product) {
var xhr = new XMLHttpRequest();
var result;
xhr.onreadystatechange = function() {
if (this.readyState == 4 && this.status == 200) {
var canEat = this.responseText;
if (canEat == undefined) {
result = false;
} else if (canEat == "0") {
result = false;
} else if (canEat == "1") {
result = true;
} else if (canEat == "Could not connect") {
result = false;
}
}
};
xhr.open("GET", "script.php?q="+product, true);
xhr.send();
return result;
}
// Previous dom, that we want to track, so we can remove the previous styling. // Previous dom, that we want to track, so we can remove the previous styling.
...@@ -27,7 +72,7 @@ document.addEventListener('mousemove', function (e) { ...@@ -27,7 +72,7 @@ document.addEventListener('mousemove', function (e) {
// Lets check if our underlying element contains any allergens. // Lets check if our underlying element contains any allergens.
if (srcElement.nodeName == 'IMG') { if (srcElement.nodeName == 'IMG') {
//if (srcElement.nodeName == 'DIV' && srcElement.className == "content___1O9rr") { //if (srcElement.nodeName == 'DIV' && srcElement.className == "content___1O9rr") {
//prevDOM != srcElement && //prevDOM != srcElement &&
srcName = srcElement.alt; srcName = srcElement.alt;
// For NPE checking, we check safely. We need to remove the class name // For NPE checking, we check safely. We need to remove the class name
// Since we will be styling the new one after. // Since we will be styling the new one after.
...@@ -40,8 +85,13 @@ document.addEventListener('mousemove', function (e) { ...@@ -40,8 +85,13 @@ document.addEventListener('mousemove', function (e) {
// srcElement.classList.add(MOUSE_VISITED_CLASSNAME); // srcElement.classList.add(MOUSE_VISITED_CLASSNAME);
// } // }
// currName = srcElement.srcName; // currName = srcElement.srcName;
srcElement.classList.add(MOUSE_VISITED_CLASSNAME); //changes outline of pic if (checkAllergen(srcName)) {
//TODO: modify behaviour according to different allergens srcElement.classList.add(MOUSE_VISITED_CLASSNAME); //changes outline of pic
}
if (!checkAllergen(srcName)) {
srcElement.classList.add(MOUSE_VISITED_CLASSNAME_FALSE);
}
//TODO: modify behaviour according to different allergens
// The current element is now the previous. So we can remove the class // The current element is now the previous. So we can remove the class
// during the next ieration. // during the next ieration.
//prevDOM = srcElement; //prevDOM = srcElement;
...@@ -65,7 +115,7 @@ function myFunction() { ...@@ -65,7 +115,7 @@ function myFunction() {
dietNum = a + (b << 1) + (c << 2) + (d << 3); dietNum = a + (b << 1) + (c << 2) + (d << 3);
} }
// function checkAllergens() { // function checkAllergens() {
// client.connect(); // client.connect();
// //TODO: find a way to connect to client at startup (le??) to remove the need to connect and disconnect repeatedly // //TODO: find a way to connect to client at startup (le??) to remove the need to connect and disconnect repeatedly
// //TODO: fill in names of columns of db // //TODO: fill in names of columns of db
...@@ -75,11 +125,10 @@ function myFunction() { ...@@ -75,11 +125,10 @@ function myFunction() {
// if (col) { // if (col) {
// return col; // return col;
// } // }
// //returns true if any allergen is present. // //returns true if any allergen is present.
// //TODO: modify behaviour according to different allergens // //TODO: modify behaviour according to different allergens
// } // }
// client.end(); // client.end();
// return false; // return false;
// }); // });
// } // }
...@@ -3,7 +3,7 @@ $product = $_GET['q']; ...@@ -3,7 +3,7 @@ $product = $_GET['q'];
$con = mysqli_connect('localhost','root','','public'); $con = mysqli_connect('localhost','root','','public');
if (!$con) { if (!$con) {
echo "Could not connect"; echo "Could not connect";
die('Could not connect: ' . mysqli_error($con)); //die('Could not connect: ' . mysqli_error($con));
} }
// $sql = "SELECT containsgluten FROM productsingredients WHERE product = $product"; // $sql = "SELECT containsgluten FROM productsingredients WHERE product = $product";
$sql = "SELECT containsgluten FROM productsingredients WHERE product = Waitrose Delicious & Zingy Lemon Loaf Cake"; $sql = "SELECT containsgluten FROM productsingredients WHERE product = Waitrose Delicious & Zingy Lemon Loaf Cake";
......
...@@ -3,7 +3,7 @@ ...@@ -3,7 +3,7 @@
"description": "Makes shopping with food requirements easier", "description": "Makes shopping with food requirements easier",
"version": "1.0", "version": "1.0",
"manifest_version" : 2, "manifest_version" : 2,
"permissions": [ "permissions": [
"activeTab", "activeTab",
"declarativeContent" "declarativeContent"
...@@ -31,6 +31,8 @@ ...@@ -31,6 +31,8 @@
"matches": ["https://www.waitrose.com/ecom/shop/browse/groceries/*"] "matches": ["https://www.waitrose.com/ecom/shop/browse/groceries/*"]
}] }]
} }
...@@ -11,7 +11,12 @@ ...@@ -11,7 +11,12 @@
background-color: #ffffff; background-color: #ffffff;
} }
.crx_mouse_visited { .crx_mouse_visited_true {
background-clip: #bcd5eb!important; background-clip: #bcd5eb!important;
outline: 1px dashed #47bb47!important; outline: 1px dashed #47bb47!important;
} }
\ No newline at end of file
.crx_mouse_visited_false {
background-clip: #bcd5eb!important;
outline: 1px dashed #fa1100!important;
}
<?xml version="1.0" encoding="UTF-8"?>
<module type="JAVA_MODULE" version="4">
<component name="NewModuleRootManager" inherit-compiler-output="true">
<exclude-output />
<content url="file://$MODULE_DIR$">
<sourceFolder url="file://$MODULE_DIR$/src" isTestSource="false" />
</content>
<orderEntry type="inheritedJdk" />
<orderEntry type="sourceFolder" forTests="false" />
</component>
</module>
\ No newline at end of file
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment