3

I am trying to build a Chrome extension but I have encountered a problem. I need access to a file that is in the same directory as my JS script. Here is the representation:

 my_script.js
 My Folder
  |-> my_file.file

I use a third party library that does some required things with it. And I need to pass in the path to my_file.file The problem is my_script.js gets injected into the website I am trying to change and the code instead of searching on the local file system it tries to find it in www.website.com/My Folder/my_file.file which obviously does not exist. How can I make it so that it seacrhes relative to my_script.js? I unfortunately can't pass the file itself to func(), it has to be the path of the file. The file I am trying to reach is about 200MB which I was planning on shipping with my extension. Also this project is vanilla JS. Thank you!

my_script.js

...
lib.func('My Folder/my_file.file').then(function (out) {
    document.out = out;
});
...
Istros
  • 31
  • 1
  • 3
  • Maybe this will help: https://stackoverflow.com/questions/7621822/access-local-files-through-google-chrome-extension – Mikhail Grechukhin Feb 10 '22 at 10:07
  • 1
    Use [chrome.runtime.getURL('My Folder/my_file.file')](https://developer.chrome.com/docs/extensions/reference/runtime/#method-getURL) – Iván Nokonoko Feb 10 '22 at 11:18
  • Thank you to both! I was able to solve it using getURL() and adding the file path to web_accessible_resources in the manifest. – Istros Feb 10 '22 at 12:55

0 Answers0