Last active
February 26, 2025 08:59
-
-
Save weiyinerzui/6349a6b1440ac25d2777ede9b5984627 to your computer and use it in GitHub Desktop.
朗文英英词典
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class enen_Longman { | |
constructor() { | |
this.name = "Longman"; | |
this.description = "朗文英英词典"; | |
this.baseUrl = "https://www.ldoceonline.com/dictionary/"; | |
} | |
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 dictEntries = doc.querySelectorAll('.dictionary .dictentry'); | |
if (!dictEntries.length) { | |
reject('No definition found'); | |
return; | |
} | |
let result = ''; | |
// Process each dictionary entry | |
dictEntries.forEach((entry, index) => { | |
// Get word and pronunciation | |
let headword = entry.querySelector('.headword'); | |
let pron = entry.querySelector('.PronCodes'); | |
let header = `<div class="head"> | |
<span class="word">${headword ? headword.textContent : word}</span> | |
${pron ? `<span class="pron">/${pron.textContent}/</span>` : ''} | |
</div>`; | |
// Get all senses of the word | |
let senses = entry.querySelectorAll('.Sense'); | |
let sensesHtml = ''; | |
senses.forEach((sense) => { | |
// Get grammar info (n-count, v-tr etc.) | |
let grammar = sense.querySelector('.GRAM'); | |
let grammarText = grammar ? grammar.textContent : ''; | |
// Get definition | |
let def = sense.querySelector('.DEF'); | |
let definition = def ? def.textContent : ''; | |
// Get examples | |
let examples = sense.querySelectorAll('.EXAMPLE'); | |
let examplesHtml = ''; | |
examples.forEach(ex => { | |
examplesHtml += `<div class="example">${ex.textContent}</div>`; | |
}); | |
sensesHtml += ` | |
<div class="sense"> | |
${grammarText ? `<span class="grammar">[${grammarText}]</span>` : ''} | |
<div class="definition">${definition}</div> | |
${examplesHtml} | |
</div>`; | |
}); | |
result += ` | |
<div class="entry"> | |
${header} | |
${sensesHtml} | |
</div>`; | |
}); | |
// Add CSS styles | |
result = ` | |
<style> | |
.entry { margin-bottom: 1em; } | |
.head { font-size: 1.2em; margin-bottom: 0.5em; } | |
.word { font-weight: bold; } | |
.pron { color: #666; margin-left: 0.5em; } | |
.grammar { color: #2962ff; font-weight: bold; } | |
.sense { margin: 0.5em 0; padding-left: 1em; } | |
.definition { margin: 0.3em 0; } | |
.example { color: #666; font-style: italic; margin: 0.2em 0; padding-left: 1em; } | |
</style> | |
${result}`; | |
resolve(result); | |
} catch (error) { | |
reject(error); | |
} | |
}); | |
} | |
} | |
// Support both browser and Node.js environments | |
if (typeof module !== 'undefined' && module.exports) { | |
module.exports = enen_Longman; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment