Skip to content

Commit d6ca253

Browse files
committed
WPF Example - Add LoadExtension Test
Uses the set_page_color that CEF uses for testing
1 parent c63c301 commit d6ca253

File tree

2 files changed

+51
-0
lines changed

2 files changed

+51
-0
lines changed

CefSharp.Wpf.Example/MainWindow.xaml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,7 @@
4848
<MenuItem Header="_Async JSB Task Tests" Command="controls:CefSharpCommands.CustomCommand" CommandParameter="AsyncJsbTaskTests" />
4949
<MenuItem Header="_Resize Hack Tests" Command="controls:CefSharpCommands.CustomCommand" CommandParameter="ResizeHackTests" />
5050
<MenuItem Header="_Google Service Worker Demo" Command="controls:CefSharpCommands.OpenTabCommand" CommandParameter="https://googlechrome.github.io/samples/service-worker/basic/"/>
51+
<MenuItem Header="Load Example Extension" Command="controls:CefSharpCommands.CustomCommand" CommandParameter="LoadExtension"/>
5152
</MenuItem>
5253
</Menu>
5354
<controls:NonReloadingTabControl x:Name="TabControl"

CefSharp.Wpf.Example/MainWindow.xaml.cs

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,11 +5,13 @@
55
using System;
66
using System.Collections.Generic;
77
using System.Collections.ObjectModel;
8+
using System.IO;
89
using System.Threading;
910
using System.Threading.Tasks;
1011
using System.Windows;
1112
using System.Windows.Input;
1213
using CefSharp.Example;
14+
using CefSharp.Example.Handlers;
1315
using CefSharp.Wpf.Example.Controls;
1416
using CefSharp.Wpf.Example.ViewModels;
1517
using Microsoft.Win32;
@@ -146,6 +148,54 @@ private void CustomCommandBinding(object sender, ExecutedRoutedEventArgs e)
146148
});
147149
}
148150
}
151+
if (param == "LoadExtension")
152+
{
153+
var browser = browserViewModel.WebBrowser;
154+
//The sample extension only works for http(s) schemes
155+
if (browser.Address.StartsWith("http"))
156+
{
157+
var requestContext = browser.GetBrowserHost().RequestContext;
158+
159+
var dir = Path.Combine(AppContext.BaseDirectory, @"..\..\..\..\CefSharp.Example\Extensions");
160+
dir = Path.GetFullPath(dir);
161+
if (!Directory.Exists(dir))
162+
{
163+
throw new DirectoryNotFoundException("Unable to locate example extensions folder - " + dir);
164+
}
165+
166+
var extensionHandler = new ExtensionHandler
167+
{
168+
LoadExtensionPopup = (url) =>
169+
{
170+
Dispatcher.BeginInvoke(new Action(() =>
171+
{
172+
var extensionWindow = new Window();
173+
174+
var extensionBrowser = new ChromiumWebBrowser(url);
175+
//extensionBrowser.IsBrowserInitializedChanged += (s, args) =>
176+
//{
177+
// extensionBrowser.ShowDevTools();
178+
//};
179+
180+
extensionWindow.Content = extensionBrowser;
181+
182+
extensionWindow.Show();
183+
}));
184+
},
185+
GetActiveBrowser = (extension, isIncognito) =>
186+
{
187+
//Return the active browser for which the extension will act upon
188+
return browser.GetBrowser();
189+
}
190+
};
191+
192+
requestContext.LoadExtensionsFromDirectory(dir, extensionHandler);
193+
}
194+
else
195+
{
196+
MessageBox.Show("The sample extension only works with http(s) schemes, please load a different website and try again", "Unable to load Extension");
197+
}
198+
}
149199

150200
if (param == "ToggleSidebar")
151201
{

0 commit comments

Comments
 (0)