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
14 var llDebug = 1, llVerbose = 2, llError = 3, llMsg = 4;
16 var vlVerbose = false;
19 var system = require('system');
20 var stderr = system.stderr;
21 var stdout = system.stdout;
23 'show_history_only': false,
26 for(var i=1; i<system.args.length; i++)
28 if(system.args[i] == '--debug') vlDebug = true;
29 else if(system.args[i] == '--verbose') vlVerbose = true;
30 else if(system.args[i] == '--history') Glob.show_history_only = true;
31 else if(system.args[i].match(/^-/))
33 stderr.write("Unknown option: "+system.args[i]+"\n");
40 Glob.felhasznalo = system.args[i];
44 Glob.jelszo = system.args[i];
48 if(system.args[i].match(/^[0-9]+$/))
50 if(typeof Glob.oraallas == "undefined") Glob.oraallas = [];
51 Glob.oraallas.push(system.args[i]);
55 stderr.write("Invalid óraállás: "+system.args[i]+"\n");
62 if(!Glob.oraallas && !Glob.show_history_only)
64 stderr.write("Usage: nkmaram.js [--debug] [--verbose] <felhasználó-azonosító> <jelszó> [--history | <mérőállás-1> [<mérőállás-2> ...]]\n");
70 var url_form = "https://www.mvmnext.hu/aram/pages/wrapper.jsp?id=740&ebillCmd=login";
71 Glob.url_history = "https://www.mvmnext.hu/aram/pages/online/korabbiMeroAllOnlineUsz.jsf?id=1300460";
72 Glob.url_report = "https://www.mvmnext.hu/aram/meroallas/?id=1300461";
73 var loadInProgress = false;
75 var page = new WebPage();
76 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";
78 function log(level, msg)
80 if(level == llDebug && vlDebug ||
81 level == llVerbose && vlVerbose ||
82 level == llError && vlError ||
83 level == llMsg && vlMsg)
85 stderr.write(msg + "\n");
89 page.onConsoleMessage = function(msg)
91 if(msg.match(/^\[DEBUG\]/))
93 else if(msg.match(/^\[ERROR\]/))
96 log(llMsg, ">>> " + msg);
99 page.onLoadStarted = function()
101 loadInProgress = true;
102 log(llVerbose, "load started [" + page.url + "]");
105 page.onLoadFinished = function()
107 loadInProgress = false;
108 log(llVerbose, "load finished <" + page.evaluate(function(){return document.title;}) + "> [" + page.url + "]");
111 page.onError = function(msg, stack)
113 log(llError, "ERROR: " + msg + "\nSTACK: " + JSON.stringify(stack));
116 page.onResourceRequested = function(requestData, networkRequest)
118 if(!requestData.url.match(/^data:/))
120 log(llDebug, requestData.method+' '+requestData.url);
123 for(var i=0; i<requestData.headers.length; i++)
125 log(llDebug, requestData.headers[i].name + ': ' + requestData.headers[i].value);
127 log(llDebug, JSON.stringify(requestData));
132 /* Define common functions used in webpage */
133 Glob.clickFunc = '(function(elem)'+
135 ' var ev = document.createEvent("MouseEvent");'+
136 ' ev.initMouseEvent("click", true /* bubble */, true /* cancelable */, window, null, 0, 0, 0, 0, /* coordinates */ false, false, false, false, /* modifier keys */ 0 /*left*/, null);'+
137 ' elem.dispatchEvent(ev);'+
141 page.open(url_form, function(status)
143 if(status !== 'success')
145 log(llError, "ERROR " + status);
153 /* Waiting page to load */
154 var field_user = document.querySelector('.loginForm input[name=user]');
157 //console.log(document.querySelectorAll('html')[0].outerHTML);
158 console.log("Waiting for login field...");
165 /* Filling in form fields and submit */
167 var field_user = document.querySelector('.loginForm input[name=user]');
168 var field_pass = document.querySelector('.loginForm input[name=password]');
170 field_user.value = param.felhasznalo;
171 field_pass.value = param.jelszo;
173 var form = document.querySelector('form.loginForm');
180 var messageBox = document.querySelector('.messageBox');
181 var egyenleg_elem = document.querySelector('.szsz_egyenleg');
182 if(!egyenleg_elem && !messageBox)
184 console.log("Waiting for page to load...");
189 console.log(messageBox.innerText);
192 var egyenleg = egyenleg_elem.innerText;
193 console.log("Egyenleg: " + egyenleg);
194 param.egyenleg = egyenleg;
195 return JSON.stringify({
202 var table = document.querySelector('form[name=korabbiMeroAllFormOnlineusz] table.rtable');
205 if(location.href == param.url_history)
206 console.log("Waiting for page to load...");
208 location.href = param.url_history;
211 console.log(table.innerText);
212 if(param.show_history_only) return param.total_steps + 1;
217 /* Navigate to 'Mérőállás bejelentés' page */
218 location.href = param.url_report;
223 /* Wait for page to load */
224 var buttons = document.querySelectorAll('form#meroTajForm input.button');
227 console.log("Waiting for page "+location.href+" ...");
230 for(var i = 0; i<buttons.length; i++)
232 console.log("[DEBUG] button \""+buttons[i].value+"\"");
233 if(buttons[i].value.match(/bejelent/))
235 /* Submit form to proceed (1) */
236 eval(param.clickFunc)(buttons[i]);
244 /* Exit if we got an error page */
245 if(location.pathname.match(/meroHibalap/))
247 console.log(document.querySelector('form#meroHibalapForm').innerText);
250 /* Wait for page to load */
251 var buttons = document.querySelectorAll('input[type=submit][name^=meroallasBejelentes]');
254 console.log("Waiting for page "+location.href+" ...");
258 var inputs = document.querySelectorAll('form#meroallasBejelentes input.numberFormat');
259 for(var i = 0; i < inputs.length; i++)
261 console.log("Enter '"+param.oraallas[i]+"'");
262 inputs[i].value = param.oraallas[i];
265 for(var i = 0; i<buttons.length; i++)
267 console.log("[DEBUG] button \""+buttons[i].value+"\"");
268 if(buttons[i].value.match(/mentés/))
270 /* Submit form to proceed (2) */
271 eval(param.clickFunc)(buttons[i]);
279 var summary = document.querySelector('form#szamlaForm');
282 console.log("Waiting for page "+location.href+" ...");
285 console.log(summary.innerText);
287 /* Accept Utility Bill */
288 var accept_btn = document.querySelector('input[name="szamlaForm:bOK"]');
289 eval(param.clickFunc)(accept_btn);
295 > ERROR: ReferenceError: Can't find variable: clickedButton
296 > STACK: [{"file":"https://www.nkmenergia.hu/aram/meroallas/meroSzamla.jsf?id=1300461","line":4648,"function":"onclick"},{"file":"","line":0,"function":"dispatchEvent"},{"file":"","line":1,"function":""},{"file":"","line":12,"function":""},{"file":"","line":15,"function":""}]
300 /* Wait for page to load */
301 var readyForm = document.querySelector('form#readyForm');
304 console.log("Waiting for page "+location.href+" ...");
307 /* Display Service Provider's message*/
308 console.log(readyForm.innerText);
313 Glob.total_steps = steps.length;
320 /* Execute next step in navigator's context */
321 log(llDebug, "step " + stepindex);
322 var ret = page.evaluate(steps[stepindex], Glob);
328 else if(ret === undefined || ret === null)
333 if(typeof ret != "number")
335 ret = JSON.parse(ret);
338 if(!ret.result.hasOwnProperty(key)) delete Glob[key];
340 for(var key in ret.result)
342 Glob[key] = ret.result[key];
344 //stderr.write(JSON.stringify(Glob));
348 log(llDebug, "step " + stepindex + " jump " + ret);
351 if(stepindex >= steps.length)
353 log(llDebug, "completed");
358 setTimeout(worker, timeout);
360 setTimeout(worker, timeout);