add charset to config.html

This commit is contained in:
James Feng Cao 2023-09-15 15:42:00 +08:00
parent 437fc7a2b4
commit 1fb143ab1b
4 changed files with 101 additions and 207 deletions

View file

@ -0,0 +1,99 @@
<head>
<base>
<script src="https://fastly.jsdelivr.net/npm/marked@3.0.7/marked.min.js"></script>
<script>
function absFile(url) {
this.name=url;
this.slice = async(offset, length) =>{
let resp = new Promise(function (resolve, reject) {
let r=new XMLHttpRequest;
r.onload=function(){
if (r.status == 200)
resolve(r.response);
else
reject(r.statusText);
}
r.onerror = () => reject(r.statusText);
r.open("GET",this.name,!0);
r.setRequestHeader('range', 'bytes=' + offset + '-' + ( offset + length -1).toString());
r.send(null);});
return await resp.arrayBuffer();
}
}
marked.setOptions({
gfm: true,
});
var file;
var blocksize = 20480;
function loadSlice(){
function render(u8array){
document.body.innerHTML = marked(new TextDecoder().decode(u8array));
scroll(0,0);
}
function setPageUrl(pageurl){
let a = document.createElement('a');
a.href = "i:5c"+pageurl;
a.click();
}
var page = 1;
var extrasize = blocksize; //extrasize should <= blocksize
let lhash = location.hash;
if(lhash){
if("p"!==lhash.charAt(1)){//#offset=&[len]
let iAmpersand = lhash.indexOf('&',8);
let offset = parseInt(lhash.substring(8,iAmpersand));
let len = parseInt(lhash.substring(iAmpersand+1));
{//update urlInc() url
let href = location.href;
let iHash = href.indexOf('#');
href=href.substring(0,iHash)+"#page="+Math.floor((offset+len)/blocksize);
setPageUrl(href);
}
file.slice(offset,len).then((buffer)=>{
let u8 = new Uint8Array(buffer);
render(u8);
});
return;
}
//#page=
page = parseInt(lhash.substring(6));
}else
setPageUrl(location.href+"#page=1");
file.slice(blocksize*(page-1),blocksize+extrasize).then((buffer)=>{
let u8 = new Uint8Array(buffer);
let u8len = u8.length;
let iStart = 0;
let limit = Math.min(u8len,extrasize);
if(1!=page){
for(let i=1;i<limit;i=i+2){
if(10==u8[i] && 10==u8[i-1]) {
iStart = i+1;
break;
}
}
}
let iEnd = Math.min(u8len,blocksize);
limit = Math.min(u8len,blocksize+extrasize);
for(let i=blocksize+1;i<limit;i=i+2){
if(10==u8[i] && 10==u8[i-1]) {
iEnd = i+1;
break;
}
}
render(u8.slice(iStart,iEnd));
});
}
//#offset=&[len]&page=
{
let url = location.href;
file = new absFile(url);
}
window.addEventListener('hashchange',loadSlice);
</script>
</head>
<body>
<script>loadSlice();</script>
</body>

View file

