ReferenceError: window is not defined chrome extension with manifest v3

4,964 views
Skip to first unread message

ym_developer

unread,
May 25, 2022, 4:46:15 AM5/25/22
to Chromium Extensions
Hi,
I have chrome extension in manifest V2
And I'm trying to move it to manifest  V3
The new manifesto looks like this

r1.JPG
I have an ONLOAD function on the background page
Like this: window.onload
And I always get the error:
r2.JPG
How do I solve this problem, I need the ONLOAD function
Thanks ,


Browser Extenstion

unread,
May 25, 2022, 8:17:16 AM5/25/22
to Chromium Extensions, ym_developer
No access to window in service worker.

Simon Bassu

unread,
May 25, 2022, 8:39:58 AM5/25/22
to ym_developer, Chromium Extensions


You have to use "self" instead.

I suggest this

let window = self;


Simon B

Le 25 mai 2022 à 04:46, ym_developer <yehud...@gmail.com> a écrit :

Hi,
I have chrome extension in manifest V2
And I'm trying to move it to manifest  V3
The new manifesto looks like this

<r1.JPG>
I have an ONLOAD function on the background page
Like this: window.onload
And I always get the error:
<r2.JPG>
How do I solve this problem, I need the ONLOAD function
Thanks ,



--
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/ed37674f-5f4a-4c81-800f-9dacd6d55c40n%40chromium.org.
<r2.JPG><r1.JPG>

PhistucK

unread,
May 25, 2022, 9:44:50 AM5/25/22
to Simon Bassu, ym_developer, Chromium Extensions
Not sure service workers have a load event/onload property, though?
You can just run your code directly. If your code runs, that means the service worker is loaded. If you want your code to run last, call it last.
Manifest version 3 does not support background pages, it only supports service workers. That means you have no DOM, in case you need it.

PhistucK


FRANK G

unread,
Jan 1, 2023, 10:38:02 PM1/1/23
to Chromium Extensions, PhistucK, ym_developer, Chromium Extensions, Simeon Bassu
I don't think self is the actual window, but rather a service worker object.

You can try

chrome.scripting.executeScript({

  target: { tabId: currentTab.id },

  func: () =>window.history.back()

});

wOxxOm

unread,
Jan 2, 2023, 3:11:02 AM1/2/23
to Chromium Extensions, fuxi...@gmail.com, PhistucK, ym_developer, Chromium Extensions, Simeon Bassu
You don't need window.onload and never needed even in MV2.

Simply remove it and use an IIFE at the end of the file:

(async () => {
  // ..........
  // ..........
})();

Reply all
Reply to author
Forward
0 new messages