Advertisement
Guest User

Untitled

a guest
Jul 29th, 2023
96
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. import sha1 from 'js-sha1';
  2. import MicroModal from 'micromodal';
  3. import { canInstallUserJS, canInstallUserCSS } from "./managers";
  4.  
  5. function onInstallClick(event) {
  6.   event.preventDefault();
  7.   doInstallProcess(event.target)
  8. }
  9.  
  10. async function doInstallProcess(installLink) {
  11.   await detectCanInstall(installLink) &&
  12.     await showPreviousVersionWarning(installLink) &&
  13.     await showAntifeatureWarning() &&
  14.     await doInstall(installLink) &&
  15.     showPostInstall();
  16. }
  17.  
  18. async function detectCanInstall(installLink) {
  19.   let installTypeJS = installLink.getAttribute("data-install-format") == 'js';
  20.   if (installTypeJS) {
  21.     if (localStorage.getItem('manualOverrideInstallJS') == '1' || canInstallUserJS()) {
  22.       return true;
  23.     }
  24.   } else if (localStorage.getItem('manualOverrideInstallCSS') == '1' || await canInstallUserCSS()) {
  25.     return true;
  26.   }
  27.   return installationHelpFunction(installTypeJS)(installLink)
  28. }
  29.  
  30. function installationHelpFunction(js) {
  31.   return async function showInstallationHelpJS(installLink) {
  32.     const { detect } = await import('detect-browser')
  33.     let browserType = detect().name
  34.     let modal = document.getElementById(js ? "installation-instructions-modal-js" : "installation-instructions-modal-css")
  35.     switch (browserType) {
  36.       case 'firefox':
  37.       case 'chrome':
  38.       case 'opera':
  39.       case 'safari':
  40.         modal.classList.add("installation-instructions-modal-" + browserType)
  41.         break;
  42.       default:
  43.         modal.classList.add("installation-instructions-modal-other")
  44.     }
  45.     let bypassLink = modal.querySelector(".installation-instructions-modal-content-bypass a")
  46.     bypassLink.setAttribute("href", installLink.getAttribute("href"))
  47.     return new Promise(resolve => {
  48.       bypassLink.addEventListener("click", function (event) {
  49.         resolve(true)
  50.         localStorage.setItem(js ? 'manualOverrideInstallJS' : 'manualOverrideInstallCSS', '1')
  51.         MicroModal.close(modal.id)
  52.         event.preventDefault()
  53.       })
  54.       MicroModal.show(modal.id, {
  55.         onClose: modal => resolve(false)
  56.       })
  57.     })
  58.   }
  59. }
  60.  
  61. async function showPreviousVersionWarning(installLink) {
  62.   return new Promise(resolve => {
  63.     if (installLink.getAttribute("data-is-previous-version") == "true") {
  64.       if (!confirm(installLink.getAttribute("data-previous-version-warning"))) {
  65.         resolve(false)
  66.         return;
  67.       }
  68.     }
  69.     resolve(true)
  70.   })
  71. }
  72.  
  73. async function showAntifeatureWarning() {
  74.   return new Promise((resolve) => {
  75.     if (document.getElementById("preinstall-modal")) {
  76.       MicroModal.show('preinstall-modal', {
  77.         onClose: function (modal, button, event) {
  78.           if (event.target.hasAttribute("data-micromodal-accept")) {
  79.             resolve(true)
  80.           } else {
  81.             resolve(false)
  82.           }
  83.         }
  84.       });
  85.       return;
  86.     }
  87.     resolve(true)
  88.   })
  89. }
  90.  
  91. async function doInstall(installLink) {
  92.   return new Promise((resolve) => {
  93.     let pingUrl = installLink.getAttribute("data-ping-url")
  94.  
  95.     if (pingUrl) {
  96.       let ping_key = sha1(installLink.getAttribute("data-ip-address") + installLink.getAttribute("data-script-id") + installLink.getAttribute("data-ping-key"));
  97.       let xhr = new XMLHttpRequest();
  98.       xhr.open("POST", pingUrl + (pingUrl.includes('?') ? '&' : '?') + "ping_key=" + encodeURIComponent(ping_key), true);
  99.       xhr.overrideMimeType("text/plain");
  100.       xhr.send();
  101.  
  102.       try {
  103.         gtag('event', 'Script install', {
  104.           'event_label': installLink.getAttribute('data-script-id'),
  105.           'script_id': installLink.getAttribute('data-script-id'),
  106.           'value': 1
  107.         });
  108.       } catch (ex) {
  109.         // Oh well, don't die.
  110.       }
  111.  
  112.       // Give time for the ping request to happen.
  113.       setTimeout(function () {
  114.         location.href = installLink.href;
  115.         resolve(true)
  116.       }, 100);
  117.     } else {
  118.      location.href = installLink.href;
  119.       resolve(true)
  120.     }
  121.   })
  122. }
  123.  
  124. function onInstallMouseOver(event) {
  125.   let url = event.target.getAttribute("data-ping-url");
  126.   if (url && !/[&?]mo=3$/.test(url)) {
  127.     event.target.setAttribute("data-ping-url", url + (url.includes('?') ? '&' : '?') + "mo=3");
  128.   }
  129. }
  130.  
  131. function showPostInstall() {
  132.   setTimeout(function() {
  133.     let postInstall = document.querySelector(".post-install");
  134.     if (!postInstall) {
  135.       return;
  136.     }
  137.     postInstall.style.display = 'flex';
  138.   }, 2000);
  139. }
  140.  
  141. function init() {
  142.   document.querySelectorAll(".install-link").forEach(function(installLink) {
  143.     installLink.addEventListener("click", onInstallClick);
  144.     installLink.addEventListener("mouseover", onInstallMouseOver);
  145.     installLink.addEventListener("touchstart", onInstallMouseOver);
  146.   });
  147. }
  148.  
  149. window.addEventListener("DOMContentLoaded", init);
  150.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement