添加目录读取失败时的报错

This commit is contained in:
fofolee 2019-04-28 19:00:08 +08:00
parent 3eb65d3bf4
commit a895309fba

View File

@ -51,7 +51,7 @@ showList = (text, index, listnum) => {
} }
// 显示手册 // 显示手册
showManual = path => { showManual = async path => {
utools.setExpendHeight(550); utools.setExpendHeight(550);
if (/^((ht|f)tps?):\/\//.test(path)) { if (/^((ht|f)tps?):\/\//.test(path)) {
window.open(path); window.open(path);
@ -64,23 +64,22 @@ showManual = path => {
var f = p[0] var f = p[0]
} }
var file = `${window.dirs.docPath}/${f}`; var file = `${window.dirs.docPath}/${f}`;
$.get(file, data => { try {
if (data) { var data = await readFile(file);
$("#mainlist").fadeOut(); $("#mainlist").fadeOut();
$("#manual").fadeOut().promise().done(() => { $("#manual").fadeOut().promise().done(() => {
if (window.dirs.docPath != 'docs') { var relPath = f.substr(0, f.lastIndexOf('/') + 1),
var filePath = file.substr(0, file.lastIndexOf('/') + 1); absPath = window.dirs.docPath + relPath;
} else { data = data.replace(/(a.*?)href="(?!http)(.*?)"(.*?)(?!\#)/g, `$1href="${relPath}$2$3"`);
var filePath = f.substr(0, f.lastIndexOf('/') + 1); data = data.replace(/(link.*?)href="(?!http)(.*?)"(.*?)(?!\#)/g, `$1href="${absPath}$2$3"`);
} data = data.replace(/src="(?!http)(.*?)"/g, `src="${absPath}$1"`);
data = data.replace(/href="(?!http)(.*?)"(.*?)(?!\#)/g, `href="${filePath}$1$2"`); $("#manual").html(`<div id="manualHead">${data}</div>`).fadeIn();
data = data.replace(/src="(?!http)(.*?)"/g, `src="${filePath}$1"`); Prism.highlightAll();
$("#manual").html(`<div id="manualHead">${data}</div>`).fadeIn(); location.href = p.length == 2 ? id : '#manualHead';
Prism.highlightAll(); })
location.href = p.length == 2 ? id : '#manualHead'; } catch(e) {
}) console.log(e)
} }
})
} }
} }
@ -109,15 +108,16 @@ init = () => {
checkUpdate = () => { checkUpdate = () => {
let cv = 'v0.0.2', let cv = 'v0.0.2',
pg = 'https://yuanliao.info/d/356'; pg = 'https://yuanliao.info/d/356';
if (localStorage[cv] != 'pass') { if (!utools.db.get(cv)) {
$.get(pg, data => { $.get(pg, data => {
data = /<title>\[插件\]\[程序员手册 (.*?)\](.*?) - 猿料<\/title>/.exec(data); data = /<title>\[插件\]\[程序员手册 (.*?)\](.*?) - 猿料<\/title>/.exec(data);
let lv = data[1], let lv = data[1],
desc = data[2]; desc = data[2];
if (lv != cv) { if (lv != cv) {
options = { options = {
type: 'info', type: 'info',
title: '可用更新', title: '插件有可用更新',
icon: window.getLogo(),
cancelId: 1, cancelId: 1,
message: `发现新版本 ${lv},是否前往更新?\n更新内容:\n${desc}`, message: `发现新版本 ${lv},是否前往更新?\n更新内容:\n${desc}`,
buttons: ['起驾', '朕知道了', '别再烦朕'] buttons: ['起驾', '朕知道了', '别再烦朕']
@ -126,7 +126,7 @@ checkUpdate = () => {
if (index == 0) { if (index == 0) {
window.open(pg) window.open(pg)
} else if (index == 2) { } else if (index == 2) {
localStorage[cv] = 'pass'; utools.db.put({ _id: cv, data: "pass" })
} }
}) })
} }
@ -153,7 +153,7 @@ loadList = listnum => {
} }
// 进入插件 // 进入插件
utools.onPluginEnter(({ code, type, payload }) => { utools.onPluginEnter( async ({ code, type, payload }) => {
init(); init();
checkUpdate(); checkUpdate();
if (code == 'options') { if (code == 'options') {
@ -168,8 +168,8 @@ utools.onPluginEnter(({ code, type, payload }) => {
baseDir = getDirname(); baseDir = getDirname();
css = `${baseDir}/assets/${code}.css` css = `${baseDir}/assets/${code}.css`
window.dirs = { window.dirs = {
idxFile: `index/${code}.json`, idxFile: `${baseDir}/index/${code}.json`,
docPath: `docs`, docPath: `${baseDir}/docs`,
} }
break; break;
case "custom": case "custom":
@ -180,20 +180,14 @@ utools.onPluginEnter(({ code, type, payload }) => {
docPath: `${baseDir}`, docPath: `${baseDir}`,
} }
break; break;
case "dash":
baseDir = allFts[code].path;
window.dirs = {
idxFile: `${baseDir}/${code}.json`,
docPath: `${baseDir}/Documents`,
}
break;
} }
if (window.exists(css)) { if (window.exists(css)) {
$("#manualCSS").attr("href", css) $("#manualCSS").attr("href", css)
} }
// 读取目录文件 // 读取目录文件
$.get(window.dirs.idxFile, data => { try {
let index = JSON.parse(data); var index = await readFile(window.dirs.idxFile);
index = JSON.parse(index);
if (type == 'over') { if (type == 'over') {
showList(payload, index, 500) showList(payload, index, 500)
} else { } else {
@ -207,7 +201,9 @@ utools.onPluginEnter(({ code, type, payload }) => {
highlightManual(text); highlightManual(text);
} }
}, '输入名称或功能进行查询'); }, '输入名称或功能进行查询');
}); } catch(e) {
document.write(e);
}
} }
}); });