Listening any event triggered to window(using window.post()) from an script in manifest v3

251 views
Skip to first unread message

Sudha Kumari

unread,
Dec 21, 2023, 5:17:02 AM12/21/23
to Chromium Extensions
Hi Folks,

I have an web application which trigger an event "captueScreenshot" to window using window.post() function.

My chrome extension(manifest v2) listen this event 
Using

window.addEventListener("message", (event) => {
    if (event.source !== window)
        return;

    if (event.data.type && (event.data.type === "CaptureScreenshot")) { // does something } }) I am working on migration of manifest v2 to manifest v3, and above code doesn't work in this scenario How can I resolve this?

Deco

unread,
Dec 21, 2023, 5:23:51 AM12/21/23
to Sudha Kumari, Chromium Extensions
You are using a background page, this has been replaced with service workers for Manifest V3 which is why it will not work. Replace the code to listen to a content script and when it captures the relevant event, it should send the message to the service worker. Something akin to this:

Content script:
window.addEventListener("message", (event) => {
    if (event.source !== window)
        return;

    if (event.data.type && (event.data.type === "CaptureScreenshot")) {
        chrome.runtime.sendMessage({ action: "captureScreenshot" });
    }
});


 Service worker:
chrome.runtime.onMessage.addListener(
    function(request, sender, sendResponse) {
        if (request.action === "captureScreenshot") {
            // Perform screenshot capture or other actions
        }
    }
);


Thanks,
Deco

--
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/9ef3ca2d-27cb-4c3d-b5cb-9255c0956ca3n%40chromium.org.

wOxxOm

unread,
Dec 21, 2023, 6:24:45 AM12/21/23
to Chromium Extensions, Deco, Chromium Extensions, Sudha Kumari
Assuming this is the content script, your old code should work in ManifestV3, so the problem is something else. Debug it in devtools or provide more info to us.

Sudha Kumari

unread,
Dec 21, 2023, 9:13:32 AM12/21/23
to Chromium Extensions, wOxxOm, Deco, Chromium Extensions, Sudha Kumari

Yes,  It is content script.

I am not able to see the content script of my extension injected in the devtool's source.

wOxxOm

unread,
Dec 21, 2023, 9:29:14 AM12/21/23
to Chromium Extensions, Sudha Kumari, wOxxOm, Deco, Chromium Extensions
Assuming you look in the correct place (devtools -> Sources panel -> Content script sub-panel), it means the content script didn't run.

It can happen due to a mistake in its declaration e.g. the wrong "matches".

If you use chrome.scripting to inject/register the script then debug that code as it probably just never runs or there's a mistake in the parameters or your "host_permissions" doesn't match the site. It can be caused by administrative policies that forbid access for extensions: open chrome://policy and see if it has ExtensionSettings with runtime_blocked_hosts inside. Another mistake is to switch the site access in the right-click menu of the extension's icon or in details of the extension in chrome://extensions page.

Reply all
Reply to author
Forward
0 new messages