WebVTTParserWrapper.sys.mjs (1597B)
1 /* This Source Code Form is subject to the terms of the Mozilla Public 2 * License, v. 2.0. If a copy of the MPL was not distributed with this 3 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ 4 5 import { WebVTT } from "resource://gre/modules/vtt.sys.mjs"; 6 7 export function WebVTTParserWrapper() { 8 // Nothing 9 } 10 11 WebVTTParserWrapper.prototype = { 12 loadParser(window) { 13 this.parser = new WebVTT.Parser(window, new TextDecoder("utf8")); 14 }, 15 16 parse(data) { 17 // We can safely translate the string data to a Uint8Array as we are 18 // guaranteed character codes only from \u0000 => \u00ff 19 var buffer = new Uint8Array(data.length); 20 for (var i = 0; i < data.length; i++) { 21 buffer[i] = data.charCodeAt(i); 22 } 23 24 this.parser.parse(buffer); 25 }, 26 27 flush() { 28 this.parser.flush(); 29 }, 30 31 watch(callback) { 32 this.parser.oncue = callback.onCue; 33 this.parser.onregion = callback.onRegion; 34 this.parser.onparsingerror = function (e) { 35 // Passing the just the error code back is enough for our needs. 36 callback.onParsingError("code" in e ? e.code : -1); 37 }; 38 }, 39 40 cancel() { 41 this.parser.oncue = null; 42 this.parser.onregion = null; 43 this.parser.onparsingerror = null; 44 }, 45 46 convertCueToDOMTree(window, cue) { 47 return WebVTT.convertCueToDOMTree(window, cue.text); 48 }, 49 50 processCues(window, cues, overlay, controls) { 51 WebVTT.processCues(window, cues, overlay, controls); 52 }, 53 54 classDescription: "Wrapper for the JS WebVTT implementation (vtt.js)", 55 QueryInterface: ChromeUtils.generateQI(["nsIWebVTTParserWrapper"]), 56 };