@ -1,205 +0,0 @@
<!DOCTYPE html>
<html lang="en-us">
<head>
<meta charset="UTF-8">
<title>mdict</title>
<meta name="viewport" content="width=device-width, initial-scale=1">
<link rel="stylesheet" type="text/css">
<link rel="stylesheet" type="text/css" href="https://cdn.jsdelivr.net/gh/torappinfo/uweb/en/searchurl/mdict/selectize.default.css" media="screen">
<style>
#btnLookup {
border: none;
height: 36px;
font-size: 12pt;
font-weight: bold;
vertical-align: top;
border-radius: 3px;
}
#btnLookup:not([disabled]) {
background: #1A4FDD;
color: white;
}
#word + .selectize-control {
display: inline-block;
min-width: 18em;
}
</style>
<script type="text/javascript" src="https://cdn.jsdelivr.net/npm/jquery@3.6.1/dist/jquery.min.js"></script>
<script type="text/javascript" src="https://cdn.jsdelivr.net/gh/fengdh/mdict-js/selectize.min.js"></script>
<script type="text/javascript" src="https://cdn.jsdelivr.net/gh/nodeca/pako/dist/pako_inflate.min.js"></script>
<script type="text/javascript" src="https://cdn.jsdelivr.net/npm/bluebird@3.7.2/js/browser/bluebird.min.js"></script>
<script type="text/javascript" src="https://cdn.jsdelivr.net/gh/torappinfo/uweb/en/searchurl/mdict/ripemd128.min.js"></script>
<script type="text/javascript" src="https://cdn.jsdelivr.net/gh/torappinfo/uweb/en/searchurl/mdict/mdict-common.min.js"></script>
<script type="text/javascript" src="https://cdn.jsdelivr.net/gh/torappinfo/uweb/en/searchurl/mdict/mdict-parser.min.js"></script>
<script type="text/javascript" src="https://cdn.jsdelivr.net/gh/torappinfo/uweb/en/searchurl/mdict/mdict-renderer.min.js"></script>
</head>
<body>
Choose a dictionary file (*.mdx + optional *.mdd): <input id="dictfile" type="file" multiple>
<p>
<input id="word" type="text" value="">
<input id="btnLookup" type="button" value="look up" disabled="false">
<div id="definition">
</div>
<script>
//file:///...mdictstem?app=....html#word=
var dictinput = document.getElementById('dictfile');
var wordinput = document.getElementById('word');
var btnLookup = document.getElementById('btnLookup');
var definition = document.getElementById('definition');
window.onhashchange = function() {
let v = location.hash.substring(6);
if(v.length>0){
wordinput.value = v;
//wordinput.dispatchEvent(new Event('change', {'bubbles': true}));
btnLookup.click();
}
};
{
let v = decodeURIComponent(location.hash.substring(6));
wordinput.value = v;
}
$(wordinput).selectize({maxItems: 1});
function accept(e) {
var fileList = $(e.target).prop('files');
$(btnLookup).attr('disabled', true);
if (fileList.length > 0) {
$(btnLookup).addClass('stripes');
$(wordinput).on('keyup', function(e) { e.which === 13
&& $(btnLookup).click(); });
MParser(fileList).then(function(resources) {
var mdict = MRenderer(resources);
function doSearch(phrase, offset) {
console.log(phrase + '');
mdict.lookup(phrase, offset).then(function($content) {
$(definition).empty().append($content.contents());
console.log('--');
});
}
$(btnLookup)
.attr('disabled', false)
.off('.#mdict')
.on('click.#mdict', function() {
doSearch($(wordinput).val());
}).click();
$(wordinput)[0].selectize.destroy();
$(wordinput).selectize({
plugins: ['restore_on_backspace'],
maxItems: 1,
maxOptions: 1 << 20,
valueField: 'value',
labelField: 'word',
searchField: 'word',
delimiter: '~~',
loadThrottle: 10,
create: function(v, callback) {
return callback({word: v, value: v});
},
createOnBlur: true,
closeAfterSelect: true,
allowEmptyOption: true,
score: function(search) {
var score =
this.getScoreFunction(search);
return function(item) {
return 1;
};
},
load: function(query, callback) {
var self = this;
if (!query.length) {
this.clearOptions();
this.refreshOptions();
return;
};
mdict.search({phrase: query, max: 5000}).then(function(list) {
// console.log(list.join(', '));
// TODO: filter candidate keyword starting with "_"
list = list.map(function(v) {
return {word: v, value: v.offset};
});
self.clearOptions();
callback(list);
});
},
onChange: function(value) {
var item = this.options[value];
if (item) {
var value = item.word;
doSearch(value, value.offset);
$(wordinput).val(value);
} else {
$(definition).empty();
}
},
});
}).catch(err => alert(err)) ;
} else {
$(btnLookup).attr('disabled', false);
}
// jump to word with link started with "entry://"
// TODO: have to ignore in-page jump
$(definition).on('click', 'a', function(e) {
var href = $(this).attr('href');
if (href && href.substring(0, 8) === 'entry://') {
var word = href.substring(8);
// TODO: remove '#' to get jump target
if (word.charAt(0) !== '#') {
word = word.replace(/(^[/\\])|([/]$)/, '');
$(wordinput).val(word);
$(btnLookup).click();
} else {
var currentUrl = location.href;
location.href = word; //Go to the target element.
history.replaceState(null,null,currentUrl);
}
return false;
}
});
}
$(dictinput).on('change', accept);
{
let path = location.pathname;
let iSlash = path.lastIndexOf('/');
let filenames=path.substring(iSlash+1);
{
let iDot = filenames.indexOf('.');
document.getElementsByTagName("link")[0].href =
filenames.substring(0,iDot+1)+"css";
}
window.onload = function(){
if(filenames.length>0){
let clickurl = "i:5fdictinput.click():"+filenames;
location.href=clickurl;
var callbackTimer = setInterval(function() {
let files = dictinput.files;
if(files.length>0){
if(!btnLookup.disabled){
clearInterval(callbackTimer);
return;
}
}else
location.href=clickurl;
dictinput.dispatchEvent(new Event('change', {'bubbles': true}));
}, 100);
}
};
}
</script>
</body>
</html>

View file

@ -17,5 +17,5 @@
<script src="config.js" charset="utf-8"></script> <html><head><meta charset="utf-8"></head><script src="config.js" charset="utf-8"></script>

View file

@ -1,6 +1,6 @@
Serif