|
5 | 5 | using System; |
6 | 6 | using System.Collections.Generic; |
7 | 7 | using System.Collections.ObjectModel; |
| 8 | +using System.IO; |
8 | 9 | using System.Threading; |
9 | 10 | using System.Threading.Tasks; |
10 | 11 | using System.Windows; |
11 | 12 | using System.Windows.Input; |
12 | 13 | using CefSharp.Example; |
| 14 | +using CefSharp.Example.Handlers; |
13 | 15 | using CefSharp.Wpf.Example.Controls; |
14 | 16 | using CefSharp.Wpf.Example.ViewModels; |
15 | 17 | using Microsoft.Win32; |
@@ -146,6 +148,54 @@ private void CustomCommandBinding(object sender, ExecutedRoutedEventArgs e) |
146 | 148 | }); |
147 | 149 | } |
148 | 150 | } |
| 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 | + } |
149 | 199 |
|
150 | 200 | if (param == "ToggleSidebar") |
151 | 201 | { |
|
0 commit comments