11144

ReferenceError: TextEncoder is not defined

Question:

I'm writing a simple addon in Firefox - 24, on Linux. I get the error:

ReferenceError: TextEncoder is not defined

when I do: var encoder = new TextEncoder(); the function I'm using is:

function write_text(filename, text) { var encoder = new TextEncoder(); var data = encoder.encode(text); Task.spawn(function() { let pfh = OS.File.open("/tmp/foo", {append: true}); yield pfh.write(text); yield pfh.flush(); yield pfh.close(); }); }

Answer1:

Ah, you're using the SDK, I gather when re-reading the actual error of your other question.

<ul><li>You need to import TextEncoder explicitly from some other module, as SDK modules lack the class.</li> <li>You need to yield OS.File.open.</li> <li>append: is only supported in Firefox 27+</li> <li>.flush() is only supported in Firefox 27+ (and a bad idea anyway). Use .writeAtomic if you need that.</li> <li>You write: true to write to a file.</li> </ul>

Here is a full, working example I tested in Firefox 25 (main.js)

const {Cu} = require("chrome"); // It is important to load TextEncoder like this using Cu.import() // You cannot load it by just |Cu.import("resource://gre/modules/osfile.jsm");| const {TextEncoder, OS} = Cu.import("resource://gre/modules/osfile.jsm", {}); const {Task} = Cu.import("resource://gre/modules/Task.jsm", {}); function write_text(filename, text) { var encoder = new TextEncoder(); var data = encoder.encode(text); filename = OS.Path.join(OS.Constants.Path.tmpDir, filename); Task.spawn(function() { let file = yield OS.File.open(filename, {write: true}); yield file.write(data); yield file.close(); console.log("written to", filename); }).then(null, function(e) console.error(e)); } write_text("foo", "some text");

Answer2:

The TextEncoder can be found in the sdk/io/buffer module:

let { TextEncoder, TextDecoder } = require('sdk/io/buffer')

Recommend

  • Kill Splash Screen When Click “BACK” Button
  • RestSharp PUT XML, RestSharp is sending it as GET?
  • laravel 5.1 auth csrf token mismatch
  • How to change type datatable column [duplicate]
  • Spring MVC with AspectJ
  • why do serviceSubscriberCellularProviders return nil? (in iOS 12)
  • CSS Selector picker/finder?
  • Back propagation algorithm: error computation
  • Plot.ly. Using slider control with multiple plots
  • Is there a way that I can manually control the thresholds between various range on choroplethr?
  • Migrating to CursorLoader & LoaderManager for AutoCompleteTextView with SQLiteDatabase
  • How to apply CSS to document.write()?
  • Change the font based on language CSS
  • Http Requests not getting routed to Https NodeJs
  • Can't access web service when connected to the network :: HTTP 407
  • Faces Servlet not parsing .xhtml pages in jsf 2. running on tomcat 7
  • ggplot2 facet_grid with distinct x-axis labels using facet_grid
  • Insert statement not working using execute(array()) of PDO Extension
  • JavaFX Embed Custom Font Not Working
  • How to create mirrored image effect with CSS single element
  • Corda: How to implement hierarchical relationships between state data persisted to H2
  • SpringBoot don't replacen System variable {user.home} in Spring Tool Suite Version: 3.8.4.RELEA
  • “Cannot open log for source” - Windows 7 - .NET web service - event log
  • Disable account chooser FirebaseUI React
  • When to use the tag in the head and body section of a html page? [duplicate]
  • Floating parent div grows to hypothetical width of floating child div
  • r - input value by user to dataframe via shiny
  • Calculate time from document
  • How to use Streams api peek() function and make it work?
  • Using redis as an LRU cache for postgres
  • Another “Cannot make static reference…” Question
  • VSTS work items list through REST API
  • `$http:badreq Bad Request Configuration` - from angular post method, what is wrong here?
  • Background transfer download task failed when app was closed
  • ssh remote server login script
  • Capture SIGFPE from SIMD instruction
  • Angular 4: Responsive Grid List
  • Using Service Component Runtime
  • How do I use TagLib-Sharp to write custom (PRIV) ID3 frames?
  • CAS 4 - Not able to retrieve the LDAP groups after successful authentication