-
Notifications
You must be signed in to change notification settings - Fork 0
/
popup.js
182 lines (148 loc) · 4.66 KB
/
popup.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
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
console.log("test");
/**
* Obtain GCM Registration Token
*/
function registerCallback(registrationId) {
if (chrome.runtime.lastError) {
// When the registration fails, handle the error and retry the
// registration later.
return;
}
// Send the registration token to your application server.
sendRegistrationId(function(succeed) {
// Once the registration token is received by your server,
// set the flag such that register will not be invoked
// next time when the app starts up.
if (succeed)
chrome.storage.local.set({registered: true});
});
}
function sendRegistrationId(callback) {
// Send the registration token to your application server
// in a secure way.
}
chrome.runtime.onStartup.addListener(function() {
chrome.storage.local.get("registered", function(result) {
// If already registered, bail out.
if (result["registered"])
return;
// Up to 100 senders are allowed.
var senderIds = ["Your-Sender-ID"];
chrome.gcm.register(senderIds, registerCallback);
});
});
/**
************************************************************************
*
** ***************** UPSTREAM: CLIENT to DB 33639986032************************
*
*
*************************************************************************
*/
//This is the project # from the Google Developers Console.
var senderId = "33639986032";
// Make the message ID unique across the lifetime of your app.
// One way to achieve this is to use the auto-increment counter
// that is persisted to local storage.
// Message ID is saved to and restored from local storage.
var messageId = 0;
chrome.storage.local.get("messageId", function(result) {
if (chrome.runtime.lastError)
return;
messageId = parseInt(result["messageId"]);
if (isNaN(messageId))
messageId = 0;
});
// Sets up an event listener for send error.
chrome.gcm.onSendError.addListener(sendError);
// Returns a new ID to identify the message.
function getMessageId() {
messageId++;
chrome.storage.local.set({messageId: messageId});
return messageId.toString();
}
//sends a Message to server
//Inv: url is a string of url browser is currently on or fetching
function sendMessage(url) {
var message = {
messageId: getMessageId(),
destinationId: senderId + "@gcm.googleapis.com",
timeToLive: 86400, // 1 day
data: {
"userid": "value1"
}
};
chrome.gcm.send(message, function(messageId) {
if (chrome.runtime.lastError) {
// Some error occurred. Fail gracefully or try to send
// again.
return;
}
// The message has been accepted for delivery. If the message
// can not reach the destination, onSendError event will be
// fired.
});
}
function sendError(error) {
console.log("Message " + error.messageId +
" failed to be sent: " + error.errorMessage);
}
/**
* Get the current URL.
*
* @param {function(string)} callback - called when the URL of the current tab
* is found.
*/
function getCurrentTabUrl(callback) {
var queryInfo = {
active: true,
currentWindow: true
};
chrome.tabs.query(queryInfo, function(tabs) {
var tab = tabs[0];
var url = tab.url;
console.assert(typeof url == 'string', 'tab.url should be a string');
callback(url);
});
}
/**
* Gets the list of websites recommended for the user from database
*/
function getRecList() {
var data = databaseCall(); // TODO
var p = JSON.parse(data);
}
function renderStatus(statusText) {
document.getElementById('status').textContent = statusText;
}
function generateListEntry(listData, i) {
var listData
}
document.addEventListener('DOMContentLoaded', function() {
var MongoClient = require('mongodb').MongoClient;
MongoClient.connect("mongodb://localhost:27017/exampleDb", function(err, db) {
if(!err) {
console.log("We are connected");
}
});
var collection = db.collection('Users');
getCurrentTabUrl(function(url) {
// Put the image URL in Google search.
renderStatus('Adding info to database for ' + url);
var listData = collection.findOne({email: getEmail()}, {recommendationList: 1});
var collection = db.collection('test');
var reclist = document.getElementById('recList');
for(var i = 0; i < 5; i++) {
var listEntry = document.createElement("list-group-item");
listEntry.innerHTML = generateListEntry(listData, i);
}
var imageResult = document.getElementById('image-result');
imageResult.width = width;
imageResult.height = height;
imageResult.src = imageUrl;
imageResult.hidden = false;
}, function(errorMessage) {
renderStatus('Cannot display image. ' + errorMessage);
});
});
});