How to ignore cache when loading html in chromiumwebbrowser? - cefsharp

I make chromiumwebbrowser in c# winform and load html in browser.
In my case, I import js in html but it doesn't work properly.
However, it works if I set disable cache in devtools and reload it.
So, I want to implement disable cache programmatically.
I tried this code but it failed.
public ChromiumWebBrowser chromiumBrowser;
settings = new CefSettings();
settings.CachePath = null;
settings.CefCommandLineArgs.Add ("disable-application-cache", "1");
settings.CefCommandLineArgs.Add("disable-session-storage", "1");
Cef.Initialize(settings);
Cef.GetGlobalCookieManager().DeleteCookies("", "");
chromiumBrowser.Load(url);
And I don't know if I can use the one in this url, and I don't know the specific usage.
https://cefsharp.github.io/api/63.0.0/html/M_CefSharp_WebBrowserExtensions_Reload_1.htm

Related

cefsharp - "Links that open a specific application" seem to be not working

I just begin with Cefsharp on C#.
Everything works fine except Cefsharp can not execute some special links that open/run a specific application on the computer.
The link still works on other Chromium official browsers (Google Chrome), I clicked the link and it launches the application. Cefshap is not, it does nothing when I clicked the link.
The link looks something like this: "runapp://api.abcxyz/..."
How can I make it work on Cefsharp?
image show that the link works on other chromium browsers
Firstly for security reasons loading of external protocols is disabled by default. Historically you would have implemented OnProtocolExecution.
There is currently an upstream bug in OnProtocolExecution see https://bitbucket.org/chromiumembedded/cef/issues/2715/onprotocolexecution-page-goes-blank-after
You can implement a workaround using RequestHandler.OnBeforeBrowser and calling Process.Start
It would look roughly something like the following (Written in Notepad++ very quickly, there maybe minor mistakes that you'd have to correct).
public class ExampleRequestHandler : RequestHandler
{
protected override bool OnBeforeBrowse(IWebBrowser chromiumWebBrowser, IBrowser browser, IFrame frame, IRequest request, bool userGesture, bool isRedirect)
{
if(request.Url.StartsWith("mailto:"))
{
System.Diagnostics.Process.Start(request.Url);
//Cancel navigation
return true;
}
return false;
}
}
browser.RequestHandler = new ExampleRequestHandler();

Using WebBrowser in ASP.Net Web Application

I am trying to use a WebBrowser in a .cs class in a Web Application - NOT A WINDOWS FORM app using (VS 2019).
I know it's a Windows Form app control, but it seems like I should be able to use it in a Web App.
Using WebBrowser in Web App does not have the .Url property, so I just use the .Navigate directly - this is where the code breaks.
I have tried many suggestions on the Net, but nothing seems to work - it looks like most of the examples uses a Web Page - I want to use the WebBrowser directly in code (.cs class).
private static void LoginViaUserAgentFlow()
{
Thread thread = new Thread(delegate ()
{
StringBuilder sb = new StringBuilder(String.Format("{0}/services/oauth2/authorize",
RHOutlook.chosenInstance.sfdcURL));
sb.Append("?response_type=token");
sb.Append(String.Format("&client_id={0}", RHOutlook.chosenInstance.clientId));
sb.Append(String.Format("&redirect_uri={0}", System.Web.HttpUtility.UrlEncode(RHOutlook.chosenInstance.redirectUri)));
WebBrowser webBrowserSFDCLogin = new WebBrowser();
//Here I can't use the .Url becaue it is not available - so I just use
//Navigate to get to the site
webBrowserSFDCLogin.Url = new Uri(sb.ToString());
//This is were it breaks, comes back with error when I navigate to the site.
webBrowserSFDCLogin.Navigate(sb.ToString());
//This code is never reached - failed at above code.
if (webBrowserSFDCLogin.LocationURL != null && webBrowserSFDCLogin.LocationURL.StartsWith(RHOutlook.chosenInstance.redirectUri))
{
//webBrowserSFDCLogin.ScriptErrorsSuppressed = true;
webBrowserSFDCLogin.Stop();
}
});
thread.SetApartmentState(ApartmentState.STA);
thread.Start();
thread.Join();
}
I expect to be able to navigate to the external website. If I can navigate to the site if I copy the url directly in a browser (say Chrome).
I tried using ; this gives me the Url property, but still get the error.
You really should stick to suggestions and examples.
What you are trying to do is to use class that is designed for Windows Application inside a Web Application.
Those two frameworks are similar like Earth and Mars. I am afraid you are not going to be able to grow a tree on Mars.

Launching a webpage in browser from an Adobe Air app not working

I've got a webpage I want to hook in to my Adobe Air app. It's running JavaScript, so I can't open it in the app, but I'm trying to launch it in the default system browser.
Here's the code that's supposed to be launching the page:
public function beginModule():void {
var loader:HTMLLoader = new HTMLLoader();
var gameURL:URLRequest = new URLRequest("file:///path/to/file.html");
loader.navigateInSystemBrowser = true;
loader.load(gameURL);
}
I've also tried using
flash.net.navigateToURL(gameURL);
but that had no effect.
Every time I hit the above method, that block executes but nothing happens other than a quick cursor change-- no browser opened, no change in the app. What am I missing here?
Before you start, check if the HTML page is properly running in the browser.
To open a HTML page in flex, use the HTML tag: <mx:HTML></mx:HTML>
Something like this: https://snipt.net/anishnair/flex-show-html-page/?key=28838d54ac287180032dee000ce33a74
Thank you.
Regards,
Anish
Simply use :
navigateToURL(url,"_blank");
to navigate to the url in the web-browser.

Invalidate browser cache

I have a question related to cache invalidation techniques... I am trying to implement a mechanism for my website, that automatically invalidates browser cache (css, images...). I want to be able to programatically invalidate browser cache, whenever I update the website (change images or styles);
For example: for the current release, among others, the css and some of the images have changed. In this situation I want that after the update is finished, when a user performs a request to the website, his browser's cache to get automatically invalidated, thus forcing the re-download of the new images and styles. This should be done only for the client's first request... the following ones should be retrieved from cache (so setting the no-cache pragma is out of the question).
Here's what i've tried:
in the BeginRequest event handler, I added the following lines:
Response.Cache.SetCacheability(HttpCacheability.ServerAndPrivate);
Response.Cache.SetETag("\"e111293b17594f1487d136ea7e9314ac\"");
this sets the ETag in the response headers. I figured that if I change this ETag at each release, and set it at each request, the cache will be invalidated, but it seems that it is not. I used Live HTTP headers to see the results and the ETAG is correctly set up for the response, but the css and images are still taken from cache.
Any ideas of how I could accomplish this, or if it can be accomplished at all?
I have run into issues like this in the past. Unfortunately I couldn't find a really nice way to accomplish this so I had to come up with a workaround. I was only dealing with this issue for CSS files so I added an extra querystring parameter to every CSS reference, for example
<link rel="stylesheet" type="text/css"
href="default.css?buildnumber=<%= Buildnumber %>" />
The build number gets incremented with each release so the browser was forced to go look for this new file. Not an ideal solution, but it worked without a hitch.
For those who are seeking for a MVC5 solution:
Step1: Change the AssemblyInfo.cs file of the project to the following
[assembly: AssemblyVersion("1.0.*")]
[assembly: AssemblyFileVersion("1.0.*")]
Step 2: Create a class to get the current version info on your project:
public class Versioner
{
public static Version GetVersion()
{
Assembly thisAssem = typeof(Versionador).Assembly;
AssemblyName thisAssemName = thisAssem.GetName();
Version vrs = thisAssemName.Version;
return vrs;
}
//Not really necessary, just if you need to show this info
public static string GetDataBuild()
{
Version version = System.Reflection.Assembly.GetExecutingAssembly().GetName().Version;
DateTime buildDate = new DateTime(2000, 1, 1).AddDays(version.Build).AddSeconds(version.Revision * 2);
string displayableVersion = $"{version} ({buildDate})";
return displayableVersion;
}
}
Step 3: Call the class methods in the views which needs cache auto-refresh on new builds.
#{
Version _ver = <MyProject>.Classes.Extensions.Versioner.GetVersion();
}
//here, <MyProject>.Classes.Extensions is my path to the Versioner.cls class file, adjust it to your project's classes path
Step 4: Use the variable with the version string to append to your scripts or .css files
<script src="~/js/index.js?v=#_ver"></script>
<link href="/css/style.css?v=#_ver" rel="stylesheet" />

