-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.js
More file actions
53 lines (47 loc) · 1.58 KB
/
Copy pathindex.js
File metadata and controls
53 lines (47 loc) · 1.58 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
const html = (url, options) => {
return fetch(url)
.then(response => response.text())
.then(str => (new window.DOMParser()).parseFromString(str, 'text/html'))
.then(dom => {
if (options.hasOwnProperty('selector')) {
return dom.querySelector(options.selector)
} else if (options.hasOwnProperty('selectorAll')) {
return dom.querySelectorAll(options.selectorAll)
} else {
return dom
}
})
}
let _sourceText = ''
const translate = ({
sourceLang = 'auto',
targetLang = 'en',
sourceText = ''
}) => {
sourceText = sourceText.trim()
// Cache sourceText
if (sourceText === _sourceText) {
return
} else {
_sourceText = sourceText
}
const url = 'https://translate.googleapis.com/translate_a/single?client=gtx&sl='
+ sourceLang + '&tl=' + targetLang + '&dt=t&q=' + encodeURI(sourceText)
fetch(url)
.then(response => response.json())
.then(data => {
console.log('response data', data)
const translatedText = data[0][0][0]
console.log('translatedText', translatedText)
document.querySelector('#translatedText').innerHTML = translatedText
html('http://tureng.com/en/turkish-english/' + translatedText, {
selectorAll: '.searchResultsTable'
})
.then(dom => {
document.querySelector('#results').innerHTML = ''
console.log(dom)
document.querySelector('#results').appendChild(dom[0])
document.querySelector('#results').appendChild(dom[1])
})
})
}