Comments (8)
Hi @smeijer can you please confirm how many content scripts are being loaded into the page by your extension that also have the webext-bridge/content-script
import?
from webext-bridge.
Hi @zikaari ,
I have 3 content-scripts in total. Only one imports webext-bridge/content-script
.
-
A single content-script with world "isolated", with contents: (that's the whole file)
// content-script/content-script.js import { allowWindowMessaging } from 'webext-bridge/content-script'; allowWindowMessaging('com.testing-playground.devtools');
-
I also inject a content-script on world "MAIN", with contents:
// content-script/window-script.js import { sendMessage, onMessage, setNamespace } from 'webext-bridge/window'; setNamespace('com.testing-playground.devtools'); // truncated
-
I inject a content-script on world MAIN that adds some methods to
window
. It does not includewebext-bridge
I load all those scripts via the manifest:
"content_scripts": [
{
"matches": ["<all_urls>"],
"js": ["content-script/content-script.js"],
"run_at": "document_start"
},
{
"matches": ["<all_urls>"],
"js": ["content-script/window-script.js"],
"run_at": "document_start",
"world": "MAIN"
},
{
"matches": ["<all_urls>"],
"js": ["window/testing-library.js"],
"run_at": "document_start",
"world": "MAIN"
}
]
For full disclosure, I also have a background script (full contents):
// background.js
import 'webext-bridge/background';
And I need to communicate between content-script/window-script.js
&& devtools. Which again, works after page refresh, but not before 🤷♂️
from webext-bridge.
@zikaari, any thoughts? I'd appreciate any help you could offer me.
from webext-bridge.
I have a similar problem, also when I open a new tab. Also only fixed when I do a full page load. In my case I'm trying to send a message from my background script to the content script in the new tab.
Then as I type in the omnibox it autocompletes a page and prerenders it. My content script is injected and starts working just fine. But then when my background script detects that this new tab is the "active" one now and tries to send it a message, the content script never receives the message.
It seems related to this Stack Overflow article - where Chrome is automatically disconnecting the port for the content scripts of prerendered pages once they transition to their "active" lifecycle stage: https://stackoverflow.com/questions/76841137/in-chrome-extension-content-script-does-not-receive-message-from-background-on
The author of that problem solved the problem by detecting the disconnect and automatically reconnecting, queueing up and finally sending any unsent messages once reconnected.
from webext-bridge.
Folks, I apologize for delayed response. Unfortunately I no longer maintain this project due to lack of resources. @smeijer would you be willing to take on this project since your project heavily depends on it?
from webext-bridge.
No worries. I ended up solving by removing this package as a dependency and just writing my own simple messaging utility functions. I didn't dig in too deeply but pretty sure the problem is in the code that generates a unique key to identify the tab. Guessing that when a page is pre-rendered (when typing in a new tab) that key is different then when it transitions to active, and then the message is aborted due to key mismatch.
from webext-bridge.
@zikaari, I wish I could, but I share the same lack of resources (time). So it wouldn't get any better in my hands. Sorry.
@grinholz , can you share that "simple messaging utility"?
from webext-bridge.
Something like this, where you just use the native events.
export async function sendMessageToTab(tabId: number, type: string, payload: unknown) {
return (await chrome.tabs
.sendMessage(tabId, {
to: "content",
type,
payload
})
);
}
from webext-bridge.
Related Issues (20)
- Window post message within an iframe. use parent.postMessage? HOT 1
- [Bug] `DataTypeKey` not working on `onMessage` and `sendMessage` parameter `messageID` HOT 3
- Pratical exemple how to send message between my js and background-script. HOT 1
- High cpu and ram usage HOT 3
- Latest version import failure related HOT 3
- Background stream sendMessage to any tab does not working
- Unchecked runtime.lastError: Could not establish connection. Receiving end does not exist. HOT 3
- This script should only be loaded in a browser extension. HOT 3
- No handler registered in 'background' error, but background handler still being found and invoked HOT 3
- Hard to debug unhandled promise errors
- 在 Vite中使用报错 TypeError: Cannot read properties of undefined (reading 'fingerprint') HOT 2
- Project unmaintained - looking for maintainers HOT 8
- [Feature]sidePanel messaging
- Create landing page & docs site HOT 2
- Update README
- Automate website PR deployments & previews
- Create automated NPM release process
- `tabId` is null when `stream.send` from background script
- How background communicates with content HOT 1
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from webext-bridge.