forked from biud436/MV
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathRS_ParseWeatherData.js
216 lines (191 loc) · 5.08 KB
/
RS_ParseWeatherData.js
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
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
/*:
* @plugindesc <RS_ParseWeatherData>
* @author biud436
*
* @param Default Platform
* @text Default
*
* @param URL
* @text URL
* @parent Default Platform
* @desc
* @default http://m.kma.go.kr/m/eng/observation/observation_01.jsp
*
* @param NodeJS
*
* @param options
* @text Options
* @parent NodeJS
* @type struct<NodeJSOptions>
* @desc
* @default {"host":"m.kma.go.kr","port":"80","path":"/m/eng/observation/observation_01.jsp"}
*
* @help
*
* ==========================================================================
* Script calls
* ==========================================================================
* This function allows you to parse the html data through callback function.
*
* RS.Weather.parseHTML(function(doc) {
*
* // Find the <div> element in target document
* var inf = doc.querySelector('.inf');
*
* // Set the game title using parsing the text for weather
* document.title = inf.firstElementChild.nextElementSibling.textContent;
*
* });
*
* ==========================================================================
* Version Log
* ==========================================================================
* 2018.04.28 (v1.0.0) - First Release.
*/
/*~struct~NodeJSOptions:
*
* @param host
* @desc
* @default m.kma.go.kr
*
* @param port
* @type number
* @desc
* @default 80
* @min 80
* @max 80
*
* @param path
* @desc
* @default /m/eng/observation/observation_01.jsp
*/
/*:ko
* @plugindesc <RS_ParseWeatherData>
* @author 러닝은빛(biud436)
*
* @param Default Platform
* @text 기본 플랫폼
*
* @param URL
* @text 주소
* @parent Default Platform
* @desc
* @default http://m.kma.go.kr/m/observation/observation_01.jsp
*
* @param NodeJS
*
* @param options
* @text 옵션
* @parent NodeJS
* @type struct<NodeJSOptions>
* @desc
* @default {"host":"m.kma.go.kr","port":"80","path":"/m/observation/observation_01.jsp"}
*
* @help
*
* ==========================================================================
* 사용법
* ==========================================================================
* 다음과 같이 스크립트를 호출하세요.
*
* RS.Weather.parseHTML(function(doc) {
* var inf = doc.querySelector('.inf');
* document.title = inf.firstElementChild.nextElementSibling.textContent;
* });
*
* ==========================================================================
* Version Log
* ==========================================================================
* 2018.04.28 (v1.0.0) - 출시
*/
/*~struct~NodeJSOptions:ko
*
* @param host
* @desc
* @default m.kma.go.kr
*
* @param port
* @type number
* @desc
* @default 80
* @min 80
* @max 80
*
* @param path
* @desc
* @default /m/observation/observation_01.jsp
*/
var RS = RS || {};
RS.Weather = RS.Weather || {};
(function($) {
var parameters = $plugins.filter(function(i) {
return i.description.contains("<RS_ParseWeatherData>");
})[0].parameters;
$.Params = $.Params || {};
$.Params.url = parameters["URL"] || "http://m.kma.go.kr/m/observation/observation_01.jsp";
$.Params.options = JSON.parse(parameters["options"]);
/**
* https://stackoverflow.com/a/20462701 By 존 슬러거
*/
function parseHTML(markup) {
if (markup.toLowerCase().trim().indexOf('<!doctype') === 0) {
var doc = document.implementation.createHTMLDocument("");
doc.documentElement.innerHTML = markup;
return doc;
} else if ('content' in document.createElement('template')) {
// Template tag exists!
var el = document.createElement('template');
el.innerHTML = markup;
return el.content;
} else {
// Template tag doesn't exist!
var docfrag = document.createDocumentFragment();
var el = document.createElement('body');
el.innerHTML = markup;
for (i = 0; 0 < el.childNodes.length;) {
docfrag.appendChild(el.childNodes[i]);
}
return docfrag;
}
};
function parse(cb) {
var method = Utils.isNwjs() ? parseHTMLFromNodeJS : parseHTMLFromXML;
method(cb);
};
function parseHTMLFromXML(cb) {
var url = $.Params.url;
var xhr = new XMLHttpRequest();
xhr.open('GET', url, true);
xhr.send(null);
xhr.overrideMimeType('document');
xhr.onload = function() {
if (xhr.status < 400) {
try {
var el = parseHTML(xhr.responseText);
cb(el);
} catch(e) {
// CORS로 인한 오류 방지
}
}
}.bind(this);
};
function parseHTMLFromNodeJS(cb) {
var options = $.Params.options;
var http = require('http');
var req = http.get(options, function(res) {
var resData = "";
res.on('data', function(chunk) {
resData += chunk;
});
res.on('end', function() {
var doc = parseHTML(resData);
cb(doc);
});
});
req.on('error', function(err) {
console.log(err.message);
});
}
// Export function
$.parseHTML = parse;
})(RS.Weather);