Giter Club home page Giter Club logo

Comments (8)

zikaari avatar zikaari commented on June 2, 2024

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.

smeijer avatar smeijer commented on June 2, 2024

Hi @zikaari ,

I have 3 content-scripts in total. Only one imports webext-bridge/content-script.

  1. 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');
  2. 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
  3. I inject a content-script on world MAIN that adds some methods to window. It does not include webext-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.

smeijer avatar smeijer commented on June 2, 2024

@zikaari, any thoughts? I'd appreciate any help you could offer me.

from webext-bridge.

grinholz avatar grinholz commented on June 2, 2024

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.

zikaari avatar zikaari commented on June 2, 2024

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.

grinholz avatar grinholz commented on June 2, 2024

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.

smeijer avatar smeijer commented on June 2, 2024

@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.

grinholz avatar grinholz commented on June 2, 2024

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)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo 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.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.