-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathscript.js
224 lines (204 loc) · 6.82 KB
/
script.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
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
let map;
let service;
let infowindow;
let loc;
let coordinates;
let markers = [];
const popup = document.getElementById("popup");
const locationInput = document.getElementById("locationInput");
const productInput = document.getElementById("productInput");
const rangeInput = document.getElementById("rangeInput")
async function initMap() {
infowindow = new google.maps.InfoWindow();
map = new google.maps.Map(document.getElementById("map"), {
center: new google.maps.LatLng(50,0),
zoom: 3.5,
});
service = new google.maps.places.PlacesService(map);
loc = await waitForLocation();
popup.style.display = "none";
map.setCenter(loc);
map.setZoom(11)
setTimeout(async()=>{
try{
var search = await getTabKeywords();
productInput.value = search;
findNearby(loc, productInput.value, rangeInput.value*1000);
}catch(err){
errorPopup(err);
}
},500);
document.getElementById("selectLocation").addEventListener('click', async ()=>{
loc = await selectLocation();
popup.style.display = "none";
map.setCenter(loc);
map.setZoom(11)
});
document.getElementById("productForm").addEventListener('submit', (e) => {
var value = productInput.value;
e.preventDefault();
if(value != ""){
findNearby(loc, value, rangeInput.value*1000);
}
});
}
function selectLocation(){
return new Promise(async (res,rej) =>{
popup.style.display = "flex";
document.getElementById("locationForm").addEventListener('submit', async function func(e) {
var value = locationInput.value;
locationInput.value = "";
e.preventDefault();
res(await getLocationFromAddress(value));
document.getElementById("locationForm").removeEventListener('submit',func);
});
});
}
function waitForLocation(){
return new Promise(async (res,rej) =>{
try{
var c = await getLocation();
res(new google.maps.LatLng(c.coords.latitude,c.coords.longitude));
}catch(err){
errorPopup(err);
res(await selectLocation())
}
});
}
function getLocation() {
var param = {
timeout: 3 * 1000
}
return new Promise((res, rej) => {
if (navigator.geolocation) {
navigator.geolocation.getCurrentPosition(
(coords)=>{
res(coords);
},
(err) =>{
rej("Location access denied")
},param);
} else {
rej("Geolocation not supported")
}
});
}
function getLocationFromAddress(address) {
var param = {
query: address,
fields: ['name', 'geometry']
};
return new Promise((res, rej) => {
service.findPlaceFromQuery(param, (results, status) => {
if (status === google.maps.places.PlacesServiceStatus.OK) {
res(results[0].geometry.location)
} else {
rej("Could not find location")
errorPopup("Could not find location");
}
})
})
}
function findNearby(coords, searchParam, range){
markers.forEach(el => {
el.setMap(null);
});
markers = [];
var param = {
location: coords,
radius: range,
keyword: searchParam
};
service.nearbySearch(param, (results, status) => {
if (status == google.maps.places.PlacesServiceStatus.OK) {
for (var i = 0; i < results.length; i++) {
createMarker(results[i]);
}
console.log(results);
}else{
errorPopup("No results found");
}
});
}
function createMarker(place) {
if (!place.geometry || !place.geometry.location) return;
const marker = new google.maps.Marker({position: place.geometry.location, map});
markers.push(marker);
google.maps.event.addListener(marker, "click", () => {
infowindow.setContent(
"<div><strong>" +
place.name +
"</strong><br>" +
place.vicinity +
"<br><div id='" + place.place_id + "' style='color: #427fed'>" +
"View on Google Maps</div></div>"
);
infowindow.open(map,marker);
document.addEventListener('click',function(e){
if(e.target && e.target.id == place.place_id){
var newURL = "https://www.google.com/maps/place/?q=place_id:" + place.place_id;
try{
chrome.tabs.create({ url: newURL , active: false});
}catch(err){
window.open(newURL, '_blank');
}
}
});
document.addEventListener("mouseover", function(e) {
if(e.target && e.target.id == place.place_id){
e.target.style.textDecoration = "underline";
}
});
document.addEventListener("mouseout", function(e) {
if(e.target && e.target.id == place.place_id){
e.target.style.textDecoration = "none";
}
});
});
}
function errorPopup(text){
var err = document.getElementById("error");
err.innerHTML = text;
err.style.display = "flex";
setTimeout(()=>{
err.classList.remove("animate__fadeInLeft")
err.classList.add("animate__fadeOutLeft")
setTimeout(()=>{
err.style.display = "none";
err.classList.add("animate__fadeInLeft")
err.classList.remove("animate__fadeOutLeft")
},1000)
},3000)
}
function getTabKeywords(){
return new Promise((res,rej) =>{
chrome.runtime.onMessage.addListener(function(request, sender) {
if (request.action == "getSource") {
if(request.source == ""){
rej("No previous search found.");
}else{
res(request.source);
}
}
});
chrome.tabs.query({ active: true, currentWindow: true }, tabs => {
if(tabs[0].url == undefined){
rej("No previous search found.");
}
else if(tabs[0].url.includes("amazon")){
chrome.tabs.executeScript(
tabs[0].id,
{ code: 'var search = document.getElementById("twotabsearchtextbox").value; chrome.runtime.sendMessage({action: "getSource", source: search});' }
);
}
else if(tabs[0].url.includes("ebay")){
chrome.tabs.executeScript(
tabs[0].id,
{ code: 'var search = document.getElementById("gh-ac").value; chrome.runtime.sendMessage({action: "getSource", source: search});' }
);
}else{
rej("No previous search found.");
}
});
});
}