Skip to content

Commit

Permalink
add DOM access example
Browse files Browse the repository at this point in the history
  • Loading branch information
dliw committed Jun 10, 2014
1 parent 6f20c20 commit 3d1c5d2
Show file tree
Hide file tree
Showing 7 changed files with 542 additions and 0 deletions.
Binary file added Examples/DOMAccess/dom.ico
Binary file not shown.
211 changes: 211 additions & 0 deletions Examples/DOMAccess/dom.lpi
Original file line number Diff line number Diff line change
@@ -0,0 +1,211 @@
<?xml version="1.0" encoding="UTF-8"?>
<CONFIG>
<ProjectOptions>
<Version Value="9"/>
<General>
<SessionStorage Value="InProjectDir"/>
<MainUnit Value="0"/>
<Title Value="dom"/>
<ResourceType Value="res"/>
<UseXPManifest Value="True"/>
<Icon Value="0"/>
</General>
<i18n>
<EnableI18N LFM="False"/>
</i18n>
<VersionInfo>
<StringTable ProductVersion=""/>
</VersionInfo>
<MacroValues Count="1">
<Macro1 Name="LCLWidgetType" Value="gtk2"/>
</MacroValues>
<BuildModes Count="3">
<Item1 Name="Linux" Default="True"/>
<Item2 Name="Win32">
<MacroValues Count="1">
<Macro2 Name="LCLWidgetType" Value="win32"/>
</MacroValues>
<CompilerOptions>
<Version Value="11"/>
<Target>
<Filename Value="simple32"/>
</Target>
<SearchPaths>
<IncludeFiles Value="$(ProjOutDir)"/>
<UnitOutputDirectory Value="lib/$(TargetCPU)-$(TargetOS)"/>
</SearchPaths>
<Parsing>
<SyntaxOptions>
<AllowLabel Value="False"/>
</SyntaxOptions>
</Parsing>
<CodeGeneration>
<RelocatableUnit Value="True"/>
<Checks>
<IOChecks Value="True"/>
<RangeChecks Value="True"/>
<OverflowChecks Value="True"/>
</Checks>
<TargetCPU Value="i386"/>
<TargetOS Value="win32"/>
</CodeGeneration>
<Linking>
<Options>
<Win32>
<GraphicApplication Value="True"/>
</Win32>
</Options>
</Linking>
<Other>
<CompilerMessages>
<UseMsgFile Value="True"/>
</CompilerMessages>
<CompilerPath Value="$(CompPath)"/>
</Other>
</CompilerOptions>
</Item2>
<Item3 Name="Win64">
<MacroValues Count="1">
<Macro2 Name="LCLWidgetType" Value="win32"/>
</MacroValues>
<CompilerOptions>
<Version Value="11"/>
<Target>
<Filename Value="simple64"/>
</Target>
<SearchPaths>
<IncludeFiles Value="$(ProjOutDir)"/>
<UnitOutputDirectory Value="lib/$(TargetCPU)-$(TargetOS)"/>
</SearchPaths>
<Parsing>
<SyntaxOptions>
<AllowLabel Value="False"/>
</SyntaxOptions>
</Parsing>
<CodeGeneration>
<RelocatableUnit Value="True"/>
<Checks>
<IOChecks Value="True"/>
<RangeChecks Value="True"/>
<OverflowChecks Value="True"/>
</Checks>
<TargetCPU Value="x86_64"/>
<TargetOS Value="win64"/>
</CodeGeneration>
<Linking>
<Options>
<Win32>
<GraphicApplication Value="True"/>
</Win32>
</Options>
</Linking>
<Other>
<CompilerMessages>
<UseMsgFile Value="True"/>
</CompilerMessages>
<CompilerPath Value="$(CompPath)"/>
</Other>
</CompilerOptions>
</Item3>
<SharedMatrixOptions Count="2">
<Item1 ID="661871476567" Modes="Linux" Type="IDEMacro" MacroName="LCLWidgetType" Value="gtk2"/>
<Item2 ID="279904151772" Modes="Win32,Win64" Type="IDEMacro" MacroName="LCLWidgetType" Value="win32"/>
</SharedMatrixOptions>
</BuildModes>
<PublishOptions>
<Version Value="2"/>
<IncludeFileFilter Value="*.(pas|pp|inc|lfm|lpr|lrs|lpi|lpk|sh|xml)"/>
<ExcludeFileFilter Value="*.(bak|ppu|o|so);*~;backup"/>
</PublishOptions>
<RunParams>
<local>
<FormatVersion Value="1"/>
</local>
<environment>
<UserOverrides Count="1">
<Variable0 Name="LD_LIBRARY_PATH" Value="."/>
</UserOverrides>
</environment>
</RunParams>
<RequiredPackages Count="2">
<Item1>
<PackageName Value="CEF3"/>
</Item1>
<Item2>
<PackageName Value="LCL"/>
</Item2>
</RequiredPackages>
<Units Count="3">
<Unit0>
<Filename Value="dom.lpr"/>
<IsPartOfProject Value="True"/>
<UnitName Value="dom"/>
</Unit0>
<Unit1>
<Filename Value="main.pas"/>
<IsPartOfProject Value="True"/>
<ComponentName Value="Mainform"/>
<HasResources Value="True"/>
<ResourceBaseClass Value="Form"/>
<UnitName Value="Main"/>
</Unit1>
<Unit2>
<Filename Value="handler.pas"/>
<IsPartOfProject Value="True"/>
<UnitName Value="Handler"/>
</Unit2>
</Units>
</ProjectOptions>
<CompilerOptions>
<Version Value="11"/>
<Target>
<Filename Value="dom"/>
</Target>
<SearchPaths>
<IncludeFiles Value="$(ProjOutDir)"/>
<UnitOutputDirectory Value="lib/$(TargetCPU)-$(TargetOS)"/>
</SearchPaths>
<Parsing>
<SyntaxOptions>
<IncludeAssertionCode Value="True"/>
<AllowLabel Value="False"/>
</SyntaxOptions>
</Parsing>
<CodeGeneration>
<Checks>
<IOChecks Value="True"/>
<RangeChecks Value="True"/>
<OverflowChecks Value="True"/>
<StackChecks Value="True"/>
</Checks>
<TargetCPU Value="x86_64"/>
<TargetOS Value="linux"/>
</CodeGeneration>
<Linking>
<Options>
<Win32>
<GraphicApplication Value="True"/>
</Win32>
</Options>
</Linking>
<Other>
<CompilerMessages>
<UseMsgFile Value="True"/>
</CompilerMessages>
<CompilerPath Value="$(CompPath)"/>
</Other>
</CompilerOptions>
<Debugging>
<Exceptions Count="3">
<Item1>
<Name Value="EAbort"/>
</Item1>
<Item2>
<Name Value="ECodetoolError"/>
</Item2>
<Item3>
<Name Value="EFOpenError"/>
</Item3>
</Exceptions>
</Debugging>
</CONFIG>
21 changes: 21 additions & 0 deletions Examples/DOMAccess/dom.lpr
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
Program dom;

