From de8afbb6ced11b44ece417a84c1858a7089913a2 Mon Sep 17 00:00:00 2001 From: NI Date: Sun, 15 Sep 2019 07:34:24 +0800 Subject: [PATCH] Programmatically check which encoding is available. --- ui/commands/common.js | 26 +++++++++++++++++++++++--- 1 file changed, 23 insertions(+), 3 deletions(-) diff --git a/ui/commands/common.js b/ui/commands/common.js index b08067e..627e0c2 100644 --- a/ui/commands/common.js +++ b/ui/commands/common.js @@ -15,9 +15,11 @@ // You should have received a copy of the GNU Affero General Public License // along with this program. If not, see . +import * as iconv from "iconv-lite"; + import Exception from "./exception.js"; -export const charsetPresets = [ +const availableEncodings = [ "utf-8", "ibm866", "iso-8859-2", @@ -27,7 +29,6 @@ export const charsetPresets = [ "iso-8859-6", "iso-8859-7", "iso-8859-8", - "iso-8859-8i", "iso-8859-10", "iso-8859-13", "iso-8859-14", @@ -52,11 +53,30 @@ export const charsetPresets = [ "euc-jp", "shift-jis", "euc-kr", - "iso-2022-kr", "utf-16be", "utf-16le" ]; +export const charsetPresets = (() => { + let r = []; + + for (let i in availableEncodings) { + try { + if (!iconv.encodingExists(availableEncodings[i])) { + continue; + } + + new TextDecoder(availableEncodings[i]); + + r.push(availableEncodings[i]); + } catch (e) { + // Do nothing + } + } + + return r; +})(); + const numCharators = { "0": true, "1": true,