-
Notifications
You must be signed in to change notification settings - Fork 7
RequestContextHandler
Mohammed Osama Mohamed Sayed Ahmed edited this page May 26, 2019
·
1 revision
public partial class RequestContextHandler : IRequestContextHandler {
private ICookieManager customCookieManager;
public ICookieManager GetCookieManager()
{
/// return null;
///
///
if (customCookieManager == null)
{
//In memory cookie manager
//customCookieManager = new CookieManager(null, persistSessionCookies: false, callback: null);
//Store cookies in cookies directory (user must have write permission to this folder)
customCookieManager = new CookieManager("cookies", persistSessionCookies: false, callback: null);
}
return customCookieManager;
//NOTE: DO NOT RETURN A NEW COOKIE MANAGER EVERY TIME
//This method will be called many times, you should return the same cookie manager within the scope
//of the RequestContext (unless you REALLY know what your doing)
//return new CookieManager("cookies", persistSessionCookies: false, callback: null);
//Default to using the Global cookieManager (default)
//return null;
}
public bool OnBeforePluginLoad(string mimeType, string url, bool isMainFrame, string topOriginUrl, WebPluginInfo pluginInfo, ref PluginPolicy pluginPolicy)
{
pluginPolicy = PluginPolicy.Allow;
return true;
// return false;
}
public void OnRequestContextInitialized(IRequestContext requestContext)
{
string error;
string errorMessage;
string SettingsPreference;
requestContext.SetPreference("spellcheck.dictionaries", new List<object> { "en-US", "ru", "ar", "zh-CN", "ja" }, out error);
requestContext.SetPreference("spellcheck.dictionary", "en-US", out error);
requestContext.SetPreference("webkit.webprefs.plugins_enabled", true, out errorMessage);
requestContext.SetPreference("spellcheck.use_spelling_service", true, out errorMessage);
requestContext.SetPreference("browser.enable_spellchecking", true, out errorMessage);
///
///
///
// with this chrome still asks for permission
requestContext.SetPreference("profile.managed_default_content_settings.media_stream", 1, out SettingsPreference); //1
requestContext.SetPreference("profile.managed_default_content_settings.media_stream_camera", 1, out SettingsPreference); //1
requestContext.SetPreference("profile.managed_default_content_settings.media_stream_mic", 1, out SettingsPreference); //1
requestContext.SetPreference("profile.managed_default_content_settings.Images", 1, out SettingsPreference); //1
requestContext.SetPreference("profile.managed_default_content_settings.plugins", 1, out SettingsPreference); //1
requestContext.SetPreference("profile.managed_default_content_settings.javascript", 1, out SettingsPreference); //1
requestContext.SetPreference("profile.managed_default_content_settings.geolocation", 1, out SettingsPreference); //1
requestContext.SetPreference("profile.managed_default_content_settings.important_site_info", 1, out SettingsPreference); //1
requestContext.SetPreference("profile.managed_default_content_settings.accessibility_events", 1, out SettingsPreference); //1
requestContext.SetPreference("profile.managed_default_content_settings.protocol_handler", 1, out SettingsPreference); //1
requestContext.SetPreference("profile.managed_default_content_settings.app_banner", 1, out SettingsPreference); //1
requestContext.SetPreference("profile.managed_default_content_settings.site_engagement", 1, out SettingsPreference); //1
requestContext.SetPreference("profile.managed_default_content_settings.payment_handler", 1, out SettingsPreference); //1
requestContext.SetPreference("profile.managed_default_content_settings.cookies", 1, out SettingsPreference); //1
requestContext.SetPreference("profile.content_settings.exceptions.geolocation", 1, out SettingsPreference); //1
//
// Settings Preference
requestContext.SetPreference("profile.content_settings.exceptions.cookies.http://*,*.setting", 1, out SettingsPreference); //https
requestContext.SetPreference("profile.content_settings.exceptions.cookies.http://*,*.last_used", 1, out SettingsPreference); //https
requestContext.SetPreference("profile.content_settings.exceptions.javascript.http://*,*.setting", 1, out SettingsPreference); //https
requestContext.SetPreference("profile.content_settings.exceptions.javascript.http://*,*.last_used", 1, out SettingsPreference); //https
requestContext.SetPreference("profile.content_settings.exceptions.protocol_handler.http://*,*.setting", 1, out SettingsPreference); //https
requestContext.SetPreference("profile.content_settings.exceptions.protocol_handler.http://*,*.last_used", 1, out SettingsPreference); //https
requestContext.SetPreference("profile.content_settings.exceptions.app_banner.http://*,*.setting", 1, out SettingsPreference); //https
requestContext.SetPreference("profile.content_settings.exceptions.app_banner.http://*,*.last_used", 1, out SettingsPreference); //https
requestContext.SetPreference("profile.content_settings.exceptions.site_engagement.http://*,*.setting", 1, out SettingsPreference); //https
requestContext.SetPreference("profile.content_settings.exceptions.site_engagement.http://*,*.last_used", 1, out SettingsPreference); //https
requestContext.SetPreference("profile.content_settings.exceptions.payment_handler.http://*,*.setting", 1, out SettingsPreference); //https
requestContext.SetPreference("profile.content_settings.exceptions.payment_handler.http://*,*.last_used", 1, out SettingsPreference); //https
// Background sync
requestContext.SetPreference("profile.managed_default_content_settings.background_sync", 1, out SettingsPreference); //1
// Settings Preference Background sync
requestContext.SetPreference("profile.content_settings.exceptions.background_sync.http://*,*.setting", 1, out SettingsPreference); //https
requestContext.SetPreference("profile.content_settings.exceptions.background_sync.http://*,*.last_used", 1, out SettingsPreference); //https
//
requestContext.SetPreference("profile.content_settings.exceptions.accessibility_events.http://*,*.setting", 1, out SettingsPreference); //https
requestContext.SetPreference("profile.content_settings.exceptions.accessibility_events.http://*,*.last_used", 1, out SettingsPreference); //https
// and this prevents chrome from starting
requestContext.SetPreference("profile.content_settings.exceptions.media_stream_mic.http://*,*.setting", 1, out SettingsPreference); //https
requestContext.SetPreference("profile.content_settings.exceptions.media_stream_mic.http://*,*.last_used", 1, out SettingsPreference); //https
requestContext.SetPreference("profile.content_settings.exceptions.media_stream_camera.http://*,*.setting", 1, out SettingsPreference); //https
requestContext.SetPreference("profile.content_settings.exceptions.media_stream_camera.http://*,*.last_used", 1, out SettingsPreference); //https
// Permission
requestContext.SetPreference("profile.content_settings.exceptions.important_site_info.http://*,*.setting", 1, out SettingsPreference); //https
requestContext.SetPreference("profile.content_settings.exceptions.important_site_info.http://*,*.last_used", 1, out SettingsPreference); //https
requestContext.SetPreference("profile.content_settings.exceptions.plugins.http://*,*.setting", 1, out SettingsPreference); //https
requestContext.SetPreference("profile.content_settings.exceptions.plugins.http://*,*.last_used", 1, out SettingsPreference); //https
requestContext.SetPreference("profile.content_settings.exceptions.geolocation.http://*,*.setting", 1, out SettingsPreference); //https
requestContext.SetPreference("profile.content_settings.exceptions.geolocation.http://*,*.last_used", 1, out SettingsPreference); //https
// and this prevents chrome from starting as well
requestContext.SetPreference("profile.content_settings.pattern_pairs.http://*,*.media_stream.video", "Allow", out SettingsPreference); //https
requestContext.SetPreference("profile.content_settings.pattern_pairs.http://*,*.media_stream.audio", "Allow", out SettingsPreference); //https
// change default camera using
requestContext.SetPreference("media.default_video_capture_Device", "\\\\?\\root#media#0002#{65e8773d-8f56-11d0-a3b9-00a0c9223196}\\global", out SettingsPreference);
} }
public CefReturnValue OnBeforeResourceLoad(IWebBrowser browserControl, IBrowser browser, IFrame frame, IRequest request, IRequestCallback callback)
{
var headers = request.Headers;
headers["Custom-Header"] = "My Custom Header";
request.Headers = headers;
return CefReturnValue.Continue;
}
Anyone got an answer for my question above re loading a page into the control when the WPF TabItem it is on it doesn't have focus?
@nzmike99 The default WPF TabControl
isn't really suitable for hosting a browser, see
When I add the ChromiumWebBrowserWithScreenshotSupport.cs file to the CefSharp.Wpf.Example project it needs the "using GalaSoft.MvvmLight.Command; " dependency which destroys the project with errors
@jeremiahjordani_twitter You could use any Delegate/RelayCommand implementation you like. Pretty common for WPF apps to have one.
If you don't have one then just change the access modifier for the paramaterless TakeScreenshot so it's usable in your instance. The command is entirely optional.
private void /*Btn*/Sceenshot_Click(object sender, RoutedEventArgs e)
{
/*
//CefSharp - Quick and Dirty Screenshot with no class dependency
MessageBox.Show("Take screenshot");
double dWidth = (webCefSharpChromiumWebBrowser.ActualWidth);
double dHeight = (webCefSharpChromiumWebBrowser.ActualHeight);
int actualWidth = 0;
int actualHeight = 0;
actualWidth = Convert.ToInt32(dWidth);
actualHeight = Convert.ToInt32(dHeight);
RenderTargetBitmap renderTargetBitmap = new RenderTargetBitmap(actualWidth, actualHeight, 90, 90, PixelFormats.Pbgra32);
renderTargetBitmap.Render(webCefSharpChromiumWebBrowser);
PngBitmapEncoder pngImage = new PngBitmapEncoder();
pngImage.Frames.Add(BitmapFrame.Create(renderTargetBitmap));
using (Stream fileStream = File.Create(@"C:\SaveFolderName\apollo.png")) //uses System.IO for fileStream
{
pngImage.Save(fileStream);
}
*/
}
[C#
] TakeScreenshot [WPF
]
/*Here is an example that reuses the image that's already rendered to the screen.*/
var template = Browser.Template;
var image = (System.Windows.Controls.Image)template.FindName("PART_image", Browser);
var pngImage = new PngBitmapEncoder();
pngImage.Frames.Add(BitmapFrame.Create((System.Windows.Media.Imaging.BitmapSource)image.Source));
var screenshotPath = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.Desktop), "CefSharp screenshot" + DateTime.Now.Ticks + ".png");
using (var fileStream = File.Create(screenshotPath))
{
pngImage.Save(fileStream);
}