Page 1 of 1
How to call javascript functions in web pages?
Posted: Sun Dec 23, 2018 1:50 am
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函数,并返回其值?
Re: How to call javascript functions in web pages?
Posted: Sun Dec 23, 2018 10:13 am
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
Re: How to call javascript functions in web pages?
Posted: Sun Dec 23, 2018 10:15 am
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');
Re: How to call javascript functions in web pages?
Posted: Sun Dec 23, 2018 1:15 pm
by serge_girard
Thanks Marek and Edward!
Serge
Re: How to call javascript functions in web pages?
Posted: Sun Dec 23, 2018 2:04 pm
by huiyi_ch
Thanks Marek and Edward
I will try this during the new year holiday.
Re: How to call javascript functions in web pages?
Posted: Mon Dec 24, 2018 5:29 am
by bpd2000
Re: How to call javascript functions in web pages?
Posted: Mon Dec 24, 2018 5:48 am
by bpd2000
Re: How to call javascript functions in web pages?
Posted: Mon Dec 24, 2018 8:36 am
by serge_girard
Well, Clipper was also declared 'dead', years ago. As long as it works, it is living!