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 @@
<Project xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<PropertyGroup>
<ProjectGuid>{3B58D3C2-1F7F-4146-A307-5F778CD7C586}</ProjectGuid>
<ProjectVersion>19.3</ProjectVersion>
<ProjectVersion>19.4</ProjectVersion>
<FrameworkType>VCL</FrameworkType>
<MainSource>DOMVisitor.dpr</MainSource>
<Base>True</Base>
@ -1287,17 +1287,17 @@
<Operation>1</Operation>
</Platform>
</DeployClass>
<ProjectRoot Platform="OSX32" Name="$(PROJECTNAME).app"/>
<ProjectRoot Platform="OSX64" Name="$(PROJECTNAME).app"/>
<ProjectRoot Platform="Win32" Name="$(PROJECTNAME)"/>
<ProjectRoot Platform="Android64" Name="$(PROJECTNAME)"/>
<ProjectRoot Platform="Android" Name="$(PROJECTNAME)"/>
<ProjectRoot Platform="Win64" Name="$(PROJECTNAME)"/>
<ProjectRoot Platform="OSXARM64" Name="$(PROJECTNAME).app"/>
<ProjectRoot Platform="Android64" Name="$(PROJECTNAME)"/>
<ProjectRoot Platform="iOSDevice32" Name="$(PROJECTNAME).app"/>
<ProjectRoot Platform="iOSDevice64" Name="$(PROJECTNAME).app"/>
<ProjectRoot Platform="Linux64" Name="$(PROJECTNAME)"/>
<ProjectRoot Platform="iOSSimulator" Name="$(PROJECTNAME).app"/>
<ProjectRoot Platform="Linux64" Name="$(PROJECTNAME)"/>
<ProjectRoot Platform="OSX32" Name="$(PROJECTNAME).app"/>
<ProjectRoot Platform="OSX64" Name="$(PROJECTNAME).app"/>
<ProjectRoot Platform="OSXARM64" Name="$(PROJECTNAME).app"/>
<ProjectRoot Platform="Win32" Name="$(PROJECTNAME)"/>
<ProjectRoot Platform="Win64" Name="$(PROJECTNAME)"/>
</Deployment>
<Platforms>
<Platform value="Win32">True</Platform>

View File

@ -15,7 +15,6 @@ object DOMVisitorFrm: TDOMVisitorFrm
OnCreate = FormCreate
OnDestroy = FormDestroy
OnShow = FormShow
PixelsPerInch = 96
TextHeight = 13
object CEFWindowParent1: TCEFWindowParent
Left = 0

View File

@ -79,6 +79,7 @@ const
RETRIEVEDOM_MSGNAME_FULL = 'retrievedomfull';
FRAMEIDS_MSGNAME = 'getframeids';
CONSOLE_MSG_PREAMBLE = 'DOMVISITOR';
FILLUSERNAME_MSGNAME = 'fillusername';
NODE_ID = 'keywords';
@ -414,6 +415,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;
@ -460,6 +482,7 @@ procedure CreateGlobalCEFApp;
begin
GlobalCEFApp := TCefApplication.Create;
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.
@ -738,7 +761,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);

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);

View File

@ -2,7 +2,7 @@
"UpdateLazPackages" : [
{
"ForceNotify" : true,
"InternalVersion" : 386,
"InternalVersion" : 387,
"Name" : "cef4delphi_lazarus.lpk",
"Version" : "100.0.23.0"
}