-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmobile-detector.js
More file actions
103 lines (85 loc) · 3.1 KB
/
mobile-detector.js
File metadata and controls
103 lines (85 loc) · 3.1 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
// ===================================
// MOBILE DETECTION & VERSION ROUTER
// ===================================
// Detect if user is on mobile device
function isMobileDevice() {
const userAgent = navigator.userAgent || navigator.vendor || window.opera;
// Check for mobile user agents
const mobileRegex = /android|webos|iphone|ipad|ipod|blackberry|iemobile|opera mini/i;
const isMobile = mobileRegex.test(userAgent.toLowerCase());
// Check screen size as backup
const isSmallScreen = window.innerWidth <= 768;
return isMobile || isSmallScreen;
}
// Check if user has preference stored
function getUserVersionPreference() {
return localStorage.getItem('mysafety_version_preference');
}
// Set user version preference
function setUserVersionPreference(version) {
localStorage.setItem('mysafety_version_preference', version);
}
// Route to appropriate version
function routeToVersion() {
const path = window.location.pathname;
const preference = getUserVersionPreference();
const isMobile = isMobileDevice();
// 1. Don't touch Auth or Index pages
if (path.includes('auth.html') || path.includes('index.html') || path === '/' || path === '') {
return;
}
const onMobilePage = path.includes('dashboard-mobile.html');
const onDesktopPage = path.includes('dashboard.html');
// 2. STRICT PREFERENCE CHECK (Overrides everything)
if (preference === 'mobile') {
if (onDesktopPage) {
window.location.href = 'dashboard-mobile.html';
}
return; // If on mobile page, STAY THERE.
}
if (preference === 'desktop') {
if (onMobilePage) {
window.location.href = 'dashboard.html';
}
return; // If on desktop page, STAY THERE.
}
// 3. AUTO-DETECTION (Only if NO preference is set)
if (!preference) {
if (isMobile && onDesktopPage) {
window.location.href = 'dashboard-mobile.html';
} else if (!isMobile && onMobilePage) {
// Optional: Auto-redirect desktop users back to desktop view
// But only if they haven't explicitly chosen mobile
window.location.href = 'dashboard.html';
}
}
}
// Show version switcher
function showVersionSwitcher() {
const isMobile = isMobileDevice();
const currentVersion = window.location.pathname.includes('mobile') ? 'mobile' : 'desktop';
return {
currentVersion,
isMobile,
canSwitch: true
};
}
// Switch version manually
function switchVersion(targetVersion) {
setUserVersionPreference(targetVersion);
if (targetVersion === 'mobile') {
window.location.href = 'dashboard-mobile.html';
} else {
window.location.href = 'dashboard.html';
}
}
// Initialize on page load
if (typeof window !== 'undefined') {
// Don't auto-route on first visit to let user see landing page
const hasVisited = localStorage.getItem('mysafety_has_visited');
if (hasVisited && !window.location.pathname.includes('index.html')) {
routeToVersion();
} else {
localStorage.setItem('mysafety_has_visited', 'true');
}
}