Last active
July 20, 2017 11:49
-
-
Save afrontend/e59f905cd4ee09c25ce8c1c1d1ea797c to your computer and use it in GitHub Desktop.
크롬 확장 사용하여 웹 데이터 읽기, https://agvim.wordpress.com/2017/07/08/read-web-data-with-chrome-extension/
This file contains 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
{ | |
"manifest_version": 2, | |
"name": "Reading Web Data Example", | |
"description": "This extension shows a current temperature of the seoul", | |
"version": "1.0", | |
"browser_action": { | |
"default_popup": "popup.html" | |
}, | |
"permissions": [ | |
"https://www.accuweather.com/" | |
] | |
} |
This file contains 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
<!DOCTYPE html> | |
<html> | |
<head> | |
<title>서울 기온</title> | |
<meta charset="utf-8" /> | |
<style type="text/css"> | |
.box { | |
width: 120px; | |
height: 14px; | |
line-height: 14px; | |
font-size: 14px; | |
color: brown; | |
} | |
.box > span { | |
color: red | |
} | |
</style> | |
<script src="popup.js"></script> | |
</head> | |
<body> | |
<div class="box">서울의 온도 <span id="temperature">0 도</span></div> | |
</body> | |
</html> |
This file contains 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
document.addEventListener('DOMContentLoaded', function () { | |
httpRequest = new XMLHttpRequest(); | |
httpRequest.onreadystatechange = getContents; | |
httpRequest.open('GET', 'https://www.accuweather.com/ko/kr/seoul/226081/weather-forecast/226081'); | |
httpRequest.send(); | |
function getElement(html) { | |
var div = document.createElement('div'); | |
div.innerHTML = html; | |
return div; | |
} | |
function parseHTML(html) { | |
var el = getElement(html); | |
var temp = el.querySelector('#current-city-tab > a > span.local-temp').innerHTML; | |
document.querySelector('#temperature').innerHTML = temp; | |
} | |
function getContents() { | |
if (httpRequest.readyState === 4) { | |
if (httpRequest.status === 200) { | |
parseHTML(httpRequest.responseText); | |
} else { | |
document.querySelector('.box').innerHTML = '온도 읽기 실패'; | |
} | |
} | |
} | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment