Jump to content

Recommended Posts

  • Moderators

redirecting 'console.log' or 'writeln' output proves to be very simple :

have a form with a Memo component :

procedure TForm1.InitializeObject;
begin
  inherited;
  {$I 'Form1:impl'}

  W3Memo1.Text := 'Console.log : ' + #10;

  browserapi.console.log := procedure(text:variant)
  begin
    W3Memo1.Text := W3Memo1.Text + #10 + text;
  end;

  writeln('aaa');
  browserapi.console.log('bbb');
end;

all subsequent calls to writeln and/or console.log are now redirected to the Memo component.

 

Link to post
Share on other sites
  • Moderators

This mechanism can be used to redefine more built-in functions. Funny.

The code below redefines 'ShowMessage' to redirect its contents to a Memo :

procedure TForm1.InitializeObject;
begin
  inherited;
  {$I 'Form1:impl'}

  W3Memo1.Text := 'ShowMessage : ' + #10;

  browserapi.window.alert := procedure(text:variant)
  begin
    W3Memo1.Text := W3Memo1.Text + #10 + text;
  end;

  ShowMessage('ccc');
  browserapi.window.alert('ddd');

end;

 

or combine the two and redefine 'ShowMessage' to redirect its contents to the 'Writeln' function, which in its turn is redirected to the Memo :

procedure TForm1.InitializeObject;
begin
  inherited;
  {$I 'Form1:impl'}

  W3Memo1.Text := 'Console.log : ' + #10;

  //WriteLn redirects to Memo
  browserapi.console.log := procedure(text:variant)
  begin
    W3Memo1.Text := W3Memo1.Text + #10 + text;
  end;

  //ShowMessage redirects to WriteLn
  browserapi.window.alert := procedure(text:variant)
  begin
    writeln(text);
  end;

  writeln('aaa');                  
  browserapi.console.log('bbb');

  ShowMessage('ccc');              
  browserapi.window.alert('ddd');

end;

 

Link to post
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...