commit 6cfcaddadfde7bd6eb9bcceae2de6d340a188c1c
parent 4d11a2ce2d4e542e5c0ce444e6230b41623a1dce
Author: Sheng <webmaster0115@gmail.com>
Date: Thu, 23 Aug 2018 21:22:14 +0800
Defined a function read_file_as_text
Diffstat:
1 file changed, 39 insertions(+), 19 deletions(-)
diff --git a/webssh/static/js/main.js b/webssh/static/js/main.js
@@ -66,6 +66,34 @@ jQuery(function($){
}
+ function read_file_as_text(file, callback, decoder) {
+ var reader = new window.FileReader();
+
+ if (decoder === undefined) {
+ decoder = new window.TextDecoder('utf-8', {'fatal': true});
+ }
+
+ reader.onload = function () {
+ var text;
+ try {
+ text = decoder.decode(reader.result);
+ } catch (TypeError) {
+ console.error('Decoding error happened.');
+ } finally {
+ if (callback) {
+ callback(text);
+ }
+ }
+ };
+
+ reader.onerror = function (e) {
+ console.error(e);
+ };
+
+ reader.readAsArrayBuffer(file);
+ }
+
+
function reset_wssh() {
var name;
@@ -112,6 +140,16 @@ jQuery(function($){
term.on_resize(geometry.cols, geometry.rows);
}
+ function term_write(text) {
+ if (term) {
+ term.write(text);
+ if (!term.resized) {
+ resize_terminal(term);
+ term.resized = true;
+ }
+ }
+ }
+
function set_encoding(new_encoding) {
// for console use
if (new_encoding === undefined) {
@@ -211,25 +249,7 @@ jQuery(function($){
};
sock.onmessage = function(msg) {
- var reader = new window.FileReader();
-
- reader.onload = function() {
- var text = decoder.decode(reader.result);
- // console.log(text);
- if (term) {
- term.write(text);
- if (!term.resized) {
- resize_terminal(term);
- term.resized = true;
- }
- }
- };
-
- reader.onerror = function(e) {
- console.error(e);
- };
-
- reader.readAsArrayBuffer(msg.data);
+ read_file_as_text(msg.data, term_write, decoder);
};
sock.onerror = function(e) {