2

Following OS.file example it uses TextEncoder however from bootstrap scope this is not available.

https://developer.mozilla.org/en-US/docs/JavaScript_OS.File/OS.File_for_the_main_thread#Example.3A_Read_the_contents_of_a_file_as_text

Only way I could access it is by going decoder = new Services.appShell.hiddenDOMWindow.TextDecoder(); but is thi the only way?

nmaier
  • 32,336
  • 5
  • 63
  • 78
Noitidart
  • 35,443
  • 37
  • 154
  • 323

1 Answers1

1

You can use TextEncoder and friends inside a javascript module. But doing so might be an overkill, if all you want is a couple of missing globals (putting aside the fact that jsm are cached and this might not work well with a restartless addon).

Conveniently Addon SDK's loader delivers what you need.

const { TextDecoder, TextEncoder } = Cu.import('resource://gre/modules/commonjs/toolkit/loader.js', {});
paa
  • 5,048
  • 1
  • 18
  • 22
  • Thanks paa! This does the trick! I don't understand your first line? Do you mean if I made a .jsm it has access to TextEncoder? – Noitidart Mar 30 '14 at 19:50
  • Yes, the code context of a `jsm` has access to the (mozilla) JS globals – paa Mar 31 '14 at 13:15