Redirect to a link

redirect if correct age

2 Likes

Hi there, @user21505 :wave:

Glad to say that our devs came up with a custom solution for you:

const waitForElement = (selector, root = document, maxAttempts = 50) => 
  new Promise((resolve) => {
    let attempts = 0;

    const check = () => {
      const element = root.querySelector(selector);
      if (element && element.offsetWidth > 0) {
        resolve(element);
      } else if (attempts < maxAttempts) {
        attempts++;
        setTimeout(check, 100);
      }
    };

    check();
  });

function checkAgeVerification() {
  for (let key in localStorage) {
    if (key.startsWith("AgeVerification.allow")) {
      try {
        const data = JSON.parse(localStorage.getItem(key));
        if (data?.value === true) {
          window.location.href = 'https://www.google.com';
          return;
        }
      } catch (e) {}
    }
  }
}

function observeLocalStorage() {
  const originalSetItem = localStorage.setItem;
  
  localStorage.setItem = function (key) {
    originalSetItem.apply(this, arguments);
    if (key.startsWith("AgeVerification.allow")) {
      checkAgeVerification();
    }
  };
}

waitForElement('.eapp-age-verification-item-allow-year-submit').then((submitButton) => {
  submitButton.addEventListener('click', () => {
    checkAgeVerification();
  });

  observeLocalStorage();
  checkAgeVerification();
});

Please add this code to the Custom JS field on the Appearance tab of your widget’s settings and replace https://www.google.com with the needed link here:


Note: JS codes operate only upon widget installation, not in preview mode

Check it out and let me know if it worked :slightly_smiling_face:

1 Like