From 409e5f1ddeae7eb9a0d2750b7624e8a60fea52a7 Mon Sep 17 00:00:00 2001 From: yutent Date: Tue, 5 Sep 2023 18:52:29 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BF=AE=E5=A4=8D=E4=B8=80=E5=A4=84=E5=BC=82?= =?UTF-8?q?=E6=AD=A5=E9=80=BB=E8=BE=91?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- usr/lib/hosts-switch/webapp/utils/index.js | 17 ++++++++++------- 1 file changed, 10 insertions(+), 7 deletions(-) diff --git a/usr/lib/hosts-switch/webapp/utils/index.js b/usr/lib/hosts-switch/webapp/utils/index.js index 0aa86b9..ceb8c1e 100644 --- a/usr/lib/hosts-switch/webapp/utils/index.js +++ b/usr/lib/hosts-switch/webapp/utils/index.js @@ -20,14 +20,15 @@ export function checkPermission() { export async function getHistory() { if (await native.fs.isfile(LOCK_FILE)) { - var cache = await native.fs.read(HOST_FILE) + let cache = await native.fs.read(HOST_FILE) return JSON.parse(cache) } - var cache = native.fs.read('/etc/hosts').toString() - var records = cache.split(/[\n\r]+/) - var list = [] - var dict = {} + let cache = await native.fs.read('/etc/hosts').toString() + let records = cache.split(/[\n\r]+/) + let list = [] + let dict = {} + records.forEach(str => { str = str.trim() let matches = str.match(/^(#*?)\s*(\d+\.\d+\.\d+\.\d+)\s+(.*)/) @@ -68,8 +69,10 @@ export async function getHistory() { } }) list = null - native.fs.write(HOST_FILE, JSON.stringify(dict)) - native.fs.write(LOCK_FILE, '') + try { + await native.fs.write(HOST_FILE, JSON.stringify(dict)) + await native.fs.write(LOCK_FILE, '') + } catch (err) {} return dict }