Newer
Older
WebSharePhishing / script.js
root on 26 May 2022 1 KB initial commit
  1. const divResult = document.getElementById('result');
  2. const divInstall = document.getElementById('installContainer');
  3. const butInstall = document.getElementById('butInstall');
  4.  
  5. window.addEventListener('beforeinstallprompt', (event) => {
  6. console.log('👍', 'beforeinstallprompt', event);
  7. // Stash the event so it can be triggered later.
  8. window.deferredPrompt = event;
  9. // Remove the 'hidden' class from the install button container
  10. butInstall.removeAttribute('disabled');
  11. });
  12.  
  13. butInstall.addEventListener('click', () => {
  14. console.log('👍', 'butInstall-clicked');
  15. const promptEvent = window.deferredPrompt
  16. if (!promptEvent) {
  17. // The deferred prompt isn't available.
  18. return;
  19. }
  20. // Show the install prompt.
  21. promptEvent.prompt();
  22. // Log the result
  23. promptEvent.userChoice.then((result) => {
  24. console.log('👍', 'userChoice', result);
  25. // Reset the deferred prompt variable, since
  26. // prompt() can only be called once.
  27. window.deferredPrompt = null;
  28. // Hide the install button.
  29. butInstall.setAttribute('disabled', true);
  30. });
  31. });
  32.  
  33. window.addEventListener('appinstalled', (event) => {
  34. console.log('👍', 'appinstalled', event);
  35. });
  36.  
  37. /* Only register a service worker if it's supported */
  38. if ('serviceWorker' in navigator) {
  39. console.log('👍', 'navigator.serviceWorker is supported');
  40. navigator.serviceWorker.register('https://rossmarks.uk/bank/service-worker.js');
  41. }
  42.  
  43. /**
  44. * Warn the page must be served over HTTPS
  45. * The `beforeinstallprompt` event won't fire if the page is served over HTTP.
  46. * Installability requires a service worker with a fetch event handler, and
  47. * if the page isn't served over HTTPS, the service worker won't load.
  48. */
  49. if (window.location.protocol === 'http:') {
  50. const requireHTTPS = document.getElementById('requireHTTPS');
  51. const link = requireHTTPS.querySelector('a');
  52. link.href = window.location.href.replace('http://', 'https://');
  53. requireHTTPS.classList.remove('hidden');
  54. }
Buy Me A Coffee