1 #!/usr/bin/env phantomjs2 --ssl-protocol=any --ignore-ssl-errors=true --web-security=false
6 1 a web interaction step failed on user side
9 4 a web interaction step failed on navigator side
13 var llDebug = 1, llStatus = 2, llError = 3, llMsg = 4;
18 var system = require('system');
19 var stderr = system.stderr;
20 var stdout = system.stdout;
23 for(var i=1; i<system.args.length; i++)
25 if(system.args[i] == '--debug') vlDebug = true;
26 else if(system.args[i] == '--verbose') vlStatus = true;
27 else if(system.args[i].match(/^-/))
29 stderr.write("Unknown option: "+system.args[i]+"\n");
34 Glob.felhazon = system.args[i];
35 Glob.gyariszam = system.args[i+1];
36 Glob.meroallas = system.args[i+2];
37 Glob.email = system.args[i+3];
38 Glob.datum = system.args[i+4];
43 if(Glob.datum == undefined)
45 stderr.write("Usage: nkmfoldgaz.js [--debug] [--verbose] <felhasználó-azonosító> <mérőóra-gyári-szám> <mérőállás> <email> <dátum>\n");
51 var url_form = "https://www.nkmfoldgaz.hu/Ugyfelszolgalat/Havi-meroallas-kozlese-pub";
52 var loadInProgress = false;
54 var page = new WebPage();
55 page.settings.userAgent = "Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/59.0.3071.90 Safari/537.36 Vivaldi/1.91.867.38";
57 function log(level, msg)
59 if(level == llDebug && vlDebug ||
60 level == llStatus && vlStatus ||
61 level == llError && vlError ||
62 level == llMsg && vlMsg)
64 stderr.write(msg + "\n");
68 page.onConsoleMessage = function(msg)
70 if(msg.match(/^\[DEBUG\]/))
73 log(llMsg, ">>> " + msg);
76 page.onLoadStarted = function()
78 loadInProgress = true;
79 log(llStatus, "load started [" + page.url + "]");
82 page.onLoadFinished = function()
84 loadInProgress = false;
85 log(llStatus, "load finished [" + page.evaluate(function(){return document.title;}) + "]");
88 page.onError = function(msg, stack)
90 log(llError, "ERROR: " + msg + "\nSTACK: " + JSON.stringify(stack));
93 page.onResourceRequested = function(requestData, networkRequest)
95 if(!requestData.url.match(/^data:/))
97 log(llDebug, requestData.method+' '+requestData.url);
100 for(var i=0; i<requestData.headers.length; i++)
102 log(llDebug, requestData.headers[i].name + ': ' + requestData.headers[i].value);
104 log(llDebug, JSON.stringify(requestData));
109 page.open(url_form, function(status)
111 if(status !== 'success')
113 log(llError, "ERROR " + status);
121 /* Waiting iframe to load */
122 /* Search for the 'Rögzít' button */
124 var spans = document.getElementsByTagName('span');
125 for(var x = 0; x < spans.length; x++)
127 if(spans[x].innerText.match(/állás mentése/))
129 button_span = spans[x];
132 if(typeof button_span == 'undefined')
134 // uncomment this to see the current document and debug
135 //console.log(document.querySelectorAll('html')[0].outerHTML);
136 console.log("Waiting for submit button...");
143 /* Filling in form fields by simulating typing on keyboard */
145 function typein(field_id, str)
147 console.log("[DEBUG] Typing in '" + str + "' in field '#" + field_id + "'...");
148 var field = document.getElementById(field_id);
150 for(var pos = 0; pos < str.length; pos++)
152 for(var keyevent in {"keydown":1, "keypress":1, "input":1, "change":1, "keyup":1})
154 var ev = document.createEvent("KeyboardEvent");
155 var keyCode = str.charCodeAt(pos);
156 var charCode = str.charCodeAt(pos);
157 ev.initKeyboardEvent(keyevent, true, true, window, null, false, false, false, false, keyCode, charCode);
160 field.dispatchEvent(ev);
166 var starting_field_id;
167 /* Find first input field's Id */
168 var inputs = document.getElementsByTagName('input');
169 for(var x = 0; x < inputs.length; x++)
171 if(inputs[x].getAttribute('title') == "Felhasználó azonosító")
173 starting_field_id = inputs[x].getAttribute('id');
176 if(typeof starting_field_id == 'undefined')
178 console.log("input filed ID is not found");
181 var starting_field_num = eval("0x" + starting_field_id.substr(2,2));
184 var x = n.toString(16).toUpperCase();
185 if(x.length == 1) x = '' + '0' + x;
189 typein(mkWDid(starting_field_num + 0), param.felhazon);
190 typein(mkWDid(starting_field_num + 5), param.gyariszam);
191 typein(mkWDid(starting_field_num + 10), param.meroallas);
192 typein(mkWDid(starting_field_num + 15), param.email);
193 typein(mkWDid(starting_field_num + 20), param.datum);
195 param['submit_button_id'] = mkWDid(starting_field_num + 23);
196 param['result_element_id'] = mkWDid(starting_field_num + 30);
197 return JSON.stringify({
204 /* Submit form by simulating user clicking on button */
206 var submit_button = document.getElementById(param['submit_button_id']);
207 var ev = document.createEvent("MouseEvent");
208 ev.initMouseEvent("click", true /* bubble */, true /* cancelable */, window, null, 0, 0, 0, 0, /* coordinates */ false, false, false, false, /* modifier keys */ 0 /*left*/, null);
209 submit_button.dispatchEvent(ev);
214 /* Wait response page to load */
215 if(!document.getElementById(param['result_element_id']))
217 console.log("Waiting for results..."+param['result_element_id']);
224 /* Echo response from server */
225 var result_elem = document.getElementById(param['result_element_id'])
226 /* the WD element (usually #WD37 or #WD38) we calculated is not always the element which holds lsdata,
227 so we look for the first element actually having lsdata with result_element_id */
228 if(!result_elem.getAttribute('lsdata'))
229 result_elem = document.querySelector('#'+param['result_element_id']+' [lsdata]');
230 param.result_elem_html_outer = result_elem.outerHTML;
231 param.result_elem_html_inner = result_elem.innerHTML;
232 param.result_elem_text_inner = result_elem.innerText;
233 param.result = result_elem.getAttribute('lsdata');
234 return JSON.stringify({
244 function display_nkm_result(result)
247 var v = result.replace(/\\x(..)/g, function(match, group_1, start_pos, whole_string){
248 return String.fromCharCode(Number("0x" + group_1));
250 stdout.write(v + "\n");
259 /* Make sure we are in the correct frame */
260 page.switchToFrame('wasframe');
261 log(llDebug, "Frame: " + page.frameName + " \"" + page.frameTitle + "\" [" + page.frameUrl + "]");
263 /* Execute next step in navigator's context */
264 log(llDebug, "step " + stepindex);
265 var ret = page.evaluate(steps[stepindex], Glob);
271 else if(ret === undefined || ret === null)
276 if(typeof ret != "number")
278 ret = JSON.parse(ret);
281 if(!ret.result.hasOwnProperty(key)) delete Glob[key];
283 for(var key in ret.result)
285 Glob[key] = ret.result[key];
287 //stderr.write(JSON.stringify(Glob));
291 log(llDebug, "step " + stepindex + " jump " + ret);
294 if(stepindex >= steps.length)
296 log(llDebug, "complete");
297 if(Glob.result && Glob.result.match(/POSITIVE/))
299 stderr.write("[OK] Transaction passed.\n");
300 stdout.write(Glob.result + "\n");
301 display_nkm_result(Glob.result);
306 stderr.write("[ERROR] Transaction failed.\n");
307 stderr.write(Glob.result_elem_html_outer + "\n");
308 stderr.write(Glob.result_elem_text_inner + "\n");
309 stderr.write(Glob.result + "\n");
310 display_nkm_result(Glob.result);
316 setTimeout(worker, timeout);
318 setTimeout(worker, timeout);