0

I am developing a chrome extension for a web page where user fill and HTML Form. After filling the form and pressing on Submit Button, there is a Javascript Confirm() popup. How to capture the response of user on this popup in Chrome Extension, i.e How to know whether user has pressed OK or Cancel on this popup inside Chrome Extension

1 Answers1

0

Your content script should override confirm in page context like this:

const eventId = chrome.runtime.id;
addEventListener(eventId, e => {
  console.log('Intercepted confirm:', e.detail);
  // do something with the result right here inside the listener
})
runInPage(fn, eventId);

function runInPage(fn, args) {
  const script = document.createElement('script');
  script.textContent = `(${fn})(${JSON.stringify(args).slice(1, -1)})`;
  document.documentElement.appendChild(script);
  script.remove();
}

function hookConfirm(eventId) {
  const { confirm } = window;
  window.confirm = (...args) => {
    const res = confirm.apply(window, args);
    dispatchEvent(eventId, new CustomEvent(eventId, {detail: res}));
    return res;
  };
}
wOxxOm
  • 65,848
  • 11
  • 132
  • 136