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

View File

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

View File

@@ -79,6 +79,7 @@ const
RETRIEVEDOM_MSGNAME_FULL = 'retrievedomfull'; RETRIEVEDOM_MSGNAME_FULL = 'retrievedomfull';
FRAMEIDS_MSGNAME = 'getframeids'; FRAMEIDS_MSGNAME = 'getframeids';
CONSOLE_MSG_PREAMBLE = 'DOMVISITOR'; CONSOLE_MSG_PREAMBLE = 'DOMVISITOR';
FILLUSERNAME_MSGNAME = 'fillusername';
NODE_ID = 'keywords'; NODE_ID = 'keywords';
@@ -414,6 +415,27 @@ begin
end; end;
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; procedure GlobalCEFApp_OnProcessMessageReceived(const browser : ICefBrowser;
const frame : ICefFrame; const frame : ICefFrame;
sourceProcess : TCefProcessId; sourceProcess : TCefProcessId;
@@ -460,6 +482,7 @@ procedure CreateGlobalCEFApp;
begin begin
GlobalCEFApp := TCefApplication.Create; 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. // 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. // 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); Clipboard.AsText := message.ArgumentList.GetString(0);
StatusText := 'Frame IDs copied to the clipboard in the render process.'; StatusText := 'Frame IDs copied to the clipboard in the render process.';
Result := True; 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 if Result then
PostMessage(Handle, MINIBROWSER_SHOWSTATUSTEXT, 0, 0); PostMessage(Handle, MINIBROWSER_SHOWSTATUSTEXT, 0, 0);

View File

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

View File

@@ -74,6 +74,7 @@ const
RETRIEVEDOM_MSGNAME_FULL = 'retrievedomfull'; RETRIEVEDOM_MSGNAME_FULL = 'retrievedomfull';
FRAMEIDS_MSGNAME = 'getframeids'; FRAMEIDS_MSGNAME = 'getframeids';
CONSOLE_MSG_PREAMBLE = 'DOMVISITOR'; CONSOLE_MSG_PREAMBLE = 'DOMVISITOR';
FILLUSERNAME_MSGNAME = 'fillusername';
NODE_ID = 'keywords'; NODE_ID = 'keywords';
@@ -400,6 +401,27 @@ begin
end; end;
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; procedure GlobalCEFApp_OnProcessMessageReceived(const browser : ICefBrowser;
const frame : ICefFrame; const frame : ICefFrame;
sourceProcess : TCefProcessId; sourceProcess : TCefProcessId;
@@ -446,6 +468,7 @@ procedure CreateGlobalCEFApp;
begin begin
GlobalCEFApp := TCefApplication.Create; 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. // 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. // 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); Clipboard.AsText := message.ArgumentList.GetString(0);
StatusText := 'Frame IDs copied to the clipboard in the render process.'; StatusText := 'Frame IDs copied to the clipboard in the render process.';
Result := True; 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 if Result then
PostMessage(Handle, MINIBROWSER_SHOWSTATUSTEXT, 0, 0); PostMessage(Handle, MINIBROWSER_SHOWSTATUSTEXT, 0, 0);

View File

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