Last active
October 22, 2020 07:23
-
-
Save keicoon/950380d1c45b67236ef358fcec69a8e8 to your computer and use it in GitHub Desktop.
code snippet about naver dictionary request in javascript
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
new Promise((resolve, reject) => { | |
var xhr = new XMLHttpRequest(); | |
xhr.open("GET", `https://endic.naver.com/searchAssistDict.nhn?query=${text}`, true); | |
xhr.onreadystatechange = function() { | |
if (xhr.readyState == 4) { | |
const content_word = /<div class="box_a">((.|\n)*?)<\/div>/g | |
const filter_word = /<span class="fnt_k20"><strong>(.+)<\/strong><\/span>/g | |
const body = xhr.responseText; | |
let words = []; | |
body.replace(content_word, function(match, p1) { | |
p1.replace(filter_word, function(match2, p2) { | |
if (p2.match(/<|>/) == null) { | |
words.push(p2); | |
} | |
}); | |
}); | |
resolve(words.join('\n')); | |
} | |
// TODO: reject | |
} | |
xhr.send(); | |
}) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment