Code Monkey home page Code Monkey logo

Comments (12)

Toumash avatar Toumash commented on July 16, 2024

Outside of the build tool scope - should be closed. Im using the quoted methods just fine

from chrome-extension-tools.

sovetski avatar sovetski commented on July 16, 2024

Outside of the build tool scope - should be closed. Im using the quoted methods just fine

Great if it works for you, but it will be more useful if you share your solution with some examples here, to help other people too :)

from chrome-extension-tools.

Toumash avatar Toumash commented on July 16, 2024

@sovetski
tl;dr
code:

// popup
const [tab] = await chrome.tabs.query({active: true,currentWindow: true});
if (tab.id === undefined) return;
await chrome.tabs.sendMessage(tab.id, { type: 'do_something' });

// content_script
function listener(msgObj, _, _) {
      if (msgObj.type === 'do_something') {
        //TODO: do something
      }
    }
chrome.runtime.onMessage.addListener(listener);

the only sample from google i could find injects a script straight from a popup - you dont need a listener then
https://github.com/GoogleChrome/chrome-extensions-samples/blob/main/functional-samples/reference.mv3-content-scripts/popup.js

My sample use case: i need to show a state in popup.js thats based on information from a content_script.
If you dont need a response (licenseState) you can just replace respond(licenseState); with your interact with the current tab's DOM code

This is a sample from react (a hook) but it doesnt matter - same methods.

note for more complex scenarios people usually communicate through a service worker (complex code that would need to run longer than popup is open) so that would be popup -> service_worker -> content_scripts.

// popup/useLicenseStatus.tsx
export const useLicenseStatus = (setLicense: (license: LicenseState) => void) => {
  useEffect(() => {
    const fetchData = async () => {
      const [tab] = await chrome.tabs.query({
        active: true,
        currentWindow: true,
      });
      if (tab.id === undefined) return;
      const response: LicenseState = await chrome.tabs.sendMessage(tab.id, { type:  'get_license_status'});
      console.log('response', response);
      if (response !== undefined) {
        setLicense(response);
      }
    };

    void fetchData();
  });
};
// contentscript.tsx
export function useShareLicenseStatus(licenseState: LicenseState) {
  const listener = useCallback(
    (msgObj, _, respond: (_: LicenseState) => void) => {
      if (msgObj.type === 'get_license_status') {
        respond(licenseState);
      }
    },
    [licenseState]
  );
  useEffect(() => {
    chrome.runtime.onMessage.addListener(listener);
    return () => chrome.runtime.onMessage.removeListener(listener);
  }, [listener]);
}

from chrome-extension-tools.

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.