/* global api */ class twtw_moedict { constructor(options) { this.options = options; this.maxexample = 2; this.word = ''; } async displayName() { let locale = await api.locale(); if (locale.indexOf('CN') != -1) return '台语萌典(繁体)'; if (locale.indexOf('TW') != -1) return '臺語萌典(繁體)'; return 'Moedict TW->TW Dictionary (TC)'; } setOptions(options) { this.options = options; this.maxexample = options.maxexample; } async findTerm(word) { this.word = word; let promises = [this.findMoedict(word), this.findSuisiann(word)]; let results = await Promise.all(promises); return [].concat(...results).filter(x => x); } async findMoedict(word) { let notes = []; if (!word) return notes; // return empty notes function T(node) { if (!node) return ''; else return node.innerText.trim(); } let base = "https://www.moedict.tw/'"; let url = base + encodeURIComponent(word); let doc = ''; try { let data = await api.fetch(url); let parser = new DOMParser(); doc = parser.parseFromString(data, 'text/html'); } catch (err) { return []; } let entries = doc.querySelectorAll('#result>div>div:not(.xrefs)') || []; //console.log("-----萌典 debug start"); for (const entry of entries) { let definitions = []; let audios = []; let expression = '' ; let allWords = entry.querySelectorAll('h1 rb'); for(const innerWords of allWords){ expression += T(innerWords); } let reading = ''; let readings = entry.querySelectorAll('h1 rt'); reading = T(readings[0]); // 其它發音 let alternatives = entry.querySelectorAll('h1>small.alternative>.pinyin'); for(const innerWords of alternatives){ reading += ", " + T(innerWords); } /* if (readings) { let reading_uk = T(readings[0]); let reading_us = T(readings[1]); reading = (reading_uk || reading_us) ? `UK[${reading_uk}] US[${reading_us}] ` : ''; } */ /* audio */ let audioMeta = entry.querySelectorAll("h1>i>meta"); audios[0] = audioMeta[1].getAttribute("content"); // audios[0] = entry.querySelector("h1>i>meta"); // audios[0] = audios[0] ? 'https://dictionary.cambridge.org' + audios[0].getAttribute('src') : ''; // audios[0] = audios[0].replace('https', 'http'); // audios[1] = entry.querySelector(".us.dpron-i source"); // audios[1] = audios[1] ? 'https://dictionary.cambridge.org' + audios[1].getAttribute('src') : ''; //audios[1] = audios[1].replace('https', 'http'); let entryItem = entry.querySelectorAll('.entry-item') || []; for (const ent of entryItem) { let definition = ''; // 同音不同詞性部份 let partOfSpeech = ent.querySelectorAll('.part-of-speech') ; let pos = T(partOfSpeech[0]); pos = pos ? `${pos}
` : ''; definition += pos; //console.log(T(partOfSpeech[0])); let moeDefines = ent.querySelectorAll('.definition') ; for (const moeDefine of moeDefines){ //console.log(moeDefine); let defExp = moeDefine.querySelectorAll('.def') ; let eng_tran = T(defExp[0]); let tran = `${eng_tran}` definition += tran ; let examples = moeDefine.querySelectorAll('.example') ; if ( examples.length > 0 ){ definition += ''; } } definition && definitions.push(definition); /* //let sensblocks = sensbody.childNodes || []; for (const sensblock of sensblocks) { let phrasehead = ''; let defblocks = []; if (sensblock.classList && sensblock.classList.contains('phrase-block')) { phrasehead = T(sensblock.querySelector('.phrase-title')); phrasehead = phrasehead ? `
${phrasehead}
` : ''; defblocks = sensblock.querySelectorAll('.def-block') || []; } if (sensblock.classList && sensblock.classList.contains('def-block')) { defblocks = [sensblock]; } if (defblocks.length <= 0) continue; // make definition segement for (const defblock of defblocks) { let eng_tran = T(defblock.querySelector('.ddef_h .def')); let chn_tran = T(defblock.querySelector('.def-body .trans')); if (!eng_tran) continue; eng_tran = `${eng_tran.replace(RegExp(expression, 'gi'),`${expression}`)}`; chn_tran = `${chn_tran}`; let tran = `${eng_tran}${chn_tran}`; definition += phrasehead ? `${phrasehead}${tran}` : `${pos}${tran}`; // make exmaple segement let examps = defblock.querySelectorAll('.def-body .examp') || []; if (examps.length > 0 && this.maxexample > 0) { definition += ''; } definition && definitions.push(definition); } } */ } let css = this.renderCSS(); notes.push({ css, expression, reading, definitions, audios }); } return notes; } async findSuisiann(word) { if (!word) return []; function T(node) { if (!node) return ''; else return node.innerText.trim(); } function unicodeToChar(text) { return text.replace(/\\u[\dA-F]{4}/gi, function (match) { return String.fromCharCode(parseInt(match.replace(/\\u/g, ''), 16)); }); } let romanBase = "https://hts.ithuan.tw/%E7%BE%85%E9%A6%AC%E5%AD%97%E8%BD%89%E6%8F%9B?%E6%9F%A5%E8%A9%A2%E8%AA%9E%E5%8F%A5="; let romanUrl = romanBase + encodeURIComponent(word); let base = 'https://suisiann.ithuan.tw/%E8%AC%9B/'; let url = base + encodeURIComponent(word); let doc = ''; let parsed = ''; try { let dataRoman = await api.fetch(romanUrl); let parser = new DOMParser(); let decodedData = unicodeToChar(dataRoman); //console.log(decodedData); parsed = JSON.parse(decodedData); //let data = await api.fetch(url); //doc = parser.parseFromString(data, 'text/html'); } catch (err) { return []; } let notes = []; //get headword and phonetic let expression = word; //headword let reading = "" ; if ( parsed["臺羅"] == word ){ reading += `臺羅`;} if ( parsed["白話字"] == word ){ reading += `白話字`;} reading += `
臺羅${parsed["臺羅"]}
白話字${parsed["白話字"]}`; let audios = []; audios[0] = `https://hapsing.ithuan.tw/bangtsam?taibun=${encodeURIComponent(parsed["臺羅"])}`; //audios[1] = `http://dict.youdao.com/dictvoice?audio=${encodeURIComponent(expression)}&type=2`; // let definition = ''; let css = ` `; //let css = this.renderCSS(); notes.push({ css, expression, reading, definitions: [''],//definition], audios }); return notes; } /* async findYoudao(word) { if (!word) return []; let base = 'http://dict.youdao.com/w/'; let url = base + encodeURIComponent(word); let doc = ''; try { let data = await api.fetch(url); let parser = new DOMParser(); doc = parser.parseFromString(data, 'text/html'); let youdao = getYoudao(doc); //Combine Youdao Concise English-Chinese Dictionary to the end. let ydtrans = getYDTrans(doc); //Combine Youdao Translation (if any) to the end. return [].concat(youdao, ydtrans); } catch (err) { return []; } function getYoudao(doc) { let notes = []; //get Youdao EC data: check data availability let defNodes = doc.querySelectorAll('#phrsListTab .trans-container ul li'); if (!defNodes || !defNodes.length) return notes; //get headword and phonetic let expression = T(doc.querySelector('#phrsListTab .wordbook-js .keyword')); //headword let reading = ''; let readings = doc.querySelectorAll('#phrsListTab .wordbook-js .pronounce'); if (readings) { let reading_uk = T(readings[0]); let reading_us = T(readings[1]); reading = (reading_uk || reading_us) ? `${reading_uk} ${reading_us}` : ''; } let audios = []; audios[0] = `http://dict.youdao.com/dictvoice?audio=${encodeURIComponent(expression)}&type=1`; audios[1] = `http://dict.youdao.com/dictvoice?audio=${encodeURIComponent(expression)}&type=2`; let definition = ''; let css = ` `; notes.push({ css, expression, reading, definitions: [definition], audios }); return notes; } function getYDTrans(doc) { let notes = []; //get Youdao EC data: check data availability let transNode = doc.querySelectorAll('#ydTrans .trans-container p')[1]; if (!transNode) return notes; let definition = `${T(transNode)}`; let css = ` `; notes.push({ css, definitions: [definition], }); return notes; } function T(node) { if (!node) return ''; else return node.innerText.trim(); } } */ renderCSS() { let css = ` `; return css; } }