我正在开发一个网页的铬扩展,其中用户填写和HTML表单。填写表单并按下Submit按钮后,将弹出一个Javascript Confirm()。如何在Chrome扩展中捕获用户对此弹出窗口的响应,即如何知道用户在Chrome扩展中是否在此弹出窗口上按了OK或Cancel
发布于 2021-02-23 06:02:55
您的content script应该覆盖page context中的confirm
,如下所示:
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;
};
}
https://stackoverflow.com/questions/65999280
复制