Search code examples
google-chrome-extension

Get local file inside the extension folder in Chrome


I know that I can't get a local file from within the extension directory. It is possible to get a file that is inside the extension directory itself?


Solution

  • You can use chrome.runtime.getURL to get a fully-qualified URL to a resource.

    // Outputs path to the file regardless if it exits
    > chrome.runtime.getURL('assets/extension-icon.png');
    "chrome-extension://kfcphocilcidmjolfgicbchdfjjlfkmh/assets/extension-icon.png"
    

    The chrome-extension protocol plus the extension id, will be the address for the extension's root directory.

    If you need something more powerful, you might also use HTML5's FileSystem API which can create, read, write and list files from a sandbox in the current user's local file system.