Accessing browser cookies from Flex

I'm building a Flex widget for a private vBulletin site, and the Flex widget needs to access an XML file on the vBulletin server in order to display data.
For security reasons, the XML URL will need to have the value in the bbsessionhash cookie passed along in the URL request from Flex. The Flex widget will be embedded in the private area that the user has logged into, so the Flex request will be coming from the same website the cookie is from.
Is there any way to access the cookies directly within Flex? I would prefer not to use ExternalInterface to grab the cookie data from JavaScript, as it could get a little messy (the templates are developed by a completely different dev team).
I have never tried this, but this library might just do the trick.
As per the flash or flex cookies are concern developer can use shared object which is one kind of cookie used for flex application.
The sample code snippet is as followes
import flash.net.SharedObject;
// get/create the shared object with a unique name.
// If the shared object exists this grab it, if not
// then it will create a new one
var so: SharedObject = SharedObject.getLocal("UniqueName");
// the shared object has a propery named data, it's
// an object on which you can create, read, or modify
// properties (you can't set the data property itself!)
// you can check to see if it already has something set
// using hasOwnProperty, so we'll check if it has a var
// use it if it does, or set it to a default if it doesn't
if (so.data.hasOwnProperty("theProp"))
{
trace("already has data! It reads: " + so.data.theProp);
}
else
{
so.data.theProp = "default value";
so.flush(); // flush saves the data
trace("It didn't have a value, so we set it.");
}
Accessing Flex SharedObject is NOT the same as accessing the Browser cookies, to access the browser cookies, you may use the ExternalInterface class, please check the following reference to see samples:
http://livedocs.adobe.com/flex/3/html/help.html?content=passingarguments_4.html
A reference of how to use and control cookies using JavaScript can be found here:
http://www.quirksmode.org/js/cookies.html
I would use the following Flex code:
var myCookie:String = ExternalInterface.call("getCookie('cookieName')");
And in the HTML I would add the following Javascript:
function getCookie(c_name) {
var i,x,y,ARRcookies=document.cookie.split(";");
for (i=0;i<ARRcookies.length;i++) {
x=ARRcookies[i].substr(0,ARRcookies[i].indexOf("="));
y=ARRcookies[i].substr(ARRcookies[i].indexOf("=")+1);
x=x.replace(/^\s+|\s+$/g,"");
if (x==c_name) return unescape(y);
}
}
If you require more help you could also check the Flex documentation.

Resources