How to capture click on alerts

178 views
Skip to first unread message

Guillermo Posse

unread,
Apr 4, 2023, 4:00:37 PM4/4/23
to Chromium Extensions
Hi,

I'm working in a Chrome Extension that is able to record events on the current window. I have not found how to record events on alerts or popup windows that can be opened by a web application. Any idea?

Thanks

Patrick Kettner

unread,
Apr 10, 2023, 9:31:12 AM4/10/23
to Guillermo Posse, Chromium Extensions
Hi Guillermo!
Sorry for the slow response, for some reason your message got filtered as spam. I am not sure I understand what you are trying to accomplish, can you elaborate?

--
You received this message because you are subscribed to the Google Groups "Chromium Extensions" group.
To unsubscribe from this group and stop receiving emails from it, send an email to chromium-extens...@chromium.org.
To view this discussion on the web visit https://groups.google.com/a/chromium.org/d/msgid/chromium-extensions/397a4d3b-0205-40f5-b693-d9be1c78976bn%40chromium.org.
Message has been deleted

Patrick Kettner

unread,
Apr 12, 2023, 1:41:27 PM4/12/23
to Guillermo Posse, Chromium Extensions
Hey Guillermo!
You can be alerted to a modal window like alert(), prompt(), etc using the debugger api. 

```

chrome.runtime.onInstalled.addListener(async () => {

  const {id} = await chrome.windows.getCurrent();

  const tabs = await chrome.tabs.query({windowId: id});


  chrome.debugger.attach({ tabId: tabId }, "1.0",async () => {

    await chrome.debugger.sendCommand({ tabId: tabId }, "Page.enable");


    chrome.debugger.onEvent.addListener((debuggeeId, method, params) => {

      if (method == "Page.javascriptDialogOpening") {

        console.log("JavaScript dialog opening: " + JSON.stringify(params));


        // Modify/prevent dialog behavior


        chrome.debugger.sendCommand({ tabId: tabId }, "Page.handleJavaScriptDialog", {

          accept: true

        });

      }

    });

  });

```


note you will need to include tabs and debugger to the permissions section of your manifest




On Wed, Apr 12, 2023 at 12:19 PM Guillermo Posse <guiller...@taqtical.co> wrote:
Thank you Patrick,

I am developing a Chrome extension for a test automation tool. By adding an event listener for each event type that can occur within a document, I can capture any user action on the active tabs page. This is working fine. But I have not found a way to capture when an alert window is generated within the page, that is, the application that is being tested executes an "alert(message)" or "confirm(message)" win a Javascript code inside it. But I have not been able to identify that this window opens and the user accepts or cancels on it.

Simeon Vincent

unread,
Apr 13, 2023, 8:13:02 AM4/13/23
to Patrick Kettner, Guillermo Posse, Chromium Extensions
Be aware that attaching a debugger using the Debugger API will cause Chrome to show an infobar (banner) on all windows.

Simeon - @dotproto


Guillermo Posse

unread,
Apr 14, 2023, 12:35:37 AM4/14/23
to Chromium Extensions, Simeon Vincent, Guillermo Posse, Chromium Extensions, Patrick Kettner
Thank you Simeon for your warning.

Patrick: your code does not define the tabId. I tried to change it this way, but not getting anything in the console:

chrome.runtime.onInstalled.addListener(async function() {

  const {id} = await chrome.windows.getCurrent();
  const tabs = await chrome.tabs.query({windowId: id, active: true});

  if (tabs.length>0 && !tabs[0].url.startsWith('chrome://')) {
    const tabId = tabs[0].id;

    chrome.debugger.attach({ tabId: tabId }, "1.0",async () => {
      await chrome.debugger.sendCommand({ tabId: tabId }, "Page.enable");
 
      chrome.debugger.onEvent.addListener((debuggeeId, method, params) => {
        if (method == "Page.javascriptDialogOpening") {
          console.log("JavaScript dialog opening: " + JSON.stringify(params));
          chrome.debugger.sendCommand({ tabId: tabId }, "Page.handleJavaScriptDialog", {
            accept: true
          });
        }
      });
    });
  }
});

Patrick Kettner

unread,
Apr 14, 2023, 12:50:01 AM4/14/23
to Guillermo Posse, Chromium Extensions, Simeon Vincent
which console? If you go to chrome://extensions and click on "Inspect views: service worker" for the extension, it should bring up the console for the extension itself - the logging should be occurring there

Guillermo Posse

unread,
Apr 14, 2023, 1:32:58 AM4/14/23
to Chromium Extensions, Patrick Kettner, Chromium Extensions, Simeon Vincent, Guillermo Posse
Thank you very much for your help. 

I was able to manage it. I move your code from "chrome.runtime.onInstalled.addListener" to "chrome.tabs.onUpdated.addListener" so the debugger listener is started when a new page is loaded. That way I'm able to detect the dialog events that are triggered in that page. That's great!

Reply all
Reply to author
Forward
0 new messages