jamescolin

Select fields in Systeme.io

Jul 5th, 2025
351
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
JavaScript 4.08 KB | Source Code | 0 0
  1. <script>
  2.   // 1. Define as many fields as you like here:
  3.   const fieldsConfig = [
  4.     {
  5.       inputId: "field-ec137bcd",
  6.       placeholderText: "Please choose a city...",
  7.       selectOptions: [
  8.         { value: "Paris", text: "Paris" },
  9.         { value: "NY",    text: "New York" },
  10.         { value: "LA",    text: "Los Angeles" }
  11.       ],
  12.       defaultValue: "",
  13.       hideOriginalInput: true
  14.     },
  15.     {
  16.       inputId: "field-b784a40e",
  17.       placeholderText: "Select a color...",
  18.       selectOptions: [
  19.         { value: "red",   text: "Red" },
  20.         { value: "green", text: "Green" },
  21.         { value: "blue",  text: "Blue" }
  22.       ],
  23.       defaultValue: "green",
  24.       hideOriginalInput: true
  25.     },
  26.     // …add more field definitions here
  27.   ];
  28.  
  29.   // Configuration for retry mechanism
  30.   const retryConfig = {
  31.     maxRetries: 10,        // Maximum number of retry attempts
  32.     retryDelay: 1000,      // Delay between retries in milliseconds (1 second)
  33.     checkInterval: 100     // Initial check interval in milliseconds
  34.   };
  35.  
  36.   // Function to process fields with retry logic
  37.   function processFieldsWithRetry(attempt = 0) {
  38.     // Check if the first field is available
  39.     const firstField = document.getElementById(fieldsConfig[0].inputId);
  40.    
  41.     if (!firstField) {
  42.       if (attempt < retryConfig.maxRetries) {
  43.         console.log(`First field not found, retrying in ${retryConfig.retryDelay}ms... (Attempt ${attempt + 1}/${retryConfig.maxRetries})`);
  44.         setTimeout(() => {
  45.           processFieldsWithRetry(attempt + 1);
  46.         }, retryConfig.retryDelay);
  47.         return;
  48.       } else {
  49.         console.error(`First field #${fieldsConfig[0].inputId} not found after ${retryConfig.maxRetries} attempts. Aborting.`);
  50.         return;
  51.       }
  52.     }
  53.  
  54.     // First field is available, proceed with processing all fields
  55.     console.log('First field found, processing all fields...');
  56.     processAllFields();
  57.   }
  58.  
  59.   // Function to process all fields
  60.   function processAllFields() {
  61.     fieldsConfig.forEach(cfg => {
  62.       const originalInput = document.getElementById(cfg.inputId);
  63.       if (!originalInput) {
  64.         console.error(`Input #${cfg.inputId} not found.`);
  65.         return;
  66.       }
  67.      
  68.       // build the <select>
  69.       const select = document.createElement("select");
  70.       select.className = originalInput.className;
  71.      
  72.       // placeholder
  73.       const placeholder = document.createElement("option");
  74.       placeholder.value    = "";
  75.       placeholder.text     = cfg.placeholderText;
  76.       placeholder.disabled = true;
  77.       placeholder.selected = true;
  78.       select.appendChild(placeholder);
  79.      
  80.       // actual options
  81.       cfg.selectOptions.forEach(opt => {
  82.         const o = document.createElement("option");
  83.         o.value = opt.value;
  84.         o.text  = opt.text;
  85.         if (opt.value === cfg.defaultValue) {
  86.           o.selected = true;
  87.           // also sync initial value
  88.           changeValue(originalInput, opt.value);
  89.         }
  90.         select.appendChild(o);
  91.       });
  92.      
  93.       // insert into DOM, hide original if requested
  94.       originalInput.parentNode.insertBefore(select, originalInput.nextSibling);
  95.       if (cfg.hideOriginalInput) {
  96.         originalInput.style.display = "none";
  97.       }
  98.      
  99.       // keep them in sync on change
  100.       select.addEventListener("change", () => {
  101.         changeValue(originalInput, select.value);
  102.         console.log(`Input #${originalInput.id} value changed to "${select.value}".`);
  103.       });
  104.     });
  105.   }
  106.  
  107.   // Initialize the process when the page loads
  108.   window.addEventListener('load', () => {
  109.     // Start with a small delay to ensure DOM is fully ready
  110.     setTimeout(() => {
  111.       processFieldsWithRetry();
  112.     }, retryConfig.checkInterval);
  113.   });
  114.  
  115.   // this helper makes sure any listeners on the original input fire as usual
  116.   function changeValue(input, value) {
  117.     Object.getOwnPropertyDescriptor(window.HTMLInputElement.prototype, "value")
  118.           .set.call(input, value);
  119.     input.dispatchEvent(new Event("input", { bubbles: true }));
  120.   }
  121. </script>
Advertisement
Add Comment
Please, Sign In to add comment