{$MODE objfpc}{$H+}

Uses
{$IFDEF UNIX}
cthreads,
{$ENDIF}
Interfaces, // this includes the LCL widgetset
Forms, Main, handler;

{$R *.res}

begin
Application.Title := 'DOMAccess';
RequireDerivedFormResource := True;
Application.Initialize;
Application.CreateForm(TMainform, Mainform);
Application.Run;
end.

86 changes: 86 additions & 0 deletions Examples/DOMAccess/handler.pas
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
Unit Handler;

{$MODE objfpc}{$H+}

(*
* Everything in here is called from a render process, so there is no access to GUI and all the
* data of the main process.
*)

Interface

Uses
Classes, SysUtils,
cef3types, cef3intf, cef3ref, cef3own;

Type
{ Custom handler for the render process }
TCustomRenderProcessHandler = class(TCefRenderProcessHandlerOwn)
protected
function OnProcessMessageReceived(const browser: ICefBrowser; sourceProcess: TCefProcessId;
const message: ICefProcessMessage): Boolean; override;
end;

Implementation

Var
// Don't access outside of callbacks
// Enables access to browser in callbacks, necessary unless nestedprocvars is used
ActiveBrowser: ICefBrowser;

procedure visitDOM(const document: ICefDomDocument);
Var
message: ICefProcessMessage;
begin
message := TCefProcessMessageRef.New('domdata');
With message.ArgumentList do
begin
SetString(0, document.GetTitle);
SetString(1, document.GetSelectionAsText);
end;

ActiveBrowser.SendProcessMessage(PID_BROWSER, message);
end;

procedure clickListener(const event: ICefDomEvent);
Var
message: ICefProcessMessage;
begin
message := TCefProcessMessageRef.New('click');
With message.ArgumentList do
begin
SetString(0, event.Target.Name);
end;

ActiveBrowser.SendProcessMessage(PID_BROWSER, message);
end;

procedure registerListener(const document: ICefDomDocument);
begin
document.Body.AddEventListenerProc('click', False, @clickListener);
end;

{ TCustomRenderProcessHandler }

function TCustomRenderProcessHandler.OnProcessMessageReceived(const browser : ICefBrowser;
sourceProcess : TCefProcessId; const message : ICefProcessMessage) : Boolean;
begin
ActiveBrowser := browser;
Case message.Name of
'visitdom':
begin
browser.MainFrame.VisitDomProc(@visitDOM);
Result := True;
end;
'addListener':
begin
browser.MainFrame.VisitDomProc(@registerListener);
Result := True;
end;
Else
Result := inherited;
end;
end;

end.

Loading

0 comments on commit 3d1c5d2

Please sign in to comment.