Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Importing from within isolated function #21

Closed
blake-regalia opened this issue Apr 18, 2022 · 4 comments
Closed

Importing from within isolated function #21

blake-regalia opened this issue Apr 18, 2022 · 4 comments

Comments

@blake-regalia
Copy link
Contributor

blake-regalia commented Apr 18, 2022

Not exactly sure how to approach this issue. Service Worker needs to execute a script dynamically using chrome.scripting.executeScript with the func parameter, providing a function that will be serialized, sent across frames, and reconstructed on the other side. This creates a problem for importing anything from within the content script.

service-worker.ts

import ContentScript from './content-script';

chrome.runtime.onMessage.addListener((msg, sender) => {
  chrome.scripting.executeScript({
    target: {tabId:sender.tab.id},
    func: ContentScript,
    args: [secret],
  });
});

content-script.ts

import { say } from 'cowsay';  // this will never work anyway since it binds outside the scope of the function

export default async function(secret) {
  const { say } = await import('cowsay');  // this does not seem to work either

  console.log(say({text:secret});
}
@samrum
Copy link
Owner

samrum commented Apr 20, 2022

I think there are two potential options.

The first would be to pass say in as an arg to your content-script function (may not work depending on how say is serialized):
service-worker.ts

import ContentScript from './content-script';
import { say } from 'cowsay';

chrome.runtime.onMessage.addListener((msg, sender) => {
  chrome.scripting.executeScript({
    target: {tabId:sender.tab.id},
    func: ContentScript,
    args: [secret, say],
  });
});

content-script.ts

export default async function(secret, say) {
  console.log(say({text:secret});
}

The second would be to generate a static JS file that you'd inject using files instead of func:
service-worker.ts

chrome.runtime.onMessage.addListener((msg, sender) => {
  chrome.scripting.executeScript({
    target: {tabId:sender.tab.id},
    files: ['outputContentScript.js'], // defined and generated by vite
  });
});

content-script.ts - transpiled to outputContentScript.js at build time

import { say } from 'cowsay';

console.log(say({text:secret});

The issue there would be forwarding values from the service worker context to the injected script. Could use a message from the content script to retrieve that from the service worker, though.

@samrum
Copy link
Owner

samrum commented Apr 20, 2022

Anyways, there's not really an issue with the plugin here that I can see, just intricacies of working with chrome.scripting.executeScript, so I'm going to close this.

@samrum samrum closed this as completed Apr 20, 2022
@blake-regalia
Copy link
Contributor Author

@samrum Thanks for the response -- the argument approach is indeed what I needed, very clever! I hadn't thought of that. Totally appreciate your willingness to help me out via GH issues albeit unorthodox ;)

@blake-regalia
Copy link
Contributor Author

For anyone else who might find this same issue -- passing the function does not work in most cases. I am still trying to figure out a sane way to do this. I will likely resort to using a custom plugin.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants