aklinker1 / webext-core

Collection of essential libraries and tools for building web extensions
https://webext-core.aklinker1.io
MIT License
96 stars 11 forks source link

Calling sendMessage from @webext-core/messaging/page gives "Uncaught Error: This script should only be loaded in a browser extension" #50

Closed AndrewWalsh closed 6 months ago

AndrewWalsh commented 6 months ago

Hello again,

This regards @webext-core/messaging.

I've tried to use defineWindowMessaging in a script that is dynamically injected via browser.scripting.registerContentScripts (in the MAIN world if it matters).

However I am receiving an error Uncaught Error: This script should only be loaded in a browser extension.

The message setup is:

import { defineWindowMessaging } from '@webext-core/messaging/page';

const ID = 'someid';

interface ProtocolSchema {
  onResponse(response: InterceptedResponse): void;
}

export const { sendMessage, onMessage } = defineWindowMessaging<ProtocolSchema>({
  namespace: ID,
});

I import sendMessage in the injected script. Whenever I call it I receive that error message.

Am I right in thinking that I can call this inside a content script in the main world?


This is the same as this issue https://github.com/zikaari/crx-bridge/issues/7 Also see https://github.com/zikaari/webext-bridge/pull/24

aklinker1 commented 6 months ago

Haha hello again @AndrewWalsh.

I have never ran a content script in the main world, so I don't know what the limitations are around messaging and the extension APIs off the top of my head

Based on the error message you're getting, scripts running in the main world do not have access to the browser or chrome globals.

But you are correct, defineWindowMessaging should work in a context that doesn't require any browser APIs. I'm thinking you have something else running in your script that is causing this error to occur.

Can you share a minimal reproduction? If you're using WXT, WXT might be throwing that error, I haven't tested it with a main world content script before.

aklinker1 commented 6 months ago

@AndrewWalsh I released v0.12.0 for WXT that includes a fix to make this work in unlisted scripts. WXT was the cause of this error, not webext-core.

AndrewWalsh commented 6 months ago

@aklinker1 That's amazing, I was going to submit a zip file but great to see you caught this. Thank you for taking a look!

AndrewWalsh commented 6 months ago

@aklinker1 Still getting this error, just had the opportunity to check. Both defineCustomEventMessaging and defineWindowMessaging do not work with injected scripts, returning the error This script should only be loaded in a browser extension..

My wxt version is v0.13.0, @webext-core/messaging is 1.4.0.

Messaging in the injected script looks like:

import { defineCustomEventMessaging } from '@webext-core/messaging/page';

interface Schema {
  onResponse(response: unknown): void;
}

export const {
  sendMessage,
  onMessage,
} = defineCustomEventMessaging<Schema>({
  namespace: 'some_ns',
});

I import sendMessage in an injected page script. When I call it, I receive that error.

Do you have any inclinations on this one and would you like me to open a new report?

aklinker1 commented 6 months ago

@AndrewWalsh Please open an issue on WXT and provide a minimal reproduction. I just reviewed the lib/page.js file that's shipped with this package, and it doesn't import the polyfill, so this isn't caused by @webext-core/messaging/page.