How to override alert in V3?

571 views
Skip to first unread message

李文胜

unread,
Jul 5, 2022, 1:21:03 PM7/5/22
to Chromium Extensions
Hi Google,

I've been developing test automation Chrome extensions for Google Chrome for a couple of years now, and I am migrating them to Manifest V3. I have a need to overload the alert() function to be able to playback a test without losing control of the application window, and I cannot find how to do this using the new Chrome Extension policy.What is the best way to handle this? Is there an option to give more control to the Chrome extension to handle power use-cases like test automation? I think it would be clear to the user if a banner is shown, such as "This browser window is controlled by automation software".

Best Regards,

Wensheng

wOxxOm

unread,
Jul 5, 2022, 2:45:59 PM7/5/22
to Chromium Extensions, lws...@gmail.com
Assuming you've been using textContent to run the script in page context, which is not allowed anymore, there are several MV3-compatible methods shown in  https://stackoverflow.com/a/9517879. And if you want to override it before any page script runs, use registerContentScripts in your service worker like this:

chrome.runtime.onInstalled.addListener(addPageScript);

async function addPageScript() {
  const scripts = [{
    id: 'override',
    js: ['override.js'],
    matches: ['<all_urls>'],
    runAt: 'document_start',
    world: 'MAIN',
  }];
  const ids = scripts.map(s => s.id);
  await chrome.scripting.unregisterContentScripts({ ids }).catch(() => {});
  await chrome.scripting.registerContentScripts(scripts);
}

Now override.js can modify `alert` directly:

window.alert = console.log.bind(null, 'alert:');

Wensheng Li

unread,
Jul 19, 2022, 10:38:33 AM7/19/22
to wOxxOm, Chromium Extensions
Hi, wOxxOm,

Thank you very much for your help! It works very well! Thanks a lot!

BR

Wensheng

Simeon Vincent

unread,
Jul 19, 2022, 12:15:40 PM7/19/22
to Wensheng Li, wOxxOm, Chromium Extensions
wOxxOm, I'm curious why your code uses the unregister and register methods rather than update. Offhand I'm guessing that update fails if the specified ID doesn't exist, but I sonr have a computer hand to test that.

Simeon - @dotproto
Chrome Extensions DevRel

--
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/CABvmCLn-FStq5a401DWn5fgoXCuetChmPC2iXJyqr4w4%2BXWzwg%40mail.gmail.com.

Wensheng Li

unread,
Jul 21, 2022, 5:04:26 PM7/21/22
to Chromium Extensions, Simeon Vincent, wOxxOm, Chromium Extensions, Wensheng Li
Thank to your both help!

I found the solution doesn't work on iframe, even I used: allFrames:true

And, I want the code in matchAboutBlank as well, but look like it is not support on registerContentScripts method. 

Is there a solution to handle the two issues please? and is it possible to set it in manifest.json file? thanks a lot!

BR

wOxxOm

unread,
Jul 21, 2022, 5:56:32 PM7/21/22
to Chromium Extensions, lws...@gmail.com, Simeon Vincent, wOxxOm, Chromium Extensions
Message has been deleted

Wensheng Li

unread,
Jul 21, 2022, 10:52:31 PM7/21/22
to Chromium Extensions, wOxxOm, Wensheng Li, Simeon Vincent, Chromium Extensions
Thank you for your reply! I found I made a mistake. To use allFrames already ok. Thanks for your help anyway!
Reply all
Reply to author
Forward
0 new messages