-
Notifications
You must be signed in to change notification settings - Fork 1
/
background.js
68 lines (64 loc) · 2.29 KB
/
background.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
let userEmail;
let firebaseConfig = {
apiKey: "AIzaSyBJh87Tv_yRcYBR1nDZjqOwtem6bYemUsU",
authDomain: "extension-71ea7.firebaseapp.com",
projectId: "extension-71ea7",
storageBucket: "extension-71ea7.appspot.com",
messagingSenderId: "706371350777",
appId: "1:706371350777:web:28ae1e6e9a583fa82eb228",
measurementId: "G-GC87XM6E15"
};
chrome.runtime.onMessage.addListener((msg, sender, sendResponse) => {
if (msg.type === 'Login' || msg.type === 'Register') {
if (!firebase.apps.length) {
firebase.initializeApp(firebaseConfig);
}
let auth = firebase.auth();
let email = msg.email;
let password = msg.password;
if (msg.type === 'Login') {
auth.signInWithEmailAndPassword(email, password).then(cred => {
alert('You are logged in');
chrome.browserAction.setPopup({
popup: "home.html"
})
}).catch(e => { alert('Wrong username or password') });
localStorage.setItem('userEmail', email);
}
else if (msg.type === 'Register') {
auth.createUserWithEmailAndPassword(email, password).then(cred => {
alert('Registered! Login to continue');
})
}
}
else if (msg.type === 'logOut') {
if (!firebase.apps.length) {
firebase.initializeApp(firebaseConfig);
}
let auth = firebase.auth();
auth.signOut().then(alert('You are logged out'));
chrome.browserAction.setPopup({
popup: "popup.html"
})
}
else if (msg.type === 'addProduct') {
let prodUrl = msg.prodUrl;
let price = msg.price;
console.log(prodUrl);
console.log(price);
let route = "http://localhost:3000/products";
$.ajax({
type: "POST",
url: route,
dataType: "json",
data: {
prodUrl: prodUrl,
price: price,
email: localStorage.getItem('userEmail')
},
success: (res) => console.log(res),
error: () => console.log('An error occured')
});
alert('You will be notified when the price drops');
}
});