PageSourceSearch

https://www.fesmes.com/modules/ltw_usergeolocation/views/js/geolocation.js?v=1790967184

js fesmes.com collected 2026-10-02 18:53:48 UTC 6,094 bytes, 183 lines download raw bytes

1/**
2 * JavaScript file to obtain user geolocation only once per session
3 */
4document.addEventListener('DOMContentLoaded', function() {
5	// First check if we already have geolocation data in session
6	checkExistingGeolocation();
7	/**
8	 * Check if geolocation data already exists in session
9	 */
10	function checkExistingGeolocation() {
11		var xhr = new XMLHttpRequest();
12		xhr.open('POST', geolocajax_url, true);
13		xhr.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
14		xhr.onreadystatechange = function() {
15			if (xhr.readyState === 4 && xhr.status === 200) {
16				try {
17					var response = JSON.parse(xhr.responseText);
18					// If we don't have geolocation data in session, try to get it
19					if (!response.exists) {
20						initGeolocation();
21					} else {
22						var storeName = document.querySelector('.js-store-name');
23						var storeHours = document.querySelector('.js-store-hours');
24						if (storeName) {
25							storeName.innerHTML = response.default_store.default_store_name;
26						}
27						if(storeHours) {
28							storeHours.innerHTML = response.default_store.default_store_hours;
29						}
30						preselectStoreOption(response.default_store.default_store_id);
31
32						console.log('Geolocation already exists in session:', response.latitude, response.longitude, 'Source:', response.source);
33					}
34				} catch (e) {
35					console.log('Error parsing JSON response:', e);
36					// In case of error, try to get geolocation anyway
37					initGeolocation();
38				}
39			}
40		};
41		xhr.send('action=checkGeolocation');
42	}
43
44	/**
45	 * Initialize geolocation process
46	 */
47	function initGeolocation() {
48		if (navigator.geolocation) {
49			navigator.geolocation.getCurrentPosition(
50				function(position) {
51					// Successfully obtained position
52					var latitude = position.coords.latitude;
53					var longitude = position.coords.longitude;
54
55					// Send data to server via AJAX
56					sendGeolocationToServer(latitude, longitude, 'browser');
57				},
58				function(error) {
59					// Error getting position
60					var errorMessage;
61					switch(error.code) {
62						case error.PERMISSION_DENIED:
63							errorMessage = 'User denied geolocation request.';
64							break;
65						case error.POSITION_UNAVAILABLE:
66							errorMessage = 'Location information is unavailable.';
67							break;
68						case error.TIMEOUT:
69							errorMessage = 'The request to get user location timed out.';
70							break;
71						case error.UNKNOWN_ERROR:
72							errorMessage = 'An unknown error occurred.';
73							break;
74					}
75					console.log('Geolocation error: ' + errorMessage);
76
77					// Fallback to IP geolocation
78					getFallbackGeolocation();
79				},
80				{
81					enableHighAccuracy: true,
82					timeout: 5000,
83					maximumAge: 0
84				}
85			);
86		} else {
87			console.log('Geolocation is not supported by this browser');
88			// Fallback to IP geolocation
89			getFallbackGeolocation();
90		}
91	}
92
93	/**
94	 * Send geolocation data to the server
95	 */
96	function sendGeolocationToServer(latitude, longitude, source) {
97		var xhr = new XMLHttpRequest();
98		xhr.open('POST', geolocajax_url, true);
99		xhr.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
100		xhr.onreadystatechange = function() {
101			if (xhr.readyState === 4 && xhr.status === 200) {
102				var response = JSON.parse(xhr.responseText);
103				var storeName = document.querySelector('.js-store-name');
104				var storeHours = document.querySelector('.js-store-hours');
105				if (storeName) {
106					storeName.innerHTML = response.default_store.default_store_name;
107				}
108				if(storeHours) {
109					storeHours.innerHTML = response.default_store.default_store_hours;
110				}
111				preselectStoreOption(response.default_store.default_store_id);
112				console.log('Geolocation saved successfully');
113			}
114		};
115		xhr.send('action=saveGeolocation&latitude=' + latitude + '&longitude=' + longitude + '&source=' + source);
116	}
117
118	/**
119	 * Get geolocation using IP as fallback method
120	 */
121	function getFallbackGeolocation() {
122		var xhr = new XMLHttpRequest();
123		xhr.open('POST', geolocajax_url, true);
124		xhr.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
125		xhr.onreadystatechange = function() {
126			if (xhr.readyState === 4 && xhr.status === 200) {
127				var response = JSON.parse(xhr.responseText);
128				var storeName = document.querySelector('.js-store-name');
129				var storeHours = document.querySelector('.js-store-hours');
130				if (storeName) {
131					storeName.innerHTML = response.default_store.default_store_name;
132				}
133				if(storeHours) {
134					storeHours.innerHTML = response.default_store.default_store_hours;
135				}
136				preselectStoreOption(response.default_store.default_store_id);
137				console.log('IP geolocation obtained');
138			}
139		};
140		xhr.send('action=getIPGeolocation');
141	}
142
143	function preselectStoreOption(defaultStoreId) {
144		const radios = document.getElementsByName('store_option');
145		radios.forEach(radio => {
146			if (radio.value == defaultStoreId) {
147				radio.checked = true;
148				preselectProductSemaphores(defaultStoreId);
149			}
150		});
151	}
152
153	function preselectProductSemaphores(defaultStoreId) {
154		var product_semaphore = document.querySelector('#product-semaphore');
155		if(product_semaphore){
156			var xhr = new XMLHttpRequest();
157			xhr.open('POST', geolocajax_url, true);
158			xhr.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
159			xhr.onreadystatechange = function() {
160				if (xhr.readyState === 4 && xhr.status === 200) {
161					product_semaphore.innerHTML = xhr.responseText;
162				}
163			};
164			xhr.send('action=getProductSempahores&productId='+productId+'&defaultStoreId=' + defaultStoreId);
165		}
166	}
167
168	const radios = document.getElementsByName('store_option');
169	radios.forEach(radio => {
170		radio.addEventListener('click', function() {
171			var xhr = new XMLHttpRequest();
172			xhr.open('POST', geolocajax_url, true);
173			xhr.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
174			xhr.onreadystatechange = function() {
175				if (xhr.readyState === 4 && xhr.status === 200) {
176					console.log('Default store set successfully');
177					location.reload();
178				}
179			};
180			xhr.send('action=setDefaultStore&defaultStoreId=' + this.value);
181		});
182	});
183});

Line numbers count LF bytes from the start of the resource, as the search results do. Vendor segments are library code the classifier recognised; they are stored but not indexed. Bytes are shown as Latin1 characters, one per byte.