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

Images in assets/img not displaying in content #38

Closed
ShaunLWM opened this issue Dec 4, 2022 · 8 comments
Closed

Images in assets/img not displaying in content #38

ShaunLWM opened this issue Dec 4, 2022 · 8 comments

Comments

@ShaunLWM
Copy link

ShaunLWM commented Dec 4, 2022

Steps to reproduce

  1. Download image to use to src/assets/img folder (example: login.svg)
  2. Import logo using import LogSvg from "@assets/img/login.svg";
  3. Run yarn dev and import extension to Chrome
  4. Logo not loaded properly - shows broken image as it is trying to fetch the image from the webpage instead
    image
@ShaunLWM ShaunLWM changed the title Images in assets/img not loading in content Images in assets/img not displaying in content Dec 4, 2022
@github-actions
Copy link

github-actions bot commented Dec 4, 2022

Thank you for your contribution. We will check and reply to you as soon as possible.

@Araneline
Copy link

Araneline commented Dec 13, 2022

Hello @ShaunLWM,
I don't think it's a boilerplate problem. Rather a lack of documentation. Let's dive into the problem:

First of all, contentscript should understand that we want an image from our extension and not from a page. For that we can use the chrome.runtime.getURL() function. It looks like this:

import login from '@assets/img/login.svg'
...
<img src={chrome.runtime.getURL(login)} alt="login" />

Oops, still not working. Check the errors in chrome-extension page and discover the line saying our image should be listed under web_accessible_resources in manifest. All right, let's do it. Open the manifest.ts file in our root directory and add this:

web_accessible_resources: [
    {
      resources: [
        ...
        'assets/svg/*.svg',
    }
]

After that, reload the extension and you should see your image.

TLDR:
You want to wrap your image imports in chrome.runtime.getURL() and list the folder with images in your manifest.ts under web_accessible_resources.

P.S. Mind that if you will try to import a png or jpg, you should add something like 'assets/png/*.png' to the list of resources.

@ShaunLWM
Copy link
Author

Understood. Will have a look. Thank you so much for the detailed explanation, appreciate it.

@Zaniyar
Copy link

Zaniyar commented Aug 18, 2023

how about other resources like pdfs or so? I get compilation erros for those files.

[vite:build-import-analysis] Parse error @:1:34998
...
...

                                         ^
2: 7H7@ݶ��6,7���P%7l7`♥��>7�7�↨��c7�7�5�S5�5,���67���k6�6�8��6►7���8↔7d7��1�2@�7�7 L�ئ7�7 ����7�7Ё�@�6.7�Ŷ�+7r7��↑7R7@��x7�7�F��Z7�7�,���6(7���`�7��� �6�6�z���67��� �6$7��0     7:7�Ͷ�*7h7♥�@�6↑7����6�6A���6�6]�@k6�61���6�6�����6�6�I���6�6s���6♦7���8�7�7p��H�7�7�v��47p7�♠��7<7Ѷ�j7�7�3��97r7 ♣� �7�7�<��7�7@m��♣7.7@����7�70��y7�7�,���7�*8�·<�7�79���7§8`��P�6∟7����4�4��  5`5д4��►6H6۵��5 6���$6h6�v5�5:�05@���5�5�����4�����������A6�6� n6�6�/���506���J6�6§��4�2`§6X6۵��6�6�F�►�6�6i��6�6�D�`�6�6t���43��22[8@�8(♣���7�8►�6@6ѵ�[6�6$��W6�6!��46p6��@
3: 686ϵ�26�6♣�@-6h6☻��)6`6���!6X6�@R6�6∟�@M6�6↑���6�6�a�@�6�6l� �6�6D��6�6�>��6�6�����67�����6�6C�@6�6���6�6X��w6�6�7��d6�6*��6�6�a�Ч6�6x��K6�6�▬�@26x6�♦��m6�60�p4�4P��4�4h��45H��4�4h��4�4���45p��45H��4�4H��45X�P4�4@��45���4@5��j4�4@��45��x4�4@�Z4�4↑�v4�48��4 5���4�4h��45`��45h��4�4H��4�4p�<45�d4�4P�X4�4 F7@ն��67���►�6
transforming (4259) node_modules\lodash\_hashDelete.js
index.js:38     GET chrome-extension://bcjighfjikochhpeopamggdmdjcbfdid/assets/js/index.js net::ERR_FILE_NOT_FOUND
dynamicImport @ index.js:38
(anonymous) @ index.js:39
preload @ index.js:8
(anonymous) @ index.js:37
Show 4 more frames
Show less
/:1 Uncaught (in promise) TypeError: Failed to fetch dynamically imported module: chrome-extension://bcjighfjikochhpeopamggdmdjcbfdid/assets/js/index.js

@Jonghakseo
Copy link
Owner

@Zaniyar

Have you added the PDF extension to your resources?

@Zaniyar
Copy link

Zaniyar commented Aug 26, 2023

@Jonghakseo I ended up using the vite plugin-url https://www.npmjs.com/package/@rollup/plugin-url to load binary files

@Jonghakseo
Copy link
Owner

@all-contributors please add @Araneline for question

@allcontributors
Copy link
Contributor

@Jonghakseo

I've put up a pull request to add @Araneline! 🎉

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

4 participants