-
Notifications
You must be signed in to change notification settings - Fork 2
/
popup.js
172 lines (156 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
// Copyright (c) 2014 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
let settings = {};
const DEFAULT_SETTINGS = {
domain: 'http://localhost:8080',
};
function showMessage(success = true) {
if (success) {
$('#msg-success').removeClass('hidden');
setTimeout(() => {
$('#msg-success').addClass('hidden');
}, 3000);
return;
}
$('#msg-error').removeClass('hidden');
setTimeout(() => {
$('#msg-error').addClass('hidden');
}, 3000);
}
function loadToServer(result) {
const POST_DATA_URL = '/api/rent-comparables/upload/extension';
$.ajax({
type: "POST",
url: settings.domain + POST_DATA_URL,
data: result,
})
.done((data, status) => {
console.log("Status", status);
if (data.error) {
console.log('Error occured, mesage:');
console.log(data.message);
showMessage(false);
} else {
console.log('Data was saved, saved item:');
console.log(data.saved);
showMessage(true);
}
})
.fail((jqXHR, status, errorThrown) => {
console.error('Error:', status);
console.error('Thrown:', errorThrown);
showMessage(false);
});
}
function generateAmenities(amenities) {
const amenitiesTable = $('#amenities');
for (let i = 0; i < amenities.length; i++) {
amenitiesTable.append(`
<li>${amenities[i]}</li>`);
}
}
function generatePhotos(photos) {
const $photos = $('#photos');
for (let i = 0; i < photos.length; i++) {
$photos.append(`
<li><img src="${photos[i]}"/></li>`);
}
}
function fillForm(data) {
$('#btn-get-data').addClass('hidden');
$('#edit-data').removeClass('hidden');
Object.keys(data).forEach((key) => {
if (key === 'amenities') {
generateAmenities(data[key]);
} else if (key === 'photos') {
generatePhotos(data[key]);
} else if (key === 'coords') {
$(`#${key}`).text(`Lat: ${data.coords.latitude}; Lng: ${data.coords.longitude}.`);
} else {
const item = data[key];
$(`#${key}`).val(item);
}
});
$('#btn-send-data').on('click', (e) => {
e.preventDefault();
const editedData = Object.assign({}, data, {
listingAgentNames: $('#agent').val(),
unit: $('#unit').val(),
address: $('#address').val(),
borough: $('#borough').val(),
zip: Number($('#zip').val()),
neighborhood: $('#neighborhood').val(),
unitType: $('#unit-type').val(),
rent: Number($('#rent').val()),
sqft: Number($('#sqft').val()),
rooms: Number($('#rooms').val()),
bedrooms: Number($('#bedrooms').val()),
bathrooms: Number($('#bathrooms').val()),
});
console.log(editedData);
loadToServer(editedData);
$('#btn-get-data').removeClass('hidden');
$('#edit-data').addClass('hidden');
});
}
function executeContentScript() {
chrome.extension.onRequest.addListener(function (parsedData) {
fillForm(parsedData);
});
chrome.tabs.executeScript(null, {
file: 'jquery.min.js',
}, () => {
chrome.tabs.executeScript(null, {
file: 'parse-data.js',
});
});
}
function loginApplication() {
const LOGIN_URL = '/#!/login-extension';
const loginPageWidth = 470;
const loginPageHeight = 624;
const loginPageType = 'popup';
let loginPageTop = (window.screen.height - loginPageHeight) / 2;
let loginPageLeft = (window.screen.width - loginPageWidth) / 2;
loginPageTop = (!isNaN(loginPageTop) && (loginPageTop > 0)) ? parseInt(loginPageTop) : 0;
loginPageLeft = (!isNaN(loginPageLeft) && (loginPageLeft > 0)) ? parseInt(loginPageLeft) : 0;
let openedWindowId = 0;
chrome.windows.create({
url: [settings.domain + LOGIN_URL],
type: loginPageType,
width: loginPageWidth,
height: loginPageHeight,
left: loginPageLeft,
top: loginPageTop
}, function (createdWindow) {
if (createdWindow) {
console.log('Opened window with id', createdWindow.id);
openedWindowId = createdWindow.id;
chrome.windows.onRemoved.addListener(function (removedId) {
if (removedId === openedWindowId) {
openedWindowId = 0;
}
});
}
});
}
document.addEventListener('DOMContentLoaded', () => {
const btnGetData = document.getElementById('btn-get-data');
btnGetData.addEventListener('click', () => {
executeContentScript();
});
const btnLogin = document.getElementById('btn-login');
btnLogin.addEventListener('click', () => {
loginApplication();
});
loadSettings();
});
function loadSettings() {
// Use default values blank strings
chrome.storage.sync.get({
domain: DEFAULT_SETTINGS.domain,
}, function (items) {
settings = items;
});
}