SRL Archive - Research & Development Lounge

APPA Javascript Interface + Example Script


bg5

2013-10-29 23:14:54

@nielsie95 hasn't told you that, but APPA has ability to inject JavaScript code into site, like e.x. firefox's console. So when writing script in APPA you have all that superpowers, which JS provides to your browser.


NOTE:
JS standards of IE (on which APPA is based) are at some points a little different then in FF.
You don't have debugger, which would tell you why function doesn't work properly. So test your code somewhere else before you put it into script (for example here or use FF/chrome console.




This is like normal procedure. Doesn't return value and runs JS code asynchronously.

procedure appaExecJS (JScode:string);
Example:
appaExecJS( 'document.write("Hello World!");' );




This runs JS code and returns result. This function is synchro ,so Simba will wait until it finish!
As first string you put JS code to exec. As second argument you put the name of variable (same as in JS code),of which value will be returned to Simba.
   function appaExecJSFunc (JScode,JSResultName:string) : string;
Example:
msg := appaExecJSFunc('var a =8; var b= 10; var res=a+b;','res');
writeln('8 + 10 = '+msg;)
Example2:

str1 := "Hello";
str2 := "World";

msg := appaExecJSFunc('var a ="'+str1+'" ; var b="'+str2+'"; var MyStr = a+" "+b+" "+"of JS !";','MyStr');
writeln(msg);




Loads into the site external JS library from URL. So you don't need to write all those functions in Simba.
  procedure appaLoadJSLib (url : string);



Gets x,y coords of object of given ID. This is just an example of function, which does something more complex. I haven't made more, because I haven't seen a point yet.


procedure GetDOMObjPos (var x,y:integer; objectID:string);


This is an open project: Feel free to copy ,modify and use this code, also share your own functions and ideas.

bg5

2013-10-29 23:16:56

Example script, just copy/paste and run:

program new;

{$loadlib APPA_Simba}
procedure appaNavigate(URL:string);
begin
if appa_CurrentPage <> URL then
begin
appa_Navigate(URL);
end;
end;

procedure appaMouse (x ,y : integer; action : variant);
begin
appa_MoveMouse(x,y);
if VarType(action) = varBoolean then
begin
appa_HoldMouse(x,y,action);
wait(10);
appa_ReleaseMouse(x,y,action);
end;
end;

function GetTargetsHandle : TintegerArray;
var
s : Tstringarray;
a ,hi : integer;
begin
appa_GetTargets(s);
writeln(s);
hi := High(s);
SetLength(Result,hi+1);
for a:=0 to hi do
Result[a] := strtoint(ReplaceRegExpr('[^0-9]',s[a],'',FALSE));
end;


function FindAndSetTarget(TitlePrefix: String; SetAsTarget: Boolean; InternalFrameHandle : integer ): Boolean;
var
T: TSysProcArr;
I: Integer;
begin
T:= GetProcesses();
for I := 0 to high(T) do
if Pos(TitlePrefix, T[i].Title) > 0 then
begin
Result := True;
if InternalFrameHandle > 0 then T[i].Handle := InternalFrameHandle;
if SetAsTarget then
begin
SetTarget(T[i]);
ActivateClient;
end;
end;
end;

procedure appaMaximalize(customWoffset,customHoffset : integer);
var t1,t2,tt,w,h : integer;
begin
t1 := GetImageTarget;
t2:= GetKeyMouseTarget;
SetDesktopAsClient;
GetClientDimensions(w,h);
tt := GetImageTarget;
FreeTarget(tt);
SetImageTarget(t1);
SetKeyMouseTarget(t2);
if customWoffset <>0 then w := w + customWoffset;
if customHoffset <>0 then h := h + customHoffset;
appa_SetSize(w,h);
end;

function WaitFindDTMEx(DTM: integer; var x, y: integer; xs, ys, xe, ye, WaitPerLoop, MaxTime: integer): boolean;
var
t: integer;
begin
t := (getSystemTime + MaxTime);

while (getSystemTime < t) do
begin
if (findDTM(DTM, x, y, xs, ys, xe, ye)) then
begin
result := true;
break;
end;

wait(WaitPerLoop);
end;
end;

function BetweenEx(const StartStr, EndStr, Data: string): string;
var
StartPos, StartLen, EndPos: LongInt;
begin
StartLen := Length(StartStr);
StartPos := Pos(StartStr, data);
if (StartPos > 0) then
begin
IncEx(StartPos, StartLen);

EndPos := PosEx(EndStr, data, StartPos);
if (EndPos > 0) then
Result := Copy(Data, StartPos, EndPos - StartPos);
end;
end;

procedure appaExecJS (JScode:string);
var str:string;
begin
str := 'javascript:'+JScode+'void(0);';
if (length(str) > 508) then
begin
writeln('You exceeded string length limit which is 508 chars.Operation aborted');
exit;
end;
appa_Navigate(str);
end;

function appaExecJSFunc (JScode,JSResultName:string) : string;
var loadBuffor,str,html,hash : string;
var t : integer;
begin
hash :='APPAsh'+tostr(random(999));
loadBuffor:='if(document.getElementById("APPAbuff")==null){var d=document.createElement("div");d.id="APPAbuff";d.className='+JSResultName+';d.title="'+hash+'";document.body.appendChild(d);}else{var d=document.getElementById("APPAbuff");d.className='+JSResultName+';d.title="'+hash+'";}';
str := 'javascript:'+JScode+loadBuffor+'void(0);';
if (length(str) > 508) then
begin
writeln('You exceeded string length limit which is 508 chars.Operation aborted');
exit;
end;
appa_Navigate(str);
T := GetSystemTime;
while(GetSystemTime - T <5000) do
begin
html := appa_GetHTML();
if (Pos(hash,html)>0) then
begin
result := BetweenEx('id="APPAbuff" class=',' tit',html);
if (result = '') then
result := BetweenEx('id=APPAbuff class=',' tit',html);
break;
end;
wait(30);
end;
Result :=ReplaceWrap(Result,'"','',[rfReplaceAll]);
end;

procedure appaLoadJSLib (url : string);
begin
appaExecJSFunc('var js=document.createElement("script");js.type="text/javascript";js.src = "'+url+'";document.body.appendChild(js);var r=0;','r');
end;


///////////////////// GAME RELATED SECTION /////////////////////////////////////
////////////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////////

var w,h : integer;
var bmp ,t,x,y,Start_Button,Hunt_Now: integer;
var targets :TintegerArray;

procedure GameLoop;
var x,y,AmmoCount : integer;
begin
GetClientDimensions(w,h);
AmmoCount := 10;
while not(FindDTM(Hunt_Now,x,y,0,0,w-1,h-1)) do
begin
if FindColor(x,y,7790931,0,0,w-1,h-1) then
begin
appamouse(x,y,true);
AmmoCount := AmmoCount - 1;
if AmmoCount <= 1 then
begin
wait(100);
appamouse(460,372,true);
writeln('Reloading');
AmmoCount := 10;
end;
wait(500);
end;

wait(100);
end;

end;

var wt,time:integer;
begin
Start_Button := DTMFromString('mWAAAAHicY2FgYDgNxDuAeAMQLwTi20D8BIivQ9npbo4M////h2N0wIiGQQAAZkYQMw==');
Hunt_Now := DTMFromString('mbQAAAHicY2VgYDgLxB+AeDcQrwDiCUAsysjAIAjE4kDMD8TyQDxx7nyG////w/H3Hz8YsAFGLBgMAIHDEdY=');

appa_ShowForm;
wait(500);
appaNavigate('http://www.flashgames247.com/play/529.html');
wait(100);
T := GetSystemTime;
while (not appa_PageLoaded)and(GetSystemTime<T+5000) do wait(50);
appaMaximalize(0,-150);
wait(100);
appaExecJS('var x=document.getElementsByTagName(''div'')[1];x.removeChild(x.childNodes[1]);x.removeChild(x.childNodes[0]);'); //top add
wait(100);
appaExecJS('var x=document.getElementsByTagName(''div'')[7];x.removeChild(x.childNodes[1]);x.removeChild(x.childNodes[0]);'); //bottom add
wait(100);
appaExecJS('var x=document.getElementsByTagName(''div'')[1];var i=document.createElement("img");i.src="http://villavu.com/forum/images/misc/srlbanner.png";x.appendChild(i);');
wait(300);
appaExecJS('var x=document.getElementsByTagName(''div'')[1];x.style.fontSize="25px";x.appendChild(document.createTextNode(" APPA Javascript Interface"));');
wait(100);
appaExecJS('var x=document.getElementById(''cpmstar-site-skin-r'');x.parentNode.removeChild(x);'); // killing adds
wait(100);
appaExecJS('var x=document.getElementById(''cpmstar-site-skin-l'');x.parentNode.removeChild(x);');
wait(100);
appaExecJS('var x=document.getElementsByTagName("table");x[5].parentNode.removeChild(x[5]);'); // 2nd bottom add

wait(1000);
appa_Caption('APPAtest');
targets := GetTargetsHandle;
appa_SetTargetHandle(targets[High(targets)]);
FindAndSetTarget('APPAtest',TRUE,targets[High(targets)]);

GetClientDimensions(w,h);
while CountColor(0,0,0,w-1,h-1) > 50000 do
wait(1000);

time := gettimerunning();

while (gettimerunning()-time<10000) do
begin
if FindDTM(Start_Button,x,y,0,0,w-1,h-1)or FindDTM(Hunt_Now,x,y,0,0,w-1,h-1) then
begin
if FindDTM(Start_Button,x,y,0,0,w-1,h-1) then
begin
appaMouse(x,y,true);
WaitFindDTMEx(Hunt_Now,x,y,0,0,w-1,h-1,200,5000);
appaMouse(x,y,true);
end else
appaMouse(x,y,true);
writeln('Start');
break;
end;

end;
GameLoop();
end. z