CEFSharp - Read web responses - c#

I am working on a project that I want to use the Chromium Web Browser and be able to read the data that would normally come though the DevTools "Network" tab. So basically all i really need is the URL and the status code (404, 200, 50x, etc).
I have everything working properly for the ChromiumWebBrowser part because that works perfectly but I cant seem to figure out the details on how to get the network data.
I found this in a github issues section but I dont really understand how to implement it. https://github.com/cefsharp/CefSharp/issues/1379
Any help would be greatly appreciated.
Here is what I have so far...
private ChromiumWebBrowser _wb;
public MainForm()
{
var cefsettings = new CefSettings { CachePath = "cache" };
cefsettings.CachePath = "cache";
if (cefsettings.CefCommandLineArgs.ContainsKey("enable-system-flash"))
{
string flashValue;
cefsettings.CefCommandLineArgs.TryGetValue("enable-system-flash", out flashValue);
if (flashValue != "1")
{
Debug.WriteLine("Flash Might Be Disabled For Chromium Web Browser");
}
}
else
{
cefsettings.CefCommandLineArgs.Add("enable-system-flash", "1");
}
//TODO: Get the latest version version folder
cefsettings.CefCommandLineArgs.Add("ppapi-flash-path","C:\\program Files (x86)\\Google\\Chrome\\Application\\51.0.2704.103\\PepperFlash\\pepflashplayer.dll");
Cef.Initialize(cefsettings);
InitializeComponent();
_wb = new ChromiumWebBrowser("http://youtube.com/")
{
Dock = DockStyle.Fill,
Location = new System.Drawing.Point(0, 22),
MinimumSize = new System.Drawing.Size(20, 20),
Size = new System.Drawing.Size(1280, 900),
TabIndex = 8
};
//Add ChromiumWebBrowser to the Browser Panel
pnlBrowser.Controls.Add(_wb);
}

Here is what I ended up doing...
Implemented a class called "RequestHandler" that implements the IRequestHandler interface. Copied most of the default code for this interfaces methods from the CEFSharp open source project and then just tweaked the "IRequestHandler.OnResourceResponse" portion to my liking.
Then on my main form that uses the web browser, I just used the code below...
//Create ChromiumWebBrowser
_wb = new ChromiumWebBrowser(Urls.HOME)
{
Dock = DockStyle.Fill,
Location = new System.Drawing.Point(0, 22),
MinimumSize = new System.Drawing.Size(20, 20),
Size = new System.Drawing.Size(1280, 900),
TabIndex = 8
};
//Add ChromiumWebBrowser to the Browser Panel and add events
pnlBrowser.Controls.Add(_wb);
var requestHandler = new RequestHandler();
_wb.RequestHandler = requestHandler;
I hope this helps someone!

Related

UserControl is blank when added to panel (sometimes)

I have an App written in .Net Framework 4.7.2 that sporadically, when a new user control is needed to be added to a flow panel, it's added successfully (and bumped to the top of the panel), but the user control is blank. Here's how I create it:
int shortcutNumber = NextShortcutNumber();
ctrlIssue newIssueCtrl = new ctrlIssue(issueData, shortcutNumber);
newIssueCtrl.IssueDeleted += new System.EventHandler(this.ctrlIssue_IssueDeletedHandler);
newIssueCtrl.Visible = false;
panIssues.Controls.Add(newIssueCtrl);
panIssues.Controls.SetChildIndex(newIssueCtrl, 0); // Always put it at the top
newIssueCtrl.Visible = true;
newIssueCtrl.Target();
I've diagnosed it down to that Control.Created is set to false. So ControlCreated is not called? I'm trying a workaround now like so:
if (!newIssueCtrl.Created)
{
newIssueCtrl.CreateControl();
}
We'll see if it works, but it's weird. Does anyone have any insight on why ControlCreated wouldn't be called? It seems sporadic and inconsistent, when it starts happening, it happens about 80% of the time.
Thanks!
EDIT: There was an ask to see the ctrlIssue constructor. There's fancy in it:
public ctrlIssue(Issue data, int shortcut)
{
InitializeComponent();
mIssueData = data;
chkPlayPause.TabIndex = 2;
lblProject.Text = mIssueData.Fields.Project.Name;
chkPlayPause.Text = mIssueData.Key;
lblSummary.Text = mIssueData.Fields.Summary;
InitializeToolTips();
UpdateShortcut(shortcut);
mSummaryFontRegular = lblSummary.Font;
mSummaryFontHover = new Font(mSummaryFontRegular, FontStyle.Underline);
mProjectFontRegular = lblProject.Font;
mProjectFontHover = new Font(mProjectFontRegular, FontStyle.Underline);
}

Adding and Opening an DialogPage (or window) for MacOS (C#)

I'm very new at programming in C# and Xamarin, and now I'm stuck.
I made a menu with some help, now I can do Cut/Copy/Paste which works fine, but now I want to make an Dialog or Window for some User Settings whenever they click on Settings in the menu. (I've made the menu in AppDelegate.cs)
I've look around a lot on Google and Xamarin forums, but al they talk about is to add a new kind of storyboard thingy. Which I guess will work, but I'm not using any storyboard (Just because we want to make everything out of code).
Found on Xamarin:
https://developer.xamarin.com/guides/mac/user-interface/working-with-dialogs/
And then the Preference window (That is what I prefer).
Code I made in AppDelegate.cs (Menu button which has to open an Window or Dialog):
var prefMenuItem = new NSMenuItem("Settings", ",", handler: delegate
{
var prefStyle = NSWindowStyle.Closable | NSWindowStyle.Resizable | NSWindowStyle.Titled;
var rect = new CoreGraphics.CGRect(200, 1000, 1024, 768);
prefWindow = new NSWindow(rect, prefStyle, NSBackingStore.Buffered, false);
prefWindow.Title = "Settings";
prefWindow.TitleVisibility = NSWindowTitleVisibility.Hidden;
});
'Settings' Button in the menu is visible btw.
Probably I forgot something, it won't open a thing.
How can I make this work?
Maybe this will work for you.
var prefMenuItem = new NSMenuItem("Settings", ",", handler: delegate
{
var prefStyle = NSWindowStyle.Closable | NSWindowStyle.Resizable | NSWindowStyle.Titled;
var rect = new CoreGraphics.CGRect(200, 1000, 1024, 768);
prefWindow = new NSWindow(rect, prefStyle, NSBackingStore.Buffered, false);
prefWindow.Title = "Settings";
prefWindow.TitleVisibility = NSWindowTitleVisibility.Hidden;
NSApplication.SharedApplication.RunModalForWindow(prefWindow);
});
I added NSApplication.SharedApplication.RunModalForWindow(prefWindow); in your code. This will open a new window.
Found this on: https://forums.xamarin.com/discussion/22729/xamarin-mac-how-to-set-position-of-modal-window

Making a tabbed cef sharp browser rename tab page

i have been creating a cefsharp browser in C#. i have made it so you can have multiple tabs and it loads the pages correctly. however, i cannot seem to find how i can rename the tab to the name of the page.
this is the code in the load event for form1.cs:
Cef.Initialize();
toolTip1.SetToolTip(button1, "Settings");
TabPage tab = new TabPage();
Tab newtab = new Tab();
newtab.Show();
newtab.TopLevel = false;
newtab.Dock = DockStyle.Fill;
tab.Controls.Add(newtab);
tabControl1.TabPages.Add(tab);
i have tried:
private void myBrowser_isLoading(object sender)
{
myBrowser.Parent.Parent.Text = myBrowser.Title;
}
but that doesn't work.
then this is the code for tab.cs:
public Tab()
{
InitializeComponent();
// Start the browser after initialize global component
InitializeChromium();
}
public CefSharp.WinForms.ChromiumWebBrowser myBrowser;
public bool nav = new bool();
public void InitializeChromium()
{
myBrowser = new CefSharp.WinForms.ChromiumWebBrowser("http://www.google.com");
this.Controls.Add(myBrowser);
myBrowser.Dock = DockStyle.Fill;
myBrowser.Parent = panel2;
if (nav == true)
{
myBrowser.Load(textBox1.Text);
nav = false;
}
}
private void Form1_FormClosing(object sender, FormClosingEventArgs e)
{
Cef.Shutdown();
}
again, i am using c# with the latest build of cef sharp(or atleast the one installed from the nuget package manager).
In your tab function in form1.cs, you need to add a title changed function like this
browser.TitleChanged += OnBrowserTitleChanged;
you also need to specify what browser is and set dockstyle to fill like this
ChromiumWebBrowser browser = new ChromiumWebBrowser("google.com");
tab.Controls.Add(browser);
browser.Dock = DockStyle.Fill;
now for the OnBrowserTitleChanged, you will need an EventArg which will tell the tab to have the document title in this format
this.InvokeOnUiThreadIfRequired(() => browserTabControl.SelectedTab.Text = args.Title);
this will add the document title to the tabcontrol browserTabControl is the name of the tabcontrol you will have to change browserTabControl to whatever name you have for the tabcontrol. Also the code you have does not belong with cef initialize. you need to create an addNewTab method with all the functions that will be processed when you want to add a new tab. Also, you cannot use a panel if you want to have tabs. you need a tabcontrol

Can't Get WP 8.1 AdControl to Engage on User Click

I have a universal app project for Windows/WP 8.1, and I am attempting to get the Ads to work on both platforms. In the OnApplyTemplate function of my control, I have an #ifdef to dynamically create the AdControl for either Windows or WP. The code works great on Windows, and on WP the ad shows up just fine; but when I click on it, nothing is happening. I have tried replacing the code with a dynamic Button to make sure something else isn't intercepting the clicks, and the Button works just fine. Any ideas?
protected override void OnApplyTemplate()
{
base.OnApplyTemplate();
var baseGrid = (Grid)GetTemplateChild("_BaseGrid");
var bannerGrid = (Grid)GetTemplateChild("_BannerGrid");
#if WINDOWS_APP
bannerGrid.Height = 450;
// Create the ad
var ad = new Microsoft.Advertising.WinRT.UI.AdControl()
{
ApplicationId = "0b7b7910-f56d-477c-b0e9-3def577b6359",
AdUnitId = "10056707",
Height = 90,
Width = 728,
Visibility = AdVisibility,
VerticalAlignment = VerticalAlignment.Top,
HorizontalAlignment = HorizontalAlignment.Center
};
baseGrid.Children.Add(ad);
#else
// Set the margins appropriately for WP
bannerGrid.Margin = new Thickness(0, 55, 0, 50);
/* This works just fine!
var button = new Button()
{
Content = "Try This!",
Height = 50,
Width = 320,
VerticalAlignment = VerticalAlignment.Top,
HorizontalAlignment = HorizontalAlignment.Center
};
button.Click += (object sender, RoutedEventArgs args) =>
{
Nova.Utilities.Logger.Info("User tapped on button.");
};
baseGrid.Children.Add(button);*/
// Create the ads
var ad = new Microsoft.Advertising.Mobile.UI.AdControl("f854ae91-1c0e-469c-8387-6c90ef8fa6a2", "11388154", true)
{
Height = 50,
Width = 320,
Visibility = AdVisibility,
IsEnabled = true,
VerticalAlignment = VerticalAlignment.Top,
HorizontalAlignment = HorizontalAlignment.Center
};
ad.IsEngagedChanged += (object sender, RoutedEventArgs args) =>
{
Nova.Utilities.Logger.Info("User tapped on Ad.");
};
baseGrid.Children.Add(ad);
#endif
}
So it seems like this may possibly still be a bug in the system:
This is a bug in our system. We are still actively working on a fix
and work out a release plan. But here is a workaround I found. BTW,
this workaround hasn't been fully tested in our team.
https://social.msdn.microsoft.com/Forums/en-US/053fe091-8ffb-4d6f-a5ca-f39cff4af618/ads-not-responding-to-tap-or-click?forum=aiaforwinphone
Per the comments, some digging showed it might be related not setting the current Context to a Frame correctly:
MonoGame people have run into this problem too with the new Microsoft
Advertising SDK for Windows 8.1. They had to make changes to make the
ads work again. Maybe the prime31 June plugin needs a similar update?
I am asking in the prime31 support forums too.
https://monogame.codeplex.com/discussions/467336
Also I have found other peoples released Unity Windows Store 8.1 games
that suffer from the freezing problem after the ad is displayed
sometimes. I guess they didn't notice it. Example: (the first ad
popping up froze the game graphics for me):
http://apps.microsoft.com/windows/e...mulator/04eea1ba-8792-437f-a7c2-faec620da6bf#
Following the links seem to show some success with setting it to a Grid:
Add Width and Height and Background to and also place it
where you want your Ad to appear. If you do not set Width and Height
and give your Grid a background color if floods all the screen.
https://monogame.codeplex.com/discussions/467336
Best answer I can find on this issue, no one seems to have been able to resolve it any other way.

How to open a new window in browser with focus

I would like to know how to open new window in the browser with focus while keeping the other window open too.
I have a button in my silverlight app that must open a pdf in another window or tab, i want the new window to have focus while keeping the initial page open.
I can get the new window to open but without focus or i can get it to replace the current page , then returning to the app requires login etc.
So i need 3 things...
1)open new window
2)focus new window
3)leave old window open
Uri myUri = new Uri(#"http://......./ViewPDF.aspx");
HtmlPage.Window.Navigate(myUri, "_newWindow");
HtmlPage.Window.Navigate(myUri);
HtmlPage.Window.Eval("window.focs()")
HtmlPage.PopupWindow(myUri, "_blank", null);
None of these meet all 3 requirements.
please help..
Thanking you in advance
HtmlPopupWindowOptions options = new HtmlPopupWindowOptions();
options.Left = 300;
options.Top = 150;
options.Width = 1024;
options.Height = 768;
options.Directories = false;
options.Location = false;
options.Menubar = false;
options.Status = false;
options.Toolbar = false;
Uri myUri = new Uri(#"http://......./ViewPDF.aspx");
HtmlPage.PopupWindow(Uri, "_blank", options);

Categories

Resources