-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhome.js
905 lines (648 loc) · 32 KB
/
home.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
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
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
document.addEventListener("DOMContentLoaded", () => {
// =============================================================================
// Script 1
// =============================================================================
script1();
function script1() {
var script = document.createElement('script');
script.type = 'text/javascript';
script.src = 'https://cdnjs.cloudflare.com/ajax/libs/bodymovin/5.12.2/lottie_light.min.js';
script.async = true;
script.loading = 'none';
//script.defer = true;
script.onload = function () {
const animation = lottie.loadAnimation({
container: document.querySelector('.sales-lotte'), // Replace with your specific selector
loop: true,
autoplay: false,
path: 'https://lottie.host/1330341b-c93d-4a94-80c3-0283fbc902a6/dK6oWNihUp.json', // Replace with the path to your animation JSON file
});
// Function to start the animation
function startAnimation() {
animation.play();
}
// Function to stop the animation
function stopAnimation() {
animation.pause();
}
const observer = new IntersectionObserver(entries => {
entries.forEach(entry => {
if (entry.isIntersecting) {
// Animation is in the viewport, start it
startAnimation();
} else {
// Animation is out of the viewport, stop it
stopAnimation();
}
});
});
observer.observe(document.querySelector('.sales-lotte'));
};
document.head.appendChild(script);
// Initialize the Intersection Observer
// Observe the Lottie container
}
// =============================================================================
// Script 2
// =============================================================================
script2();
function script2() {
var numInputs = document.querySelectorAll('.num-input');
var mailInputs = document.querySelectorAll('.mail-input');
var forSaleTxt = document.querySelector('.sticker-for-sale');
numInputs.forEach(inputElement => {
inputElement.addEventListener('input', () => {
console.log('phone - change');
console.log(`1. ${forSaleTxt} 2. ${inputElement.value}`);
resizeInputFont(forSaleTxt, inputElement, 'phone');
//inputElement.style.maxWidth = Math.max(inputElement.value.length) + "ch";
});
inputElement.addEventListener('focus', () => {
console.log('phone - change');
console.log(`1. ${forSaleTxt} 2. ${inputElement.value}`);
resizeInputFont(forSaleTxt, inputElement, 'phone');
// inputElement.style.maxWidth = Math.max(inputElement.value.length) + "ch";
});
inputElement.addEventListener('input', function () {
// Remove any characters that are not valid in a phone number
inputElement.value = inputElement.value.replace(/[^0-9+()\s\-]/g, '');
});
});
mailInputs.forEach(inputElement => {
inputElement.addEventListener('input', () => {
console.log('mail - change');
console.log(`1. ${forSaleTxt} 2. ${inputElement}`);
resizeInputFont(forSaleTxt, inputElement, 'email');
// inputElement.style.maxWidth = Math.max(inputElement.value.length) + "ch";
});
inputElement.addEventListener('focus', () => {
console.log('mail - change');
console.log(`1. ${forSaleTxt} 2. ${inputElement}`);
resizeInputFont(forSaleTxt, inputElement, 'email');
//inputElement.style.maxWidth = Math.max(inputElement.value.length) + "ch";
});
inputElement.addEventListener('input', function () {
// Remove any characters that are not valid in an email address
inputElement.value = inputElement.value.replace(/[^a-zA-Z0-9@._-]/g, '');
});
});
function resizeInputFont(forSaleTxt, userInput, inputField) {
if (forSaleTxt && userInput) {
if (inputField === 'email') {
if (userInput.value.length >= 10 && userInput.value.indexOf('@') + 6 <= userInput.value.length && userInput.value.includes('@')) {
console.log("email");
var referenceText = forSaleTxt.textContent;
var targetText = userInput.value;
const computedStyle = window.getComputedStyle(forSaleTxt);
var referenceFontSize = parseFloat(computedStyle.getPropertyValue('font-size'));
var desiredFontSize = calculateFontSizeToMatchWidth(referenceText, targetText, referenceFontSize);
// Apply the desired font size to the target text
userInput.style.fontSize = `${desiredFontSize}px`;
} else {
console.log("email - fall back");
var referenceText = forSaleTxt.textContent;
var targetText = "[email protected]";
const computedStyle = window.getComputedStyle(forSaleTxt);
var referenceFontSize = parseFloat(computedStyle.getPropertyValue('font-size'));
var desiredFontSize = calculateFontSizeToMatchWidth(referenceText, targetText, referenceFontSize);
// Apply the desired font size to the target text
userInput.style.fontSize = `${desiredFontSize}px`;
userInput.style.cssText += `font-size: ${desiredFontSize}px;`;
}
} else if (inputField === 'phone') {
console.log(userInput.value.length);
if (userInput.value.length >= 7) {
console.log("phone");
var referenceText = forSaleTxt.textContent;
var targetText = userInput.value;
const computedStyle = window.getComputedStyle(forSaleTxt);
var referenceFontSize = parseFloat(computedStyle.getPropertyValue('font-size'));
var desiredFontSize = calculateFontSizeToMatchWidth(referenceText, targetText, referenceFontSize);
// Apply the desired font size to the target text
userInput.style.fontSize = `${desiredFontSize}px`;
} else {
console.log("phone - fall back");
var referenceText = forSaleTxt.textContent;
var targetText = "555-555-5555";
const computedStyle = window.getComputedStyle(forSaleTxt);
var referenceFontSize = parseFloat(computedStyle.getPropertyValue('font-size'));
var desiredFontSize = calculateFontSizeToMatchWidth(referenceText, targetText, referenceFontSize);
// Apply the desired font size to the target text
userInput.style.fontSize = `${desiredFontSize}px`;
// userInput.style.cssText += `font-size: ${desiredFontSize}px;`;
}
}
if (userInput.value.length != 0) {
console.log('normal');
// this.style.maxWidth = Math.max(this.value.length) + "ch";
console.log('anser: ' + userInput.value.length + "ch");
console.log((Math.max(userInput.value.length, userInput.placeholder.length) - 2));
console.log((Math.max(userInput.value.length, userInput.placeholder.length)));
//this.maxWidth = this.length + "ch";
const fontSizeInPx = 16; // Hardcoded font size in pixels
userInput.style.width = (userInput.value.length * fontSizeInPx / 16) + "ch";
//userInput.style.width = (Math.max(userInput.value.length) ) + "ch";
} else if (userInput.placeholder.length != 0) {
console.log('placeholder');
console.log(userInput.placeholder);
userInput.style.width = (Math.max(userInput.placeholder.length)) + "ch";
}
//adjustSpaceFontSize();
}
}
function calculateFontSizeToMatchWidth(referenceText, targetTextValue, referenceFontSize) {
// Access the value of the input box
var referenceWidth = getTextWidth(referenceText, referenceFontSize);
const ghostSpaceWidth = getTextWidth('a', referenceFontSize);
const spaceWidth = getTextWidth('a', 20);
console.log(`${referenceWidth} | ${spaceWidth} | ${ghostSpaceWidth}`);
referenceWidth = (referenceWidth - ghostSpaceWidth) + spaceWidth;
const targetWidth = getTextWidth(targetTextValue, referenceFontSize);
const desiredFontSize = ((referenceFontSize * referenceWidth) / targetWidth);
return desiredFontSize;
}
function getTextWidth(text, fontSize) {
const canvas = document.createElement('canvas');
const context = canvas.getContext('2d');
//context.font = `${fontSize}px Arial`;
context.font = `${fontSize}px Montserrat, sans-serif`;
return context.measureText(text).width;
}
}
// =============================================================================
// Script 3
// =============================================================================
//script3();
function script3() {
// JavaScript to randomize the order of review cards
function shuffleReviewCards() {
var hiddenCards = 2;
const reviewContainer = document.getElementById("review-container");
const reviewCards = reviewContainer.querySelectorAll(".review-card");
// Convert NodeList to an array for easier manipulation
const cardsArray = Array.from(reviewCards);
// Shuffle the array
for (let i = cardsArray.length - 1; i > 0; i--) {
const j = Math.floor(Math.random() * (i + 1));
[cardsArray[i], cardsArray[j]] = [cardsArray[j], cardsArray[i]];
}
// Clear the existing cards
reviewContainer.innerHTML = "";
// Append the shuffled cards back to the container
cardsArray.slice(0, -hiddenCards).forEach((card) => {
reviewContainer.appendChild(card);
});
}
// Call the function to shuffle review cards
shuffleReviewCards();
const reviewContainer = document.getElementById("review-container");
const reviewSection = document.getElementById("review-section");
let isScrolling = false;
reviewSection.addEventListener("mouseenter", () => {
isScrolling = true;
});
reviewSection.addEventListener("mouseleave", () => {
isScrolling = false;
reviewContainer.style.overflowY = "hidden"; // Reset overflowY to "hidden" when not scrolling
});
// Define a factor to control the scroll speed (adjust as needed)
const scrollSpeedFactor = 0.5; // Adjust this value for slower or faster scrolling
// Handle scroll events
reviewSection.addEventListener("wheel", (e) => {
if (isScrolling) {
const containerWidth =
reviewContainer.scrollWidth - reviewContainer.clientWidth;
if (e.deltaY < 0 && reviewContainer.scrollLeft <= 0) {
// If scrolling up at the start, allow page scroll
reviewContainer.style.overflowY = "auto";
} else if (
e.deltaY > 0 &&
reviewContainer.scrollLeft >=
containerWidth - containerWidth * 0.095
) {
// If scrolling down at the end, allow page scroll
reviewContainer.style.overflowY = "auto";
} else {
e.preventDefault(); // Prevent default page scroll
// Scroll horizontally with slower speed
reviewContainer.style.overflowY = "hidden"; // Prevent page scroll while hovering
reviewContainer.scrollLeft += e.deltaY * scrollSpeedFactor;
}
}
});
}
// =============================================================================
// Script 4
// =============================================================================
script4();
function script4() {
// Function to set a cookie
function setVisitedCookie(cname, cvalue, exdays) {
var d = new Date();
d.setTime(d.getTime() + (exdays * 24 * 60 * 60 * 1000));
var expires = "expires=" + d.toUTCString();
document.cookie = cname + "=" + cvalue + ";" + expires + ";path=/";
}
// Function to get the value of a cookie by name
function getVisitedCookie(cname) {
var name = cname + "=";
var decodedCookie = decodeURIComponent(document.cookie);
var ca = decodedCookie.split(';');
for (var i = 0; i < ca.length; i++) {
var c = ca[i];
while (c.charAt(0) === ' ') {
c = c.substring(1);
}
if (c.indexOf(name) === 0) {
return c.substring(name.length, c.length);
}
}
return "";
}
// Check if the 'visited' cookie exists
var forSaleTxts = document.querySelectorAll('.sticker-for-sale');
forSaleTxts.forEach(function (forSaleTxt) {
forSaleTxt.addEventListener("click", () => {
var visitedCookie = getVisitedCookie("visited");
if (visitedCookie === "") {
// If the 'visited' cookie doesn't exist, set it and perform actions for first-time visitors
setVisitedCookie("visited", "true", 1); // Set the cookie to expire in 365 days
// Actions for first-time visitors
// alert("Welcome! It's your first time visiting this site.");
displayHelpPopup();
} else {
// Actions for returning visitors
//alert("Welcome back!");
}
});
});
// Function to check if an element is in the viewport with a tolerance
function isElementInViewport(el, tolerance = 0) {
var rect = el.getBoundingClientRect();
return (
rect.top - tolerance <= (window.innerHeight || document.documentElement.clientHeight) &&
rect.left - tolerance <= (window.innerWidth || document.documentElement.clientWidth) &&
rect.bottom + tolerance >= 0 &&
rect.right + tolerance >= 0
);
}
function isScrollWithinDistances(distFromTop, distFromBottom) {
var totalHeight = document.documentElement.scrollHeight;
var windowHeight = window.innerHeight || document.documentElement.clientHeight;
var scrollPosition = window.scrollY || window.pageYOffset;
var fromTop = (scrollPosition / totalHeight) * 100; // Percentage from the top
var fromBottom = ((totalHeight - scrollPosition - windowHeight) / totalHeight) * 100; // Percentage from the bottom
return fromTop <= distFromTop || fromBottom <= distFromBottom;
}
// Function to handle the visibility of the button
function handleButtonVisibility() {
var stickerBuilders = document.querySelectorAll('.sticker-builder');
var helpButton = document.getElementById('helpButton');
// Check if any sticker-builder is in view with increased tolerance (e.g., 20 pixels)
var isAnyStickerBuilderInView = Array.from(stickerBuilders).some(el => isElementInViewport(el, 20));
var isAnyStickerBuilderInView = Array.from(stickerBuilders).some(el => isScrollWithinDistances(10, 10));
console.log(isAnyStickerBuilderInView);
if (isAnyStickerBuilderInView && isAnyStickerBuilderInView) {
// Show or hide the button based on the result
helpButton.style.display = isAnyStickerBuilderInView ? 'block' : 'none';
} else {
helpButton.style.display = "none";
}
adjustButtonPosition();
}
var scrollTimer;
function adjustButtonPosition() {
var helpButton = document.getElementById('helpButton');
helpButton.style.bottom = '20px';
clearTimeout(scrollTimer);
const distanceFromBottom = document.body.scrollHeight - window.scrollY - window.innerHeight;
var footer = document.querySelector('footer');
var footerHeight = footer.offsetHeight;
if (distanceFromBottom <= footerHeight) {
helpButton.style.bottom = `${footerHeight - distanceFromBottom + 20}px`; // 20px is the original bottom value
} else {
helpButton.style.bottom = '20px';
}
scrollTimer = setTimeout(() => {
// This code will run after the user stops scrolling for a certain duration
adjustButtonPosition();
}, 0.05); // Adjust the duration as needed
}
// Attach the adjustButtonPosition function to the window scroll event
window.addEventListener('scroll', handleButtonVisibility);
window.addEventListener('touch', handleButtonVisibility);
// Attach the handleButtonVisibility function to scroll and resize events
window.addEventListener('scroll', handleButtonVisibility);
window.addEventListener('resize', handleButtonVisibility);
// Initial check when the page loads
handleButtonVisibility();
}
// =============================================================================
// script 5
// =============================================================================
script5();
function script5() {
window.addEventListener('focus', function () {
// The window has gained focus
console.log('Window has focus');
pageVisible = true;
});
// Add a 'blur' event listener to track when the window loses focus
window.addEventListener('blur', function () {
// The window has lost focus
console.log('Window has lost focus');
pageVisible = false;
});
window.addEventListener('scroll', function () {
// The user is scrolling the page
if (!pageVisible) {
pageVisible = true;
console.log('User is scrolling');
setTimeout(() => {
if (!document.hasFocus()) {
pageVisible = false;
console.log('Page is not visible (timed out)');
}
}, 60000); // 5 seconds (5000 milliseconds)
}
});
const video = document.getElementById("myVideo");
let videoStarted = false;
let checkInterval; // Variable to store the interval for time checking
let isVideoPlaying = false;
let timeCounter = 0;
let pageVisible = true;
const timer = setInterval(timeTracker, 1000);
function timeTracker() {
if (pageVisible) {
timeCounter++;
// console.log(timeCounter);
}
};
document.addEventListener("visibilitychange", () => {
if (document.hidden) {
// Page is not visible
pageVisible = false;
} else {
// Page is visible
pageVisible = true;
}
});
window.addEventListener("beforeunload", () => {
clearInterval(timer);
});
// Function to start the video
function startVideo() {
if (!videoStarted) {
// Calculate the elapsed time
const currentTime = Date.now();
//const elapsed = (currentTime - initialTimestamp) / 1000; // Subtract 5 seconds to start earlier
var elapsed = timeCounter;
video.load();
video.addEventListener("loadeddata", function () {
// Make sure elapsed time is not negative
video.currentTime = elapsed < 0 ? 0 : elapsed;
//video.currentTime = 0;
});
var playPromise = video.play();
if (playPromise !== undefined) {
playPromise.then(_ => {
// Autoplay started successfully
}).catch(error => {
// Autoplay was prevented, handle the error
console.error("Autoplay not allowed:", error);
video.setAttribute("controls", "true");
video.addEventListener("play", function () {
console.log("User clicked play button");
checkVideoTime();
video.removeAttribute("controls");
});
});
}
isVideoPlaying = true;
videoStarted = true;
console.log("video started playing: ", video.currentTime);
const viewportHeightInPixels = window.innerHeight;
const marginInPixels = viewportHeightInPixels * 0.5;
const observer = new IntersectionObserver(entries => {
entries.forEach(entry => {
if (entry.isIntersecting) {
if (!isVideoPlaying) {
video.play();
console.log("video resumed", video.currentTime);
isVideoPlaying = true;
checkVideoTime();
}
} else {
if (isVideoPlaying) {
video.pause();
console.log("video paused", video.currentTime);
isVideoPlaying = false;
}
}
});
},
{
rootMargin: `${marginInPixels}px 0px ${marginInPixels}px 0px`,
}
);
observer.observe(video);
// Set up an interval to check video time every 10 seconds
checkInterval = setInterval(() => {
if (isVideoPlaying) {
checkVideoTime();
}
}, 10000); // Check every 10 seconds
}
}
initialTimestamp = Date.now();
// Function to check if the video should start
function checkVideoStart() {
const videoRect = video.getBoundingClientRect();
// Check if the video is at least 50vh above the viewport
if (videoRect.top - window.innerHeight - 59 <= 0) { //original 59
startVideo();
// Remove the scroll event listener once video starts
window.removeEventListener("scroll", checkVideoStart);
}
}
// Function to check video time synchronization
function checkVideoTime() {
//const currentTime = Date.now();
//const elapsed = (currentTime - initialTimestamp) / 1000;
var elapsed = timeCounter;
// Check if video time is out of sync by more than 5 seconds
var offSyncLimit = 10;
if (Math.abs(video.currentTime - elapsed) > offSyncLimit || Math.abs(video.currentTime - elapsed) < offSyncLimit) {
// Adjust video time to match elapsed time
video.currentTime = elapsed;
}
}
// Add an event listener to check when the page is scrolled
window.addEventListener("scroll", checkVideoStart);
// Start the video when the page loads (assuming it's in the viewport)
window.onload = checkVideoStart;
}
// =============================================================================
// script 6
// =============================================================================
script6();
function script6() {
// JavaScript to handle the responsive behavior
const tabletHiddenElements = document.querySelectorAll(".tablet-hidden");
// Store the original display value of each element
tabletHiddenElements.forEach(function (element) {
element.dataset.originalDisplay = getComputedStyle(element).display;
});
function setMobileHiddenDisplay() {
const screenWidth = document.documentElement.clientWidth;
// Check if the screen width is between 360px and 639px
if (screenWidth >= 639 && screenWidth <= 768) {
// Set the display property of elements with .mobile-hidden to 'none'
tabletHiddenElements.forEach(function (element) {
element.style.display = "none";
});
} else {
// Restore the original display property
tabletHiddenElements.forEach(function (element) {
element.style.display = element.dataset.originalDisplay;
});
}
}
// Add an event listener for window resize
window.addEventListener("resize", setMobileHiddenDisplay);
// Run the event listener on page load
setMobileHiddenDisplay();
}
// =============================================================================
// script 7
// =============================================================================
script7();
function script7() {
// JavaScript to handle the responsive behavior
const mobileHiddenElements = document.querySelectorAll(".mobile-hidden");
// Store the original display value of each element
mobileHiddenElements.forEach(function (element) {
element.dataset.originalDisplay = getComputedStyle(element).display;
});
function setMobileHiddenDisplay() {
const screenWidth = document.documentElement.clientWidth;
// Check if the screen width is between 360px and 639px
if (screenWidth >= 0 && screenWidth <= 639) {
// Set the display property of elements with .mobile-hidden to 'none'
mobileHiddenElements.forEach(function (element) {
element.style.display = "none";
});
} else {
// Restore the original display property
mobileHiddenElements.forEach(function (element) {
element.style.display = element.dataset.originalDisplay;
});
}
}
// Add an event listener for window resize
window.addEventListener("resize", setMobileHiddenDisplay);
// Run the event listener on page load
setMobileHiddenDisplay();
}
// =============================================================================
// script 8
// =============================================================================
script8();
function script8() {
// Initialize variable for tracking time in seconds
let totalTime = 0;
let isPageVisible = true; // Track page visibility
// Function to update the time on the page
function updateTimeOnPage() {
if (isPageVisible) {
totalTime++;
const timeOnPageDiv = document.getElementById("time-on-page");
let timeLabel;
let displayTime;
if (totalTime < 60) {
var seconds = totalTime % 60;
secondsLabel = totalTime % 60 === 0 ? "Second" : "Seconds";
displayTime = `${seconds} ${secondsLabel}`;
timeOnPageDiv.innerHTML = `<b>Time On Page: ${displayTime}</b>`;
} else {
const minutes = Math.floor(totalTime / 60);
var seconds = totalTime % 60;
if (seconds === 1) {
secondsLabel = "Second";
} else {
secondsLabel = "Seconds";
}
minutesLabel = totalTime <= 120 ? "Minute" : "Minutes";
displayTime = `${minutes} ${minutesLabel} ${seconds} ${secondsLabel}`;
timeOnPageDiv.innerHTML = `<b>Time On Page: ${displayTime}</b>`;
}
}
}
// Update the time every second
const interval = setInterval(updateTimeOnPage, 1000);
// Add an event listener to track page visibility
document.addEventListener("visibilitychange", () => {
if (document.hidden) {
// Page is not visible
isPageVisible = false;
} else {
// Page is visible
isPageVisible = true;
}
});
window.addEventListener('focus', function () {
// The window has gained focus
isPageVisible = true;
});
// Add a 'blur' event listener to track when the window loses focus
window.addEventListener('blur', function () {
// The window has lost focus
isPageVisible = false;
});
window.addEventListener('scroll', function () {
// The user is scrolling the page
if (!isPageVisible) {
isPageVisible = true;
setTimeout(() => {
if (!document.hasFocus()) {
isPageVisible = false;
console.log('Page is not visible (timed out)');
}
}, 60000); // 5 seconds (5000 milliseconds)
}
});
// To stop the timer when the user leaves the page, you can use the 'beforeunload' event
window.addEventListener("beforeunload", () => {
clearInterval(interval);
});
}
});
function showHelp() {
displayHelpPopup();
}
function closePopup() {
document.getElementById("popup-content").classList.add("close-animation");
setTimeout(function () {
var popupContainer = document.getElementById("popup-container");
//var popupContent = document.getElementById("popup-content");
document.getElementById("popup-content").classList.remove("close-animation");
popupContainer.style.display = "none";
}, 400);
}
function displayHelpPopup() {
var popupContainer = document.getElementById("popup-container");
var popupContent = document.getElementById("popup-content");
popupContainer.style.display = "flex";
var firstChild = popupContent.firstChild;
/*
// Create and append the iframe
var iframe = document.createElement("iframe");
iframe.src = "terms-of-service.html";
iframe.id = "iframe-container";
iframe.setAttribute("frameborder", "0");
iframe.setAttribute("width", "100%");
iframe.setAttribute("height", "100%");
popupContent.insertBefore(iframe, firstChild);
*/
}