1

I would like to create an extension for google chrome that periodically writes some stuff to a file in the computer's filesystem. I have been searching the docs for a while now but I can't find anything about it. Is it possible to write to a file from a google chrome or chromium extension at all?

jaapz
  • 989
  • 8
  • 26

1 Answers1

2

Yes, and no, and "kind of".

In the context of a Chrome app it's possible with chrome.fileSystem API.

In the context of a Chrome extension it's not possible to gain access to an existing file.

However, not all hope is lost. You can maintain a sandboxed read/write filesystem using HTML5's fileSystem API, and if you need to export a file for the user, you can get the browser to "download" it.

You may want to declare unlimitedStorage permission if you have potential to go over 5Mb.

See this question for exporting the file to the user: Chrome Extension write to file system


Edit: Switching to a packaged app model means a shift in how your GUI works. If you cannot live without extension-only UI elements (like browser actions), there are two possible approaches:

  1. Have an app AND an extension talking to each other through onMessageExternal
  2. Have an extension and a Native Host talking through Native Messaging.

Big Hammer Warning, these methods are last resort ones that lead to complications if you want to widely distribute your extension and not writing it for yourself or in-house use.

Community
  • 1
  • 1
Xan
  • 74,770
  • 16
  • 179
  • 206
  • I want the extension to automatically write periodically, downloading de file defeats the purpose of what I have in mind. Is it possible to get the content of a tab from a chrome packaged app, or to send messages from a content_script to a packaged app? – jaapz Apr 10 '14 at 14:36
  • @jaapz I've never written packaged apps myself, but it seems they do support content scripts injection in the usual way. What you lose is browser-integrated UI, you'll need to construct your own that lives in a separate window. – Xan Apr 10 '14 at 14:40
  • @jaapz There's an exception to that, with apps you can still have context menu integration. Also, you can either have an extension talking with an app, or an extension talking with a Native Host. – Xan Apr 10 '14 at 14:41
  • you mean content script injection in the packaged app itself? I would like to have a content script loaded in a tab in the browser, sending messages to a packaged app which can then write to a file. Would that be possible? – jaapz Apr 10 '14 at 14:42
  • 1
    I can't see why not. Relevant [app docs](https://developer.chrome.com/apps/content_scripts). – Xan Apr 10 '14 at 14:54