-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfirebase.js
72 lines (61 loc) · 1.73 KB
/
firebase.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
69
70
71
72
// Your web app's Firebase configuration
var firebaseConfig = {
apiKey: "xxxxx",
authDomain: "firestore-example-xxxx.firebaseapp.com",
databaseURL: "https://firestore-example-xxx.firebaseio.com",
projectId: "firestore-example-xxx",
storageBucket: "firestore-example-xxx.appspot.com",
messagingSenderId: "xxx",
appId: "1:xxx:web:xxx"
};
// Initialize Firebase
firebase.initializeApp(firebaseConfig);
console.log(firebase);
var db = firebase.firestore();
/*
var db = firebase.firestore();
db.collection("cities").doc("LA").set({
name: "Los Angeles",
state: "CA",
country: "USA"
})
.then(function() {
console.log("Document successfully written!");
})
.catch(function(error) {
console.error("Error writing document: ", error);
});
*/
chrome.runtime.onMessage.addListener((msg, sender, resp) => {
if(msg.command == "post"){
db.collection("cities").doc("test-doc").set({
data: msg.data
})
.then(function() {
console.log("Document successfully written!");
})
.catch(function(error) {
console.error("Error writing document: ", error);
});
}
if(msg.command == "fetch"){
var docRef = db.collection("cities").doc("LA");
docRef.get().then(function(doc) {
if (doc.exists) {
//doc.data()
resp({type: "result", status: "success", data: doc.data(), request: msg});
} else {
//No such document!
resp({type: "result", status: "error", data: 'No such document!', request: msg});
}
}).catch(function(error) {
//Error getting document:",error
resp({type: "result", status: "error", data: error, request: msg});
});
}
//submit data..
if(msg.command == "post"){
//...
}
return true;
})