Skip to content

Instantly share code, notes, and snippets.

@weiyinerzui
Created February 26, 2025 09:10
Show Gist options
  • Save weiyinerzui/1eb53e80ca079bdf83ee5aeed9f9e8a2 to your computer and use it in GitHub Desktop.
Save weiyinerzui/1eb53e80ca079bdf83ee5aeed9f9e8a2 to your computer and use it in GitHub Desktop.
vocabulary 英英词典
class enen_Vocabulary {
constructor(options) {
this.name = "Vocabulary";
this.description = "Vocabulary.com 英英词典";
this.baseUrl = "https://www.vocabulary.com/dictionary/";
this.options = options;
this.maxexample = options?.maxexample || 2;
}
setOptions(options) {
this.options = options;
this.maxexample = options.maxexample;
}
async findTerm(word) {
return new Promise(async (resolve, reject) => {
try {
const url = this.baseUrl + encodeURIComponent(word.toLowerCase());
let data = await fetch(url);
if (!data.ok) {
reject(`Network error: ${data.statusText}`);
return;
}
let html = await data.text();
let parser = new DOMParser();
let doc = parser.parseFromString(html, 'text/html');
// Get the main dictionary content
let mainContent = doc.querySelector('.word-area');
if (!mainContent) {
reject('No definition found');
return;
}
let notes = [];
let expression = word;
let reading = '';
let definitions = [];
// Get short definition
let shortDef = mainContent.querySelector('.short');
if (shortDef) {
let pos = shortDef.querySelector('.pos');
let pos_text = pos ? pos.textContent : '';
let def_text = shortDef.querySelector('.definition')?.textContent || '';
let definition = '';
pos = pos_text ? `<span class="pos">${pos_text}</span>` : '';
let eng_tran = `<span class="eng_tran">${def_text}</span>`;
definition = `${pos}<span class="tran">${eng_tran}</span>`;
// Get examples
let examples = mainContent.querySelectorAll('.example');
if (examples.length > 0) {
definition += '<ul class="sents">';
examples.forEach((ex, idx) => {
if (idx >= this.maxexample) return;
let eng_sent = ex.textContent.replace(RegExp(expression, 'gi'), `<b>${expression}</b>`);
definition += `<li class="sent"><span class="eng_sent">${eng_sent}</span></li>`;
});
definition += '</ul>';
}
definitions.push(definition);
}
// Get long definition
let longDef = mainContent.querySelector('.long');
if (longDef) {
let def_text = longDef.textContent;
let definition = `<span class="tran"><span class="eng_tran">${def_text}</span></span>`;
definitions.push(definition);
}
// Get audio URLs (using YouDao API as fallback)
let audios = [
`https://dict.youdao.com/dictvoice?audio=${encodeURIComponent(expression)}&type=1`,
`https://dict.youdao.com/dictvoice?audio=${encodeURIComponent(expression)}&type=2`
];
notes.push({
css: this.renderCSS(),
expression,
reading,
definitions,
audios
});
resolve(notes);
} catch (error) {
reject(error);
}
});
}
renderCSS() {
return `
<style>
span.pos {text-transform:lowercase; font-size:0.9em; margin-right:5px; padding:2px 4px; color:white; background-color:#0d47a1; border-radius:3px;}
span.tran {margin:0; padding:0;}
span.eng_tran {margin-right:3px; padding:0;}
ul.sents {font-size:0.9em; list-style:square inside; margin:3px 0;padding:5px;background:rgba(13,71,161,0.1); border-radius:5px;}
li.sent {margin:0; padding:0;}
span.eng_sent {margin-right:5px;}
.word { font-weight: bold; }
</style>`;
}
}
// Support both browser and Node.js environments
if (typeof module !== 'undefined' && module.exports) {
module.exports = enen_Vocabulary;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment