forked from sureshmurali/IPscanner
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscript.js
More file actions
295 lines (244 loc) · 9.18 KB
/
script.js
File metadata and controls
295 lines (244 loc) · 9.18 KB
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
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
(function () {
/** GLOBAL VARIABLES - STARTS */
/* globals google */
/* globals snazzyMapStyle */
/* globals ClipboardJS */
// Move these to .env files before production
const ipAddressApiUrl = '';
const GoogleMapsApiKey = '';
// Animation controls (in milliseconds)
const initialMapRevealDelay = 2000;
const gridDominoesAnimDelay = 0.075;
const domionesFallRate = 100;
const dominoAnimationRandomness = 100;
const plusAnimationRandomness = 500;
const plusFlickerAnimationDuration = 400;
const initialPlusRevealDelay = 1000;
const mapRevealAnimationDelay = 3500;
const aboutUsRevealDelay = 0;
const badgeRevealDelay = 0;
const copyButtonRevealDelay = 0;
const flashDuration = 500;
const gridCellCount = 200;
const gridCells = [];
const plusSymbolsInGrid = [];
let refreshCopyButtonTimeout = null;
/** GLOBAL VARIABLES - ENDS */
/** GENERIC HELPER FUNCTIONS - STARTS */
const random = (min,max) => Math.floor(Math.random() * (max - min) + min); // random number generator
function eleID (id) { return document.getElementById(id) }
const httpGetAsync = (apiURL, callback) => {
var xmlHttp = new XMLHttpRequest();
xmlHttp.onreadystatechange = function() {
// console.log(xmlHttp);
if (xmlHttp.readyState == 4 && xmlHttp.status == 200) {
if(xmlHttp.response)
callback({response: JSON.parse(xmlHttp.responseText), status: 'success'});
else
callback({status: 'error'});
}
else if(xmlHttp.readyState == 4 && (xmlHttp.status == 0 || xmlHttp.status == 404)){
callback({status: 'error'});
}
}
xmlHttp.open("GET", apiURL, true);
xmlHttp.send(null);
}
/** GENERIC HELPER FUNCTIONS - ENDS */
// Flicker Animation for given DOM element
const flicker = (domElement, duration, delay) => {
duration = duration ? duration: 250;
delay = delay ? delay: 0;
domElement.style.animation = "flicker";
domElement.style.animationDuration = duration+"ms";
domElement.style.animationDelay = delay+"ms";
domElement.style.animationIterationCount = "1";
domElement.style.animationFillMode = "forwards";
}
// setFullScreenHeight for the web app. NO more scrolling.
const setFullScreenHeight = () => {
const { clientHeight } = window.document.documentElement;
eleID("fullScreenContainer").style.height = `${clientHeight}px`;
// console.log(`Client Hieght: ${clientHeight}`);
}
// Render Google map on given DOM element
const initializeMap = (mapDomElementID, Latitude, Longitude, city, country) => {
// For more options see: https://developers.google.com/maps/documentation/javascript/reference#MapOptions
const defaultZoomLevel = 14, indiaMapZoomLevel = 12;
const mapOptions = {
zoom: country == "India" ? indiaMapZoomLevel : defaultZoomLevel,
center: new google.maps.LatLng(Latitude, Longitude),
styles: snazzyMapStyle,
disableDefaultUI: true,
disableDoubleClickZoom: true,
draggable: false,
draggableCursor: false,
draggingCursor: false,
clickableIcons: false,
};
const map = new google.maps.Map(eleID(mapDomElementID), mapOptions);
eleID(mapDomElementID).removeAttribute('tabindex');
google.maps.event.addListenerOnce(map, 'tilesloaded', function(){
//console.log('Map loaded, now reveal map');
revealMap(city, country);
});
}
const displayPlusymbols = () => {
for(let i=0;i<gridCellCount;i++)
{
plusSymbolsInGrid[i].style.animation = "flicker";
plusSymbolsInGrid[i].style.animationDuration = plusFlickerAnimationDuration+"ms";
plusSymbolsInGrid[i].style.animationDelay = initialPlusRevealDelay + random(0,plusAnimationRandomness)+"ms";
plusSymbolsInGrid[i].style.animationFillMode = "forwards";
}
}
const displayAboutUs = () => {
flicker(eleID("AboutUs"), 350, mapRevealAnimationDelay + aboutUsRevealDelay);
}
const displayPHBadge = () => {
flicker(eleID("productHuntBadge"), 350, mapRevealAnimationDelay + aboutUsRevealDelay + badgeRevealDelay);
}
// Display IP address AND copy button
const displayIP = (ipAddress) => {
eleID("ipAddressContainer").style.animationIterationCount = "1";
eleID("ipAddressContainer").style.animationName = "none"; // Fkin iOS
eleID("caption").innerHTML = "YOUR IP ADDRESS";
eleID("ipAddress").innerHTML = ipAddress;
flicker(eleID("caption"), 350, 0);
flicker(eleID("ipAddress"),350, 0);
setTimeout(()=>{flicker(eleID("copyButton", 350, 0))}, copyButtonRevealDelay);
}
// Display Location after rendering map
const displayLocation = (city, country) => {
if(city && country && country === city && country.length <40)
{
eleID("locationContainer").innerHTML = country;
flicker(eleID("locationContainer"),350, mapRevealAnimationDelay);
}
else if(city && country && city.length + country.length <40)
{
eleID("locationContainer").innerHTML = city+ " // "+ country;
flicker(eleID("locationContainer"),350, mapRevealAnimationDelay);
}
else if(!city && country && country.length <40)
{
eleID("locationContainer").innerHTML = country;
flicker(eleID("locationContainer"),350, mapRevealAnimationDelay);
}
else if(city && !country && city.length <40)
{
eleID("locationContainer").innerHTML = city;
flicker(eleID("locationContainer"),350, mapRevealAnimationDelay);
}
displayAboutUs();
displayPHBadge();
}
// Display IP address and Render map location
const renderAPIresult = (ApiResponse) => {
// API response is 200
if(ApiResponse.response && ApiResponse.status === 'success') //&& ApiResponse.geobytesipaddress)
{
const response = ApiResponse.response;
//console.log(response);
const {
ip,
country,
city,
lat,
long
} = response;
// show IP
displayIP(ip);
// Render Google map's location based on the API response
initializeMap(
"map",
parseFloat(lat),
parseFloat(long),
city,
country
);
}
// UI error handler
else{
displayPlusymbols();
eleID("caption").className = "captionError";
eleID("caption").innerHTML = "ERRRRRRROR";
eleID("ipAddressContainer").style.animationDuration = "50ms";
}
}
const drawGrid = (gridContainerDomID) => {
const container = eleID(gridContainerDomID);
for(let i=0;i<gridCellCount;i++)
{
const cell = document.createElement('div');
cell.className += "gridCell";
const plus = document.createElement('span');
plus.innerHTML = "+";
plus.className = "plusSymbol";
plusSymbolsInGrid.push(plus);
cell.appendChild(plus);
container.appendChild(cell);
gridCells.push(cell);
}
}
const revealMap = (city, country) => {
if(country !== "South Korea") // Snazzy map doensn't work for South Korea
{
displayPlusymbols();
let delay = 0.5;
for(let i=0;i<gridCellCount;i++)
{
gridCells[i].style.animation = "black2Transparent";
gridCells[i].style.animationDuration = "100ms";
gridCells[i].style.animationFillMode = "forwards";
gridCells[i].style.animationDelay = (initialMapRevealDelay + random(0,dominoAnimationRandomness) + (domionesFallRate * delay)) + "ms";
delay += gridDominoesAnimDelay;
}
}
// showLocation
displayLocation(city, country);
}
const flashIpAddress = () => {
if(refreshCopyButtonTimeout){
clearTimeout(refreshCopyButtonTimeout);
}
eleID("ipAddress").style.animation = "flash";
eleID("ipAddress").style.animationDuration = flashDuration+ "ms";
eleID("ipAddress").style.animationFillMode = "forwards";
setTimeout(() => { eleID("ipAddress").style.animation = "none"; }, 450);
refreshCopyButtonTimeout = setTimeout(refreshCopyButton, 6000);
}
const refreshCopyButton = () => {
eleID("copyButton").innerHTML = "COPY";
eleID("copyButton").style.backgroundColor = "transparent";
eleID("copyButton").style.color = "#FFF";
}
// INITIAL TRIGGER ON LOAD
const triggerOnLoad = () => {
// 1. Set Full screen height for the parent container - No scrolling allowed
setFullScreenHeight();
// 2. Get IP address and location from API
httpGetAsync(ipAddressApiUrl, renderAPIresult);
// 3. Draw grid
drawGrid("gridOverlayContainer");
}
/** EVENT LISTENERS - STARTS */
window.onload = triggerOnLoad;
window.onresize = function(){ location.reload(); }
eleID("copyButton").onclick = flashIpAddress;
const clipboard = new ClipboardJS('#copyButton');
clipboard.on('success', function(e) {
// console.info('Action:', e.action);
// console.info('Text:', e.text);
// console.info('Trigger:', e.trigger);
eleID("copyButton").innerHTML = "COPIED";
eleID("copyButton").style.backgroundColor = "white";
eleID("copyButton").style.color = "#333333"
e.clearSelection();
});
clipboard.on('error', function(e) {
// console.error('Action:', e.action);
// console.error('Trigger:', e.trigger);
});
/** EVENT LISTENERS - ENDS */
})();