serversideup / webext-bridge

💬 Messaging in Web Extensions made easy. Batteries included.
https://serversideup.net/open-source/webext-bridge
MIT License
505 stars 46 forks source link

This script should only be loaded in a browser extension. #68

Open addlistener opened 1 year ago

addlistener commented 1 year ago
image

webext-bridge 6.0.1

complaining code

import { sendMessage, onMessage } from "webext-bridge/content-script";

Possibly related to https://github.com/zikaari/crx-bridge/issues/7 ? However no installation of webextension-polyfill or crx-polyfill are found

zikaari commented 11 months ago

You might be importing this in the wrong runtime, for example if this is being run in a script injected by content script - it won't work

beautyfree commented 9 months ago

You might be importing this in the wrong runtime, for example if this is being run in a script injected by content script - it won't work

Faced the same problem. Is there a way to make this work in an injected script?

beautyfree commented 9 months ago

Looks like I managed it. Now I use webext-bridge/widnow in injected script

and don't forget about it: https://github.com/zikaari/webext-bridge?tab=readme-ov-file#sendmessagemessageid-string-data-any-destination-string

Note: For security reasons, if you want to receive or send messages to or from window context, one of your extension's content script must call allowWindowMessaging() to unlock message routing. Also call setNamespace() in those window contexts. Use same namespace string in those two calls, so webext-bridge knows which message belongs to which extension (in case multiple extensions are using webext-bridge in one page)