mirror of
https://github.com/fofolee/uTools-Manuals.git
synced 2025-06-08 06:55:36 +08:00
添加目录读取失败时的报错
This commit is contained in:
parent
3eb65d3bf4
commit
a895309fba
620
assets/index.js
620
assets/index.js
@ -1,312 +1,308 @@
|
|||||||
// 正则转义
|
// 正则转义
|
||||||
RegExp.escape = function (s) {
|
RegExp.escape = function (s) {
|
||||||
return s.replace(/[-\/\\^$*+?.()|[\]{}]/g, '\\$&');
|
return s.replace(/[-\/\\^$*+?.()|[\]{}]/g, '\\$&');
|
||||||
};
|
};
|
||||||
|
|
||||||
// 列表
|
// 列表
|
||||||
list = (path, initial, type, name ,desc) => {
|
list = (path, initial, type, name ,desc) => {
|
||||||
return `<div class='info' path='${path}'>
|
return `<div class='info' path='${path}'>
|
||||||
<div class="initial">${initial}</div>
|
<div class="initial">${initial}</div>
|
||||||
<div class="type">${type}</div>
|
<div class="type">${type}</div>
|
||||||
<div class="name">${name}</div>
|
<div class="name">${name}</div>
|
||||||
<div class="description">${desc}</div>
|
<div class="description">${desc}</div>
|
||||||
</div>`
|
</div>`
|
||||||
}
|
}
|
||||||
|
|
||||||
// 显示列表
|
// 显示列表
|
||||||
showList = (text, index, listnum) => {
|
showList = (text, index, listnum) => {
|
||||||
window.info = [];
|
window.info = [];
|
||||||
var obn = [];
|
var obn = [];
|
||||||
var obd = [];
|
var obd = [];
|
||||||
index.forEach(i => {
|
index.forEach(i => {
|
||||||
let name = i.name,
|
let name = i.name,
|
||||||
desc = i.desc != undefined ? i.desc : "",
|
desc = i.desc != undefined ? i.desc : "",
|
||||||
initial = name.slice(0, 1).toUpperCase(),
|
initial = name.slice(0, 1).toUpperCase(),
|
||||||
reg = new RegExp(RegExp.escape(text), "i"),
|
reg = new RegExp(RegExp.escape(text), "i"),
|
||||||
match1 = reg.exec(name),
|
match1 = reg.exec(name),
|
||||||
match2 = reg.exec(desc);
|
match2 = reg.exec(desc);
|
||||||
// 优先显示名称匹配的内容
|
// 优先显示名称匹配的内容
|
||||||
if (match1) {
|
if (match1) {
|
||||||
name = highlightList(name, match1[0]);
|
name = highlightList(name, match1[0]);
|
||||||
if (match2) desc = highlightList(desc, match2[0]);
|
if (match2) desc = highlightList(desc, match2[0]);
|
||||||
// 置顶全字匹配的内容
|
// 置顶全字匹配的内容
|
||||||
if (i.name.toUpperCase() == text.toUpperCase()) {
|
if (i.name.toUpperCase() == text.toUpperCase()) {
|
||||||
obn.unshift(list(i.path, initial, i.type, name, desc));
|
obn.unshift(list(i.path, initial, i.type, name, desc));
|
||||||
} else {
|
} else {
|
||||||
obn.push(list(i.path, initial, i.type, name, desc));
|
obn.push(list(i.path, initial, i.type, name, desc));
|
||||||
}
|
}
|
||||||
// 其次显示描述匹配的内容
|
// 其次显示描述匹配的内容
|
||||||
} else if (match2) {
|
} else if (match2) {
|
||||||
desc = highlightList(desc, match2[0]);
|
desc = highlightList(desc, match2[0]);
|
||||||
obd.push(list(i.path, initial, i.type, name, desc));
|
obd.push(list(i.path, initial, i.type, name, desc));
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
window.info = obn.concat(obd);
|
window.info = obn.concat(obd);
|
||||||
$("#mainlist").html(window.info.slice(0, listnum).join(''));
|
$("#mainlist").html(window.info.slice(0, listnum).join(''));
|
||||||
let num = $(".info").length
|
let num = $(".info").length
|
||||||
utools.setExpendHeight(num > 11 ? 550 : 50 * num);
|
utools.setExpendHeight(num > 11 ? 550 : 50 * num);
|
||||||
$(".select").removeClass('select');
|
$(".select").removeClass('select');
|
||||||
$(".info:first").addClass('select');
|
$(".info:first").addClass('select');
|
||||||
window.mouseLockTime = new Date().getTime();
|
window.mouseLockTime = new Date().getTime();
|
||||||
}
|
}
|
||||||
|
|
||||||
// 显示手册
|
// 显示手册
|
||||||
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);
|
||||||
} else {
|
} else {
|
||||||
let p = path.split('.html#')
|
let p = path.split('.html#')
|
||||||
if (p.length == 2) {
|
if (p.length == 2) {
|
||||||
var f = p[0] + '.html';
|
var f = p[0] + '.html';
|
||||||
var id = '#' + p[1];
|
var id = '#' + p[1];
|
||||||
} else {
|
} else {
|
||||||
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)
|
||||||
}
|
}
|
||||||
})
|
}
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
// 手册搜索结果高亮
|
||||||
// 手册搜索结果高亮
|
highlightManual = text => {
|
||||||
highlightManual = text => {
|
$("#manual").removeHighlight() ;
|
||||||
$("#manual").removeHighlight() ;
|
if (text) {
|
||||||
if (text) {
|
$("#manual").highlight(text);
|
||||||
$("#manual").highlight(text);
|
window.findex = 0;
|
||||||
window.findex = 0;
|
}
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
// 列表搜索结果高亮
|
||||||
// 列表搜索结果高亮
|
highlightList = (string, match) => string.replace(match, `<span style="color:#ff5722">${match}</span>`)
|
||||||
highlightList = (string, match) => string.replace(match, `<span style="color:#ff5722">${match}</span>`)
|
|
||||||
|
// 初始化
|
||||||
// 初始化
|
init = () => {
|
||||||
init = () => {
|
$("#mainlist").fadeOut(0);
|
||||||
$("#mainlist").fadeOut(0);
|
$("#options").fadeOut(0);
|
||||||
$("#options").fadeOut(0);
|
$("#manual").fadeOut(0);
|
||||||
$("#manual").fadeOut(0);
|
$("html").niceScroll();
|
||||||
$("html").niceScroll();
|
$("#manual").niceScroll();
|
||||||
$("#manual").niceScroll();
|
}
|
||||||
}
|
|
||||||
|
// 检查升级
|
||||||
// 检查升级
|
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 (!utools.db.get(cv)) {
|
||||||
if (localStorage[cv] != 'pass') {
|
$.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: ['起驾', '朕知道了', '别再烦朕']
|
||||||
};
|
};
|
||||||
window.messageBox(options, index => {
|
window.messageBox(options, index => {
|
||||||
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" })
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// 切换列表和手册视图
|
// 切换列表和手册视图
|
||||||
toggleView = () => {
|
toggleView = () => {
|
||||||
if ($("#manual").is(":hidden") && $("#mainlist").is(":visible")) {
|
if ($("#manual").is(":hidden") && $("#mainlist").is(":visible")) {
|
||||||
$("#manual").fadeIn();
|
$("#manual").fadeIn();
|
||||||
$("#mainlist").fadeOut();
|
$("#mainlist").fadeOut();
|
||||||
} else if ($("#manual").is(":visible") && $("#mainlist").is(":hidden")) {
|
} else if ($("#manual").is(":visible") && $("#mainlist").is(":hidden")) {
|
||||||
$("#manual").fadeOut();
|
$("#manual").fadeOut();
|
||||||
$("#mainlist").fadeIn();
|
$("#mainlist").fadeIn();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// 加载剩余列表
|
// 加载剩余列表
|
||||||
loadList = listnum => {
|
loadList = listnum => {
|
||||||
if ($(window).scrollTop() >= (listnum * 50 - 550) && $(".info").length <= listnum) {
|
if ($(window).scrollTop() >= (listnum * 50 - 550) && $(".info").length <= listnum) {
|
||||||
$("#mainlist").append(window.info.slice(listnum).join(''));
|
$("#mainlist").append(window.info.slice(listnum).join(''));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// 进入插件
|
// 进入插件
|
||||||
utools.onPluginEnter(({ code, type, payload }) => {
|
utools.onPluginEnter( async ({ code, type, payload }) => {
|
||||||
init();
|
init();
|
||||||
checkUpdate();
|
checkUpdate();
|
||||||
if (code == 'options') {
|
if (code == 'options') {
|
||||||
showOptions();
|
showOptions();
|
||||||
} else {
|
} else {
|
||||||
$("#mainlist").fadeIn();
|
$("#mainlist").fadeIn();
|
||||||
var allFts = getAllFeatures(),
|
var allFts = getAllFeatures(),
|
||||||
baseDir,
|
baseDir,
|
||||||
css;
|
css;
|
||||||
switch (allFts[code].type) {
|
switch (allFts[code].type) {
|
||||||
case "default":
|
case "default":
|
||||||
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":
|
||||||
baseDir = allFts[code].path;
|
baseDir = allFts[code].path;
|
||||||
css = `${baseDir}/${code}.css`
|
css = `${baseDir}/${code}.css`
|
||||||
window.dirs = {
|
window.dirs = {
|
||||||
idxFile: `${baseDir}/${code}.json`,
|
idxFile: `${baseDir}/${code}.json`,
|
||||||
docPath: `${baseDir}`,
|
docPath: `${baseDir}`,
|
||||||
}
|
}
|
||||||
break;
|
break;
|
||||||
case "dash":
|
}
|
||||||
baseDir = allFts[code].path;
|
if (window.exists(css)) {
|
||||||
window.dirs = {
|
$("#manualCSS").attr("href", css)
|
||||||
idxFile: `${baseDir}/${code}.json`,
|
}
|
||||||
docPath: `${baseDir}/Documents`,
|
// 读取目录文件
|
||||||
}
|
try {
|
||||||
break;
|
var index = await readFile(window.dirs.idxFile);
|
||||||
}
|
index = JSON.parse(index);
|
||||||
if (window.exists(css)) {
|
if (type == 'over') {
|
||||||
$("#manualCSS").attr("href", css)
|
showList(payload, index, 500)
|
||||||
}
|
} else {
|
||||||
// 读取目录文件
|
showList('', index, 500)
|
||||||
$.get(window.dirs.idxFile, data => {
|
}
|
||||||
let index = JSON.parse(data);
|
// 子输入框
|
||||||
if (type == 'over') {
|
utools.setSubInput(({ text }) => {
|
||||||
showList(payload, index, 500)
|
if ($('#manual').is(':hidden')) {
|
||||||
} else {
|
showList(text, index, 500);
|
||||||
showList('', index, 500)
|
} else {
|
||||||
}
|
highlightManual(text);
|
||||||
// 子输入框
|
}
|
||||||
utools.setSubInput(({ text }) => {
|
}, '输入名称或功能进行查询');
|
||||||
if ($('#manual').is(':hidden')) {
|
} catch(e) {
|
||||||
showList(text, index, 500);
|
document.write(e);
|
||||||
} else {
|
}
|
||||||
highlightManual(text);
|
}
|
||||||
}
|
});
|
||||||
}, '输入名称或功能进行查询');
|
|
||||||
});
|
// 单击列表,显示手册
|
||||||
}
|
$("#mainlist").on('mousedown', '.info', function (e) {
|
||||||
});
|
if (1 == e.which) {
|
||||||
|
showManual($(".select").attr('path'));
|
||||||
// 单击列表,显示手册
|
}
|
||||||
$("#mainlist").on('mousedown', '.info', function (e) {
|
});
|
||||||
if (1 == e.which) {
|
|
||||||
showManual($(".select").attr('path'));
|
// 鼠标滑过列表,高亮
|
||||||
}
|
$("#mainlist").on('mousemove', '.info', function () {
|
||||||
});
|
// 设置500ms的鼠标锁
|
||||||
|
var mouseUnlockTime = new Date().getTime();
|
||||||
// 鼠标滑过列表,高亮
|
if (mouseUnlockTime - window.mouseLockTime > 500) {
|
||||||
$("#mainlist").on('mousemove', '.info', function () {
|
$(".select").removeClass('select');
|
||||||
// 设置500ms的鼠标锁
|
$(this).addClass('select');
|
||||||
var mouseUnlockTime = new Date().getTime();
|
}
|
||||||
if (mouseUnlockTime - window.mouseLockTime > 500) {
|
});
|
||||||
$(".select").removeClass('select');
|
|
||||||
$(this).addClass('select');
|
// 右键单击手册,退出手册
|
||||||
}
|
$("#manual").on('mousedown', function (e) {
|
||||||
});
|
if (3 == e.which) {
|
||||||
|
toggleView();
|
||||||
// 右键单击手册,退出手册
|
}
|
||||||
$("#manual").on('mousedown', function (e) {
|
})
|
||||||
if (3 == e.which) {
|
|
||||||
toggleView();
|
// 手册中a标签
|
||||||
}
|
$("#manual").on('mousedown', 'a', function (e) {
|
||||||
})
|
if (1 == e.which) {
|
||||||
|
showManual($(this).attr('href'));
|
||||||
// 手册中a标签
|
}
|
||||||
$("#manual").on('mousedown', 'a', function (e) {
|
});
|
||||||
if (1 == e.which) {
|
|
||||||
showManual($(this).attr('href'));
|
// 滚动到边界加载列表
|
||||||
}
|
$(document).scroll(() => {
|
||||||
});
|
loadList(500);
|
||||||
|
})
|
||||||
// 滚动到边界加载列表
|
|
||||||
$(document).scroll(() => {
|
// 按键监听
|
||||||
loadList(500);
|
$(document).keydown(e => {
|
||||||
})
|
switch (e.keyCode) {
|
||||||
|
// TAB
|
||||||
// 按键监听
|
case 9:
|
||||||
$(document).keydown(e => {
|
toggleView();
|
||||||
switch (e.keyCode) {
|
break;
|
||||||
// TAB
|
// 回车
|
||||||
case 9:
|
case 13:
|
||||||
toggleView();
|
// 列表界面进入手册
|
||||||
break;
|
if ($('#manual').is(':hidden')) {
|
||||||
// 回车
|
showManual($(".select").attr('path'));
|
||||||
case 13:
|
// 手册界面搜索下一个
|
||||||
// 列表界面进入手册
|
} else {
|
||||||
if ($('#manual').is(':hidden')) {
|
if (window.findex > 0) {
|
||||||
showManual($(".select").attr('path'));
|
$(`.founds:eq(${window.findex - 1})`).removeClass('firstFound');
|
||||||
// 手册界面搜索下一个
|
} else {
|
||||||
} else {
|
$('.founds:last').removeClass('firstFound');
|
||||||
if (window.findex > 0) {
|
}
|
||||||
$(`.founds:eq(${window.findex - 1})`).removeClass('firstFound');
|
$(`.founds:eq(${window.findex})`).addClass('firstFound');
|
||||||
} else {
|
$('.firstFound').get(0).scrollIntoView({ behavior: "smooth", block: "nearest" });
|
||||||
$('.founds:last').removeClass('firstFound');
|
if (window.findex == $('.founds').length - 1) {
|
||||||
}
|
window.findex = 0;
|
||||||
$(`.founds:eq(${window.findex})`).addClass('firstFound');
|
} else {
|
||||||
$('.firstFound').get(0).scrollIntoView({ behavior: "smooth", block: "nearest" });
|
window.findex += 1;
|
||||||
if (window.findex == $('.founds').length - 1) {
|
}
|
||||||
window.findex = 0;
|
}
|
||||||
} else {
|
break;
|
||||||
window.findex += 1;
|
// 上
|
||||||
}
|
case 38:
|
||||||
}
|
let pre = $(".select").prev();
|
||||||
break;
|
// 没有到达边界时移动选择条
|
||||||
// 上
|
if(pre.length != 0){
|
||||||
case 38:
|
// event.preventDefault();
|
||||||
let pre = $(".select").prev();
|
if(pre.offset().top < $(window).scrollTop()){
|
||||||
// 没有到达边界时移动选择条
|
$("html").animate({ scrollTop: "-=50" }, 0);
|
||||||
if(pre.length != 0){
|
}
|
||||||
// event.preventDefault();
|
pre.addClass("select");
|
||||||
if(pre.offset().top < $(window).scrollTop()){
|
$(".select:last").removeClass("select");
|
||||||
$("html").animate({ scrollTop: "-=50" }, 0);
|
// 到达边界闪烁移动选择条
|
||||||
}
|
}else{
|
||||||
pre.addClass("select");
|
$(".select").animate({"opacity":"0.3"}).delay(500).animate({"opacity":"1"})
|
||||||
$(".select:last").removeClass("select");
|
}
|
||||||
// 到达边界闪烁移动选择条
|
break;
|
||||||
}else{
|
// 下
|
||||||
$(".select").animate({"opacity":"0.3"}).delay(500).animate({"opacity":"1"})
|
case 40:
|
||||||
}
|
let next = $(".select").next();
|
||||||
break;
|
// 没有到达边界时移动选择条
|
||||||
// 下
|
if(next.length !=0){
|
||||||
case 40:
|
// event.preventDefault();
|
||||||
let next = $(".select").next();
|
if(next.offset().top >= $(window).scrollTop() + 550){
|
||||||
// 没有到达边界时移动选择条
|
$("html").animate({ scrollTop: "+=50" }, 0);
|
||||||
if(next.length !=0){
|
}
|
||||||
// event.preventDefault();
|
loadList(500);
|
||||||
if(next.offset().top >= $(window).scrollTop() + 550){
|
next.addClass("select");
|
||||||
$("html").animate({ scrollTop: "+=50" }, 0);
|
$(".select:first").removeClass("select");
|
||||||
}
|
// 到达边界闪烁移动选择条
|
||||||
loadList(500);
|
}else{
|
||||||
next.addClass("select");
|
$(".select").animate({"opacity":"0.3"}).delay(500).animate({"opacity":"1"})
|
||||||
$(".select:first").removeClass("select");
|
}
|
||||||
// 到达边界闪烁移动选择条
|
break;
|
||||||
}else{
|
}
|
||||||
$(".select").animate({"opacity":"0.3"}).delay(500).animate({"opacity":"1"})
|
});
|
||||||
}
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
});
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user