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

What could be right way to dynamically import a file from a npm package into a webpage through content_script? #44

Closed
abhinavzspace opened this issue Jul 21, 2020 · 10 comments
Labels
help wanted Issue with a clear description that the community can help with. status: needs reproduction This issue needs a simplified reproduction of the bug for further troubleshooting. type: question or discussion Issue discussing or asking a question

Comments

@abhinavzspace
Copy link

I have been unsuccessful in trying to import a file from npm package into a web page through content_script. I want to import this file on an action from popup browser action.

To establish the basic setup, I tried importing your emoji-log library. I followed the following steps:

  1. Add new CopyWebpackPlugin([{from: 'node_modules/emoji-log/lib/index.js', to: 'js/emoji-log/lib/index.js'}]), in webpack.config.js
  2. Add following in manifest.json.
"web_accessible_resources": [
  "js/emoji-log/lib/index.js"
],
  1. In content script. (Right now for testing purpose, I am opening it directly. Otherwise I want to open it on an action)
import browser from 'webextension-polyfill';
(async () => {
	const src = browser.extension.getURL("js/emoji-log/lib/index.js");
	await import(src);
})();

I get the following error:

Uncaught (in promise) Error: Cannot find module 'chrome-extension://bkglmbblddekhjgldmkjgadhojbhakjg/js/emoji-log/lib/index.js'

And strangely when I click on the filename, it opens up in the new tab straight away.

What am I missing here? I am stuck on this from quite a few hours.

(These instructions are mostly from here and here)

@abhijithvijayan
Copy link
Owner

Could you kindly add a reproducible repo for me to test the above?

@abhijithvijayan abhijithvijayan added status: needs reproduction This issue needs a simplified reproduction of the bug for further troubleshooting. type: question or discussion Issue discussing or asking a question labels Jul 21, 2020
@abhinavzspace
Copy link
Author

Sure. Give me a few minutes. And Thanks!

@abhijithvijayan
Copy link
Owner

Try with browser.runtime.getURL()

@abhinavzspace
Copy link
Author

Tried both. runtime and extension.

Please find the code here

@abhijithvijayan
Copy link
Owner

abhijithvijayan commented Jul 21, 2020

The path is generated correctly but the import statement seems to break the injected script usage

I will look more into it anyways. Also, even if this works in chrome, it won't work in firefox

@abhinavzspace
Copy link
Author

ahhh... Is there an alternate way of doing this. I just need to inject script occasionally on an action.

@abhijithvijayan
Copy link
Owner

@abhinavzspace I had luck with this but it doesn't use dynamic imports.

I will look into this issue anyways. My guess with with babel plugins.

@abhijithvijayan abhijithvijayan added the help wanted Issue with a clear description that the community can help with. label Jul 21, 2020
@abhinavzspace
Copy link
Author

Thanks! Will look into it.

@abhinavzspace
Copy link
Author

Confirmed. Alternate way you suggested, works. Thanks a lot.

@abhijithvijayan
Copy link
Owner

Cool. The way with dynamic import should've worked, but at this point I am unclear about why it doesn't work.

Closing this issue for now. Feel free to reopen if you run into problems again.

Thanks

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
help wanted Issue with a clear description that the community can help with. status: needs reproduction This issue needs a simplified reproduction of the bug for further troubleshooting. type: question or discussion Issue discussing or asking a question
Projects
None yet
Development

No branches or pull requests

2 participants