1
0
mirror of https://github.com/salvadordf/CEF4Delphi.git synced 2025-08-04 21:32:54 +02:00

Use GlobalCEFApp.OnFocusedNodeChanged in DOMVisitor to fill an input element

This commit is contained in:
salvadordf
2022-04-18 10:39:28 +02:00
parent 52c42ad27d
commit a7d682639a
6 changed files with 67 additions and 17 deletions

View File

@ -1,7 +1,7 @@
<?xml version="1.0" encoding="UTF-8"?>
<CONFIG>
<ProjectOptions>
<Version Value="11"/>
<Version Value="12"/>
<PathDelim Value="\"/>
<General>
<Flags>
@ -9,9 +9,9 @@
<MainUnitHasCreateFormStatements Value="False"/>
<MainUnitHasTitleStatement Value="False"/>
<MainUnitHasScaledStatement Value="False"/>
<CompatibilityMode Value="True"/>
</Flags>
<SessionStorage Value="InProjectDir"/>
<MainUnit Value="0"/>
<Title Value="DOMVisitor"/>
<UseAppBundle Value="False"/>
<ResourceType Value="res"/>
@ -25,7 +25,6 @@
</PublishOptions>
<RunParams>
<FormatVersion Value="2"/>
<Modes Count="0"/>
</RunParams>
<RequiredPackages Count="2">
<Item1>

View File

@ -73,7 +73,8 @@ const
RETRIEVEDOM_MSGNAME_PARTIAL = 'retrievedompartial';
RETRIEVEDOM_MSGNAME_FULL = 'retrievedomfull';
FRAMEIDS_MSGNAME = 'getframeids';
CONSOLE_MSG_PREAMBLE = 'DOMVISITOR';
CONSOLE_MSG_PREAMBLE = 'DOMVISITOR';
FILLUSERNAME_MSGNAME = 'fillusername';
NODE_ID = 'keywords';
@ -400,6 +401,27 @@ begin
end;
end;
procedure GlobalCEFApp_OnFocusedNodeChanged(const browser : ICefBrowser;
const frame : ICefFrame;
const node : ICefDomNode);
var
TempMsg : ICefProcessMessage;
begin
// If the user focuses the username box then we send a message to the main
// process to fill it with a username value.
if (frame <> nil) and
frame.IsValid and
(node <> nil) and
(CompareText(node.ElementTagName, 'input') = 0) and
(CompareText(node.GetElementAttribute('id'), 'username') = 0) then
try
TempMsg := TCefProcessMessageRef.New(FILLUSERNAME_MSGNAME);
frame.SendProcessMessage(PID_BROWSER, TempMsg);
finally
TempMsg := nil;
end;
end;
procedure GlobalCEFApp_OnProcessMessageReceived(const browser : ICefBrowser;
const frame : ICefFrame;
sourceProcess : TCefProcessId;
@ -445,7 +467,8 @@ end;
procedure CreateGlobalCEFApp;
begin
GlobalCEFApp := TCefApplication.Create;
GlobalCEFApp.OnProcessMessageReceived := GlobalCEFApp_OnProcessMessageReceived;
GlobalCEFApp.OnProcessMessageReceived := GlobalCEFApp_OnProcessMessageReceived;
GlobalCEFApp.OnFocusedNodeChanged := GlobalCEFApp_OnFocusedNodeChanged;
// Enabling the debug log file for then DOM visitor demo.
// This adds lots of warnings to the console, specially if you run this inside VirtualBox.
@ -721,7 +744,10 @@ begin
Clipboard.AsText := message.ArgumentList.GetString(0);
StatusText := 'Frame IDs copied to the clipboard in the render process.';
Result := True;
end;
end
else
if (message.Name = FILLUSERNAME_MSGNAME) and (frame <> nil) and frame.IsValid then
frame.ExecuteJavaScript('document.getElementById("username").value = "myusername";', 'about:blank', 0);
if Result then
PostMessage(Handle, MINIBROWSER_SHOWSTATUSTEXT, 0, 0);