How to call javascript functions in web pages?

General Help regarding HMG, Compilation, Linking, Samples

Moderator: Rathinagiri

Post Reply
huiyi_ch
Posts: 172
Joined: Sat May 21, 2016 5:27 am

How to call javascript functions in web pages?

Post by huiyi_ch »

Hello Friends
Is there a way to call a javascript function in a web page in HMG and return its value?
有什么方法能在HMG中调用网页中的javascript函数,并返回其值?
User avatar
mol
Posts: 3718
Joined: Thu Sep 11, 2008 5:31 am
Location: Myszków, Poland
Contact:

Re: How to call javascript functions in web pages?

Post by mol »

This is solution from our forum friend Edk.

Maybe this piece of code will help you:

Code: Select all

cParameters := "whatever you want to send to script"	// parameter of sample script file

IF !FILE(_ExePath+'SampleJavaScriptFile.js')
	MsgExclamation("SampleJavaScriptFile.js  not found!')
	return .f.
endif

if !FILE(_ExePath+'phantomjs.exe')
	MsgExclamation("You need to download: phantomjs.exe"+chr(10)+;
				"from http://phantomjs.org/download.html and install in execution path")
	RETURN .f.
ENDIF

IF !isInternet()
	MsgExclamation("No internet connection!")
	RETURN .f.
ENDIF

WAIT WINDOW 'Trying do call .js' NOWAIT

WaitRun( _ExePath+'phantomjs.exe '+_ExePath+'SampleJavaScriptFile.js '+cParameters, SW_HIDE )

WAIT CLEAR

Last edited by mol on Sun Dec 23, 2018 10:16 am, edited 1 time in total.
User avatar
mol
Posts: 3718
Joined: Thu Sep 11, 2008 5:31 am
Location: Myszków, Poland
Contact:

Re: How to call javascript functions in web pages?

Post by mol »

and javascript file for retrieve info from internet page:

Code: Select all

/**
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions are met:
 * 
 *   * Redistributions of source code must retain the above copyright
 *     notice, this list of conditions and the following disclaimer.
 *   * Redistributions in binary form must reproduce the above copyright
 *     notice, this list of conditions and the following disclaimer in the
 *     documentation and/or other materials provided with the distribution.
 *   * Neither the name of the <organization> nor the
 *     names of its contributors may be used to endorse or promote products
 *     derived from this software without specific prior written permission.
 * 
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
 * ARE DISCLAIMED. IN NO EVENT SHALL <COPYRIGHT HOLDER> BE LIABLE FOR ANY
 * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 */
"use strict";
function waitFor(testFunction, readyFunction, timeOutMillis) {
	var maxtimeOutMillis = timeOutMillis ? timeOutMillis : 4000,
		start = new Date().getTime(),
		condition = false,
		interval = setInterval(function() {
			if ( (new Date().getTime() - start < maxtimeOutMillis) && !condition ) {
				condition = (typeof(testFunction) === "string" ? eval(testFunction) : testFunction());
				
			} else {
				if(!condition) {
					phantom.exit(1);
				} else {
					typeof(readyFunction) === "string" ? eval(readyFunction) : readyFunction();
					clearInterval(interval);
                		}
            		}
        	}, 250);
};

var system = require('system'),
	fs = require('fs'),
	args = system.args,
	nip,
	nieJest = "Podmiot o podanym identyfikatorze podatkowym NIP nie jest zarejestrowany jako podatnik VAT",
	Jest = "NIP jest zarejestrowany",
	cNazwaPliku = "NIPwVAT.txt", 
	cUrl = "https://ppuslugi.mf.gov.pl/",
	page = require('webpage').create();

if (1 === args.length) {
	try { 
		fs.write(cNazwaPliku, 'Wymagany jest NIP.', 'w'); 
	} catch(e) { 
		console.log(e); 
	} 
	phantom.exit();
	console.log('');

} else {
	nip = args[1];
}

page.open(cUrl, function(status) {
					if (status !== "success") {
						try {
							fs.write(cNazwaPliku, "Witryna '" + cUrl +"' nie odpowiada.", 'w'); 
						} catch(e) { 
							console.log(e); 
						} 
						phantom.exit();
						console.log('');
					} else {
						waitFor(function(){
							return page.evaluate(function() { return $('#SidebarActions_WebUMn li').length > 0; });
						        }, function(){
								page.evaluate(function() { 
											FWDC.executeAction(1005, null, 'FLOW');
								});
						waitFor(function(){
							return page.evaluate(function() { return $('#b-7').length > 0 && $('#inputcontainer_b-7').length > 0; });
							}, function(){
								page.evaluate(function(nip) { 
											$('#b-7').val(nip);
											$('#b-8').trigger('click');
								}, nip);

						waitFor(function(){
							return page.evaluate(function() { return $('#container_b-3').is(':visible') });
							}, function() {
								var date = page.evaluate(function() { return $.trim($('#caption2_b-b').text().split(':')[1]).split('-').reverse().join('-'); }),
									response = {
										"nip": nip,
										"status": page.evaluate(function() { return $.trim($('#container_b-3').text()); }).indexOf(Jest) >= 0,
										"message": page.evaluate(function() { return $.trim($('#container_b-3').text()); }),
										"date": date				
									};
								
								var cHtml = page.content;
								cHtml = cHtml.replace( /href=\"\.\.\//g, 'href="'+cUrl);
								cHtml = cHtml.replace( /\.\.\/Image/g, cUrl+"/Image");

								try { 
									fs.write(cNazwaPliku.replace(/\.txt/g, ".html"), cHtml, 'w'); 
								} catch(e) { 
									console.log(e); 
								} 



								var status2 = page.evaluate(function() { return $.trim($('#container_b-3').text()); }) == nieJest ? false : true;
								
								if (status2 === response.status){
									try { 
										fs.write(cNazwaPliku, JSON.stringify(response), 'w'); 
									} catch(e) { 
										console.log(e); 
									} 

								}else{
									/*
									 * W odpowiedzi serwera nie ma frazy ze zmiennej 'Jest' oraz 'nieJest'
									*/

									try { 
										fs.write(cNazwaPliku, "Status nieznany, informacja otrzymana z " + cUrl +": " + response.message, 'w'); 
									} catch(e) { 
										console.log(e); 
									} 
						
								}
								phantom.exit();
							});
						});
						});        
					}
}); 
fs.write(cNazwaPliku, 'Brak odpowiedzi dla NIP ' + nip, 'w'); 
User avatar
serge_girard
Posts: 3161
Joined: Sun Nov 25, 2012 2:44 pm
DBs Used: 1 MySQL - MariaDB
2 DBF
Location: Belgium
Contact:

Re: How to call javascript functions in web pages?

Post by serge_girard »

Thanks Marek and Edward!

Serge
There's nothing you can do that can't be done...
huiyi_ch
Posts: 172
Joined: Sat May 21, 2016 5:27 am

Re: How to call javascript functions in web pages?

Post by huiyi_ch »

Thanks Marek and Edward
I will try this during the new year holiday.
User avatar
bpd2000
Posts: 1207
Joined: Sat Sep 10, 2011 4:07 am
Location: India

Re: How to call javascript functions in web pages?

Post by bpd2000 »

BPD
Convert Dream into Reality through HMG
User avatar
bpd2000
Posts: 1207
Joined: Sat Sep 10, 2011 4:07 am
Location: India

Re: How to call javascript functions in web pages?

Post by bpd2000 »

BPD
Convert Dream into Reality through HMG
User avatar
serge_girard
Posts: 3161
Joined: Sun Nov 25, 2012 2:44 pm
DBs Used: 1 MySQL - MariaDB
2 DBF
Location: Belgium
Contact:

Re: How to call javascript functions in web pages?

Post by serge_girard »

Well, Clipper was also declared 'dead', years ago. As long as it works, it is living!
There's nothing you can do that can't be done...
Post Reply