imacros with jquery and ajax

Discussions and Tech Support related to automating the iMacros Browser or Internet Explorer from any scripting and programming language, such as VBS (WSH), VBA, VB, Perl, Delphi, C# or C++.
Forum rules
Before asking a question or reporting an issue:
1. Please review the list of FAQ's.
2. Use the search box (at the top of each forum page) to see if a similar problem or question has already been addressed.
3. Try searching the iMacros Wiki - it contains the complete iMacros reference as well as plenty of samples and tutorials.
4. We can respond much faster to your posts if you include the following information: CLICK HERE FOR IMPORTANT INFORMATION TO INCLUDE IN YOUR POST
Post Reply
Shoop
Posts: 18
Joined: Fri Apr 17, 2015 10:58 am

imacros with jquery and ajax

Post by Shoop » Thu May 07, 2015 12:47 pm

can imacros play jquery and ajax code if it is found in their .js file? or will it return an error?

also u can call a macro in a javascript. but can u call javascript in macro? (like the reverse of retcode/iimPlay)

edit: not working

Code: Select all

njuskalo=new XMLHttpRequest();

njuskalo.onreadystatechange = function() {
    if (njuskalo.readyState == 4) {
        console.log(njuskalo.responseText);
    }
}

njuskalo.open("GET","http://www.njuskalo.hr/usb-memorija/mem-ufd-8gb-jf300-ts-oglas-15090605");
njuskalo.send();
the code does not work with imacros, while on http://jsfiddle.net/ks037hxm/1/ it works without problems. that answers my questions up on top.
hopefully the imacros staff can start to implement the XMLHttpRequest and other constantsinto the imacros, which could be rly usefull when CORS becomes a standard. or atleast add it to standard/enterprise edition or smthing. hope u guys can bring it up with the dev team. if u need more info why i might need it feel free to ask.
Lantus
Posts: 4
Joined: Tue Jan 19, 2016 5:40 pm

Re: imacros with jquery and ajax

Post by Lantus » Wed Apr 15, 2020 9:55 pm

Yes, it's totally possible.
Load the constructor with the const XMLHttpRequest.
To make multiple HTTP requests, you have to push them in an array.

I made a function to do it...

Code: Select all

	const XMLHttpRequest = Components.Constructor("@mozilla.org/xmlextras/xmlhttprequest;1");

	var xhr = new Array(); // ARRAY OF XML-HTTP REQUESTS
	var xi = new Array(0); // ARRAY OF XML-HTTP REQUEST INDEXES
	xi[0] = 1; // FIRST INDEX SET TO 1 MAKING IT AVAILABLE

	function xhrRequest(type) {
		if (!type) {type = 'html';}

		// xhrsend IS THE xi POSITION THAT GETS PASSED BACK
		// INITIALIZED TO THE LENGTH OF THE ARRAY(LAST POSITION + 1)
		// IN CASE A FREE RESOURCE ISN'T FOUND IN THE LOOP
		var xhrsend = xi.length;

		// GO THROUGH AVAILABLE xi VALUES
		for (var i=0; i<xi.length; i++) {
			// IF IT'S 1 (AVAILABLE), ALLOCATE IT FOR USE AND BREAK
			if (xi[i] == 1) {
				xi[i] = 0;
				xhrsend = i;
				break;
			}
		}
		// SET TO 0 SINCE IT'S NOW ALLOCATED FOR USE
		xi[xhrsend] = 0;

		// SET UP THE REQUEST
		if (window.XMLHttpRequest) {
			xhr[xhrsend] = new XMLHttpRequest();
			if (xhr[xhrsend].overrideMimeType) {
				xhr[xhrsend].overrideMimeType('text/' + type);
			}
		}
		else if (window.ActiveXObject) {
			try {
				xhr[xhrsend] = new ActiveXObject("Msxml2.XMLHTTP");
			} 
			catch (e) {if(log) log.erro(e);
				try {
					xhr[xhrsend] = new ActiveXObject("Microsoft.XMLHTTP");
				} catch (e) {if(log) log.erro(e);}
			}
		} 
		 
		return (xhrsend);
	 }
	 
	 function ajax(opcoes){
		try{
			var set = {
				method : 'GET',//padrão
				endereco : false,
				parametros : false, //string completa ou object
				callback : false,
				onError : false,
				nome : 'ajax request',

				headers:false,
			
				testando : false,
				assincrono : false,
				ignoreError : false,
				alertError : false,
				retornoPHP : true
			};

			//CHANGE THE SETTINGS PASSING A OBJECT
			if(opcoes && opcoes instanceof Object){
				for (var property in opcoes) {
				    if (opcoes.hasOwnProperty(property)) {				    	
				        set[property] = opcoes[property];
				    }
				}
			}
			
			var xhri = xhrRequest('html');
			if(set.testando || set.callback || set.onError){			
				if(!set.callback) set.callback = function(){};
				if(!set.onError) set.onError = function(){};

				xhr[xhri].onreadystatechange = function() {					
					if(set.testando && xhr[xhri].readyState != 0){
						log.ins("READY STATE "+xhr[xhri].readyState+"/"+xhr[xhri].status);
					}
					if (xhr[xhri].readyState == 4 && xhr[xhri].status == 200) {//SUCESS
						var ret = xhr[xhri].responseText;
						window.console.log(ret);
						if(set.testando){ 
							alert(txt);
						}						
						if(set.callback) set.callback(xhr[xhri].responseText);

						//IMPORTANT! THIS LINES FREE THE INDEX FOR USE, OTHERWISE ANOTHER ONE IS CREATED
						xi[xhri] = 1;
						xhr[xhri] = null;
					}
					if (xhr[xhri].readyState == 4 && xhr[xhri].status == 500){//FAILURE
						if(set.testando){ 
							alert(retornoPHP(xhr[xhri].responseText));
						}
						if(set.onError) set.onError(xhr[xhri].responseText);
						
						//IMPORTANT! THIS LINES FREE THE INDEX FOR USE, OTHERWISE ANOTHER ONE IS CREATED
						xi[xhri] = 1;
						xhr[xhri] = null;
					}
				}
			 }			

			if(set.parametros && isString(set.parametros)){
				var complemento = set.parametros;
			} else {//is object
				var complemento = '';
				if(set.parametros && set.parametros instanceof Object){				
					for (var property in set.parametros) {
					    if (set.parametros.hasOwnProperty(property)) {
					    	complemento += "&" + encodeURIComponent(property) + "=" + encodeURIComponent(set.parametros[property]);
					    }
					}				
				 }				
				if(complemento) complemento = complemento.slice(1); 
			}
			if(set.testando) alert(set.endereco +'?'+ complemento);

			var requestBody;
			var enderecoCompleto = set.endereco;
			if(set.method == 'GET'){
				var enderecoCompleto = set.endereco +'?'+ complemento;				
			} else if(set.method == 'POST'){
				requestBody = complemento;		
			}

			xhr[xhri].open(set.method, enderecoCompleto, set.assincrono); ///DIFERENT FOR METOD GET AND POST!

			if(set.headers && set.headers instanceof Object){
				for (var property in set.headers) {
				    if (set.headers.hasOwnProperty(property)) {
				    	xhr[xhri].setRequestHeader(property, set.headers[property]);
				    }
				}				
			 }
			xhr[xhri].send(requestBody);

			if(!set.testando && !set.callback && !set.onError){
				xi[xhri] = 1;
				xhr[xhri] = null;
			}
		} catch(err){
			if(!set.ignoreError){				
				if(set.alertError) alert(err.message);
				if(set.onError && set.onError != function(){}) return set.onError(err)
				if(err.message == 'Failure'){
					alert('failure');
				} else { throw err; }
			}
		}
	 }

	 //THEN USE IT SENDIND A OBJECT, LIKE THIS

	var ajaxSetup = {
		endereco : 'http://gen.lib.rus.ec/search.php',
		parametros : {
			req: 'busca',
			lg_topic:'libgen',
			open:'0',
			view:'simple',
			res:'25',
			phrase:'1',
			column:'def'
		},
		callback : function(ret){
			alert(ret);
		}
	};
	ajax(ajaxSetup);

	 
Post Reply