-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpopup.js
157 lines (122 loc) · 4.75 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
var apiUrl = 'https://research-pal-2.uc.r.appspot.com/notes'
// 'https://research-pal-2.uc.r.appspot.com/notes' not working
// 'http://research-pal-2.uc.r.appspot.com/notes' not working
// 'http://research-pal.appspot.com/notes' not working (golang 1.9 is depricated)
// 'http://localhost:8080/notes' working
function getCurrentTabUrl(callback) { //Question: what does callback hear mean?
// Query filter to be passed to chrome.tabs.query - see
// https://developer.chrome.com/extensions/tabs#method-query
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'); //Question: does console.assert() going to work in chrome extensions?
callback(url);
});
}
function dosubmit(){
var notes = document.getElementById('notes').value;
var id = document.getElementById('notesid').value; //TODO: need a better way to store/retrieve the ID instead of in the html field
getCurrentTabUrl(function(url) {
if (id === undefined){
postNotesData(encodeURIComponent(url),encodeURIComponent(notes), function(errorMessage) {
renderStatus("create failed!!!", errorMessage)
});
}else{
updateNotesData(encodeURIComponent(url),encodeURIComponent(notes),id, function(errorMessage) {
renderStatus("save failed!!!", errorMessage)
});
}
});
}
function postNotesData(url, notes, errorCallback){
var postUrl = apiUrl;
var x = new XMLHttpRequest();
x.open('POST', postUrl);
// x.setRequestHeader( 'Access-Control-Allow-Origin', '*');
x.responseType = 'json';
x.onload = function() { // Parse and process the response
var response = x.response;
if (!response) {
errorCallback('No response from POST API!');
return;
}
// var firstResult = response.Notes;
// document.getElementById('notes').value = firstResult;
};
x.onerror = function() {
errorCallback('Network error.');
};
var body = '[{"url":"'+url+'","notes":"'+notes+'", "status":"new"}]';
x.send(body);
renderStatus("create successfully"); // TODO: need to check for the status /response of send to determine if the update is sucessful or not
}
function updateNotesData(url, notes, id, errorCallback){
var patchUrl = apiUrl+"/"+id
var x = new XMLHttpRequest();
x.open('PATCH', patchUrl);
// x.setRequestHeader( 'Access-Control-Allow-Origin', '*');
x.responseType = 'json';
x.onload = function() { // Parse and process the response
var response = x.response;
if (!response) {
errorCallback('No response from PATCH API!');
return;
}
// var firstResult = response.Notes;
// document.getElementById('notes').value = firstResult;
};
x.onerror = function() {
errorCallback('Network error.');
};
var body = '{"notes":"'+notes+'"}';
x.send(body);
renderStatus("updated successfully"); // TODO: need to check for the status /response of send to determine if the update is sucessful or not
}
function getNotesData(searchTerm, errorCallback) {
var searchUrl = apiUrl + '?url='+searchTerm;
var x = new XMLHttpRequest();
x.open('GET', searchUrl);
// x.setRequestHeader( 'Access-Control-Allow-Origin', '*');
//x.setRequestHeader( 'Content-Type', 'application/json' );
x.responseType = 'json';
x.onload = function() { // Parse and process the response
var response = x.response;
// // enable this when debugging an issue
// if (!response) {
// errorCallback('No response from GET API!');
// //document.getElementById('postButton').value="Create"
// return;
// }
// if (response.length ===0){
// errorCallback('Zero response from API!');
// return;
// }
document.getElementById('notes').value = decodeURIComponent(response[0].notes);
document.getElementById('notesid').value = response[0].id;
};
x.onerror = function() {
errorCallback('Network error.');
};
x.send();
}
function renderStatus(statusText) {
document.getElementById('status').textContent = statusText;
setTimeout(function(){ document.getElementById('status').textContent = ""; }, 60000);
}
document.addEventListener('DOMContentLoaded', function() { //Question: what is the significance of this pattern of calling function in nested manner in javascript?
getCurrentTabUrl(function(url) {
//renderStatus(url);
//renderStatus(encodeURIComponent(url));
getNotesData(encodeURIComponent(url), function(errorMessage) {
renderStatus(errorMessage);
});
});
var postButton = document.getElementById('postButton');
postButton.addEventListener('click', function() {
dosubmit();
}, false); //Question: what is this false doing here?
});