samrum / vite-plugin-web-extension

A vite plugin for generating cross browser platform, ES module based web extensions.
MIT License
323 stars 33 forks source link

Can't use injected scripts #118

Open aleksolutions opened 1 year ago

aleksolutions commented 1 year ago

When I try to inject a script to the client (I need to use some globals available in the site) and build the extension, the transpiled code tries to use getUrl but that method is not available on the client (obviously)

There is any way to make this work?

samrum commented 1 year ago

The plugin creates a wrapper script for scripts that contain imports, so to avoid that you can remove imports from your script. Alternatively could drop a static script in public and manually inject it.

gehaktmolen commented 1 year ago

Can't you just use the Chrome executeScript function?

await chrome.scripting.executeScript({
              target: {
                  tabId = 0,
                  // frameIds = []
              },
              func: runInjectScript,
              args: ['something', true, 123],
              world: 'MAIN'
          });

const runInjectScript = () => { console.log('Hi, show me window object:', window);}