Skip to content

Improve environment compatibility for React Native and others #42

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,8 @@
"author": "Vyacheslav <[email protected]>",
"license": "MIT",
"dependencies": {
"cheerio": "^1.0.0-rc.5",
"@xmldom/xmldom": "^0.9.8",
"jsdom": "^26.1.0",
"random-useragent": "^0.5.0"
},
"devDependencies": {
Expand Down
101 changes: 64 additions & 37 deletions src/reverso.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
const { getRandom } = require('random-useragent')
const { load } = require('cheerio')

const available = require('./utils/languages/available.js')
const compatibility = require('./utils/languages/compatibility.js')
Expand Down Expand Up @@ -30,6 +29,31 @@ module.exports = class Reverso {
this.insecureHTTPParser = insecureHTTPParser
}

/** @private */
#parseHTML(htmlString) {
let dom

if (typeof DOMParser !== 'undefined') {
const parser = new DOMParser()

dom = parser.parseFromString(htmlString, 'text/html')
} else if (
typeof process === 'object' &&
typeof process.versions === 'object' &&
typeof process.versions.node === 'string'
) {
const { JSDOM } = require('jsdom')

dom = new JSDOM(htmlString).window.document
} else {
const { DOMParser } = require('@xmldom/xmldom')

dom = new DOMParser().parseFromString(htmlString, 'text/html')
}

return dom
}

/**
* Get context examples of the query.
* @public
Expand Down Expand Up @@ -80,7 +104,7 @@ module.exports = class Reverso {
})
if (!response.success) return this.#handleError(response.error, cb)

const $ = load(response.data)
const document = this.#parseHTML(response.data)
const sourceDirection =
source === SupportedLanguages.ARABIC
? `rtl ${SupportedLanguages.ARABIC}`
Expand All @@ -94,36 +118,38 @@ module.exports = class Reverso {
? 'rtl'
: 'ltr'

const sourceExamples = $(
const sourceExamplesElements = document.querySelectorAll(
`.example > div.src.${sourceDirection} > span.text`
)
.text()
.trim()
.split('\n')
const targetExamples = $(
const targetExamplesElements = document.querySelectorAll(
`.example > div.trg.${targetDirection} > span.text`
)
.text()
.trim()
.split('\n')
const targetTranslations = $('#translations-content > div')
.text()
.trim()
.split('\n')
const targetTranslationsElements = document.querySelectorAll(
'#translations-content > div'
)

const sourceExamples = Array.from(sourceExamplesElements).map((el) =>
el.textContent.trim()
)
const targetExamples = Array.from(targetExamplesElements).map((el) =>
el.textContent.trim()
)
const targetTranslations = Array.from(targetTranslationsElements).map(
(el) => el.textContent.trim()
)

const examples = sourceExamples.map((e, i) => ({
id: i,
source: e.trim(),
target: targetExamples[i].trim(),
source: e,
target: targetExamples[i],
}))
const translations = targetTranslations.map((e) => e.trim())

const result = {
ok: true,
text,
source,
target,
translations,
translations: targetTranslations,
examples,
}

Expand Down Expand Up @@ -264,14 +290,14 @@ module.exports = class Reverso {
})
if (!response.success) return this.#handleError(response.error, cb)

const $ = load(response.data)
const document = this.#parseHTML(response.data)

const synonyms = []

$('a.synonym.relevant').each((i, e) => {
document.querySelectorAll('a.synonym.relevant').forEach((e, i) => {
synonyms.push({
id: i,
synonym: $(e).text(),
synonym: e.textContent,
})
})

Expand Down Expand Up @@ -485,16 +511,17 @@ module.exports = class Reverso {
})
if (!response.success) return this.#handleError(response.error, cb)

const $ = load(response.data)
const document = this.#parseHTML(response.data)

const verbForms = []

$('div[class="blue-box-wrap"]').each((i, e) => {
const header = $(e).attr('mobile-title').trim()
const data = []
document
.querySelectorAll('div[class="blue-box-wrap"]')
.forEach((e, i) => {
const header = e.getAttribute('mobile-title').trim()
const data = []

$(e)
.find(
e.querySelectorAll(
`i[class="verbtxt${
[
SupportedLanguages.RUSSIAN,
Expand All @@ -504,21 +531,21 @@ module.exports = class Reverso {
? '-term'
: ''
}"]`
)
.each((j, word) => {
if (!$(word).parents('.transliteration').attr('class')) {
data.push($(word).text())
).forEach((word) => {
if (!word.closest('.transliteration').classList.length) {
data.push(word.textContent)
}
})

verbForms.push({
id: i,
conjugation: header,
verbs: [...new Set(data)],
verbForms.push({
id: i,
conjugation: header,
verbs: [...new Set(data)],
})
})
})

const infinitive = $('#ch_lblVerb').text()
const infinitive =
document.getElementById('ch_lblVerb')?.textContent || ''

const result = {
ok: true,
Expand Down
Loading