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
Asked
Active
Viewed 146 times
0
-
As per documentation that is not possible – Anchit Jindal Feb 22 '21 at 02:58
-
Can u please share code snippet – Anchit Jindal Feb 22 '21 at 20:42
1 Answers
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