Commit 04a60d19 authored by Zhai, Zirun's avatar Zhai, Zirun

Merge branch 'develop' into 'master'

Develop

See merge request !1
parents a7c74e2c 6ab6fb22
web: node index.js
\ No newline at end of file
// handles the click event for link 1, sends the query
function getOutput() {
getRequest(
'database.php', // URL for the PHP file
drawOutput, // handle successful request
drawError // handle error
);
return false;
}
// handles drawing an error message
function drawError() {
var container = document.getElementById('output');
container.innerHTML = 'There was an error!';
}
// handles the response, adds the html
function drawOutput(responseText) {
var container = document.getElementById('output');
container.innerHTML = responseText;
}
// helper function for cross-browser request object
function getRequest(url, success, error) {
var req = false;
try{
// most browsers
req = new XMLHttpRequest();
} catch (e){
// IE
try{
req = new ActiveXObject("Msxml2.XMLHTTP");
} catch(e) {
// try an older version
try{
req = new ActiveXObject("Microsoft.XMLHTTP");
} catch(e) {
return false;
}
}
}
if (!req) return false;
if (typeof success != 'function') success = function () {};
if (typeof error!= 'function') error = function () {};
req.onreadystatechange = function(){
if(req.readyState == 4) {
return req.status === 200 ?
success(req.responseText) : error(req.status);
}
}
req.open("GET", url, true);
req.send(null);
return req;
}
hello this is ajax
// 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 dietNum = 0;
var requestResult;
// var currName;
const { Client } = require('pg');
//Querying database on heroku
const client = new Client({
connectionString: process.env.DATABASE_URL, //default name for main database is DATABASE_URL
ssl: {
rejectUnauthorized: false
// import { Client } from 'pg';
// const client = new Client({
// connectionString: process.env.DATABASE_URL, //default name for main database is DATABASE_URL
// ssl: {
// rejectUnauthorized: false
// }
// });
//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;
}
});
// Previous dom, that we want to track, so we can remove the previous styling.
//var prevDOM = null;
function checkAllergens(name) {
client.connect();
client.query('SELECT Containsgluten, Containsdairy, Containscorn, Containsmeat FROM productsIngredients WHERE Product = ' + name + ';', (err, res) => {
if (err) throw err;
for (let col of res.cols) {
if (col) {
return col;
}
//returns true if any allergen is present.
}
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;
}
return false;
});
client.end();
// return true;
}
};
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.
//var prevDOM = null;
// Mouse listener for any move event on the current document.
document.addEventListener('mousemove', function (e) {
let srcElement = e.srcElement;
// Lets check if our underlying element contains any allergens.
if (srcElement.nodeName == 'IMG') {
//if (srcElement.nodeName == 'DIV' && srcElement.className == "content___1O9rr") {
//prevDOM != srcElement &&
//prevDOM != srcElement &&
srcName = srcElement.alt;
// For NPE checking, we check safely. We need to remove the class name
// Since we will be styling the new one after.
// if (prevDOM != null) {
......@@ -46,11 +84,14 @@ document.addEventListener('mousemove', function (e) {
// if (srcName == "Waitrose coffee & walnut cake") {
// srcElement.classList.add(MOUSE_VISITED_CLASSNAME);
// }
if (checkAllergens(srcElement.alt)) {
// currName = srcElement.srcName;
if (checkAllergen(srcName)) {
srcElement.classList.add(MOUSE_VISITED_CLASSNAME); //changes outline of pic
//TODO: modify behaviour according to different allergens
}
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
// during the next ieration.
//prevDOM = srcElement;
......@@ -74,5 +115,20 @@ function myFunction() {
dietNum = a + (b << 1) + (c << 2) + (d << 3);
}
// function checkAllergens() {
// client.connect();
// //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
// client.query('SELECT Containsgluten, Containsdairy, Containscorn, Containsmeat FROM productsIngredients WHERE Product = currName;', (err, res) => {
// if (err) throw err;
// for (let col of res.cols) {
// if (col) {
// return col;
// }
// //returns true if any allergen is present.
// //TODO: modify behaviour according to different allergens
// }
// client.end();
// return false;
// });
// }
<?php
$product = $_GET['q'];
$con = mysqli_connect('localhost','root','','public');
if (!$con) {
echo "Could not connect";
//die('Could not connect: ' . mysqli_error($con));
}
// $sql = "SELECT containsgluten FROM productsingredients WHERE product = $product";
$sql = "SELECT containsgluten FROM productsingredients WHERE product = Waitrose Delicious & Zingy Lemon Loaf Cake";
$result = mysqli_query($con, $sql);
$row = mysqli_fetch_array($result);
$edible = $row['containsgluten'];
// $row = mysqli_fetch_array($result, 'MYSQLI_ASSOC');
// $edible = $row['containsgluten'];
if ($edible == 1) {
echo "1";
} else {
echo "0";
}
mysqli_close($con);
?>
File added
......@@ -3,7 +3,12 @@
"description": "Makes shopping with food requirements easier",
"version": "1.0",
"manifest_version" : 2,
"permissions": [
"activeTab",
"declarativeContent"
],
"permissions": [
"activeTab",
"declarativeContent"
......@@ -26,6 +31,8 @@
"matches": ["https://www.waitrose.com/ecom/shop/browse/groceries/*"]
}]
}
......@@ -4,21 +4,16 @@
<meta charset = "utf-8"/>
<title> clickerance </title>
<link type="text/css" rel="stylesheet" href="style.css"/>
<script src="require.js" type="text/javascript"></script>
<script src="index.js" type="text/javascript"></script>
<script src="content.js" type="text/javascript"></script>
<script src="content.js"></script>
</head>
<body>
<div class="Title">
<b>Clickerance</b>
</div>
<div id="form">
<div class="Title"></div>
<h1> Clickerance </h1>
Dairy : <input type="checkbox" id="Dairy"><br>
Meat : <input type="checkbox" id="Meat"><br>
Corn : <input type="checkbox" id="Corn"><br>
Gluten : <input type="checkbox" id="Gluten"><br>
</div>
<p>Which ones you can't eat?</p>
......
This diff is collapsed.
.Title
{
text-align: justify;
font: 30px helvetica, sans-serif;
font: 40px helvetica, sans-serif;
color: white;
background-color: #47BD95;
padding: 4px
padding: 28px
}
.nameWrapper___4c05X {
background-color: #ffffff;
}
.crx_mouse_visited {
.crx_mouse_visited_true {
background-clip: #bcd5eb!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"?>
<project version="4">
<component name="masterDetails">
<states>
<state key="ProjectJDKs.UI">
<settings>
<last-edited>11</last-edited>
<splitter-proportions>
<option name="proportions">
<list>
<option value="0.2" />
</list>
</option>
</splitter-proportions>
</settings>
</state>
</states>
</component>
</project>
\ No newline at end of file
# Default ignored files
/workspace.xml
\ No newline at end of file
<component name="ProjectCodeStyleConfiguration">
<code_scheme name="Project" version="173">
<DBN-PSQL>
<case-options enabled="true">
<option name="KEYWORD_CASE" value="lower" />
<option name="FUNCTION_CASE" value="lower" />
<option name="PARAMETER_CASE" value="lower" />
<option name="DATATYPE_CASE" value="lower" />
<option name="OBJECT_CASE" value="preserve" />
</case-options>
<formatting-settings enabled="false" />
</DBN-PSQL>
<DBN-SQL>
<case-options enabled="true">
<option name="KEYWORD_CASE" value="lower" />
<option name="FUNCTION_CASE" value="lower" />
<option name="PARAMETER_CASE" value="lower" />
<option name="DATATYPE_CASE" value="lower" />
<option name="OBJECT_CASE" value="preserve" />
</case-options>
<formatting-settings enabled="false">
<option name="STATEMENT_SPACING" value="one_line" />
<option name="CLAUSE_CHOP_DOWN" value="chop_down_if_statement_long" />
<option name="ITERATION_ELEMENTS_WRAPPING" value="chop_down_if_not_single" />
</formatting-settings>
</DBN-SQL>
</code_scheme>
</component>
\ No newline at end of file
This diff is collapsed.
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="GoogleJavaFormatSettings">
<option name="enabled" value="false" />
</component>
</project>
\ No newline at end of file
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="ProjectRootManager" version="2" languageLevel="JDK_11" default="true" project-jdk-name="11" project-jdk-type="JavaSDK">
<output url="file://$PROJECT_DIR$/out" />
</component>
</project>
\ No newline at end of file
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="ProjectModuleManager">
<modules>
<module fileurl="file://$PROJECT_DIR$/untitled.iml" filepath="$PROJECT_DIR$/untitled.iml" />
</modules>
</component>
</project>
\ No newline at end of file
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="VcsDirectoryMappings">
<mapping directory="$PROJECT_DIR$/.." vcs="Git" />
</component>
</project>
\ No newline at end of file
This diff is collapsed.
<?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