Injecting CSS in WebBrowser Control
What I did to get it working
CurrentDocument = (mshtml.HTMLDocument)webBrowser1.Document.DomDocument;
styleSheet = CurrentDocument.createStyleSheet("", 0);
StreamReader streamReader = new StreamReader(@"test.css"); //test.css is Stylesheet file
string text = streamReader.ReadToEnd();
streamReader.Close();
styleSheet.cssText = text;
How to inject CSS in WebBrowser Control
What are you trying to reference with IHTMLStyleElement
and IHTMLStyleSheetElement
? Its not anywhere in the .NET framework.
Just modify your code to write the contents of the generic HtmlElement
type instead by writing the contents of the .InnerText
or .InnerHtml
with your stylesheet
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
webBrowser1.Navigate("https://www.aparat.com/aleffamily/live2");
webBrowser1.DocumentCompleted += webBrowser1_DocumentCompleted;
}
private void webBrowser1_DocumentCompleted(object sender, WebBrowserDocumentCompletedEventArgs e)
{
HtmlElement head = webBrowser1.Document.GetElementsByTagName("head")[0];
HtmlElement styleEl = webBrowser1.Document.CreateElement("style");
styleEl.InnerHtml = @"body {background-color:transparent !important; margin: 0px auto; overflow: hidden; }.live__content{display:none;}.chat__footer{display:none;}.chat__header{display:none}#header{display:none}.in-chat-avatar{display:none}.message__username{font-style:italic;font-weight: 800!important;color:ff8f0f !important}.message__text{font-style:italic;font-weight: 400!important;color:ff8f0f !important}.chat__content{ background-color:transparent !important}.chat{ background-color:transparent !important}";
head.AppendChild(styleEl);
}
}
WebBrowser component not showing CSS 3
Just for further reference to other people needing this:
First of all:
Thanks to Boo & Lex Li
For helping me find the answer to my question.
You have to set a certain registry to the right value:
There are two different sets of keys for 32 bit and 64 bit applications.
32 bit:
HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Internet Explorer\MAIN\FeatureControl\FEATURE_BROWSER_EMULATION
Value Key: yourapplication.exe
64 bit:
HKEY_LOCAL_MACHINE\SOFTWARE\Wow6432Node\Microsoft\Internet Explorer\MAIN\FeatureControl\FEATURE_BROWSER_EMULATION
Value Key: yourapplication.exe
The value to set this key to is (taken from MSDN here) as decimal values:
9999 (0x270F)
Internet Explorer 9. Webpages are displayed in IE9 Standards mode, regardless of the !DOCTYPE directive.
9000 (0x2328)
Internet Explorer 9. Webpages containing standards-based !DOCTYPE directives are displayed in IE9 mode.
8888 (0x22B8)
Webpages are displayed in IE8 Standards mode, regardless of the !DOCTYPE directive.
8000 (0x1F40)
Webpages containing standards-based !DOCTYPE directives are displayed in IE8 mode.
7000 (0x1B58)
Webpages containing standards-based !DOCTYPE directives are displayed in IE7 Standards mode.
Even tough MSDn claims that 9000 is the automatically assigned value. Apperently this is simply not true.
Below you can find the code how to add these keys to your registry. Please not that your application has a different processname when you debug.
RegistryKey key = Registry.LocalMachine.OpenSubKey(@"SOFTWARE\Microsoft\Internet Explorer\MAIN\FeatureControl\FEATURE_BROWSER_EMULATION", true);
if (key != null)
{
key.SetValue("YourApplicationName.exe", 9000, RegistryValueKind.DWord);
}
key = Registry.LocalMachine.OpenSubKey(@"SOFTWARE\Wow6432Node\Microsoft\Internet Explorer\MAIN\FeatureControl\FEATURE_BROWSER_EMULATION", true);
if (key != null)
{
key.SetValue("YourApplicationName.exe", 9000, RegistryValueKind.DWord);
}
So thanks all and Good Luck
Edit: User Account Control should be off to make this code work.
wpf webbrowser control not applying css
In your constructor, you can write these lines of code in order to resolve issues with your CSS:
var fileName = System.IO.Path.GetFileName(Process.GetCurrentProcess().MainModule.FileName);
SetBrowserFeatureControlKey("FEATURE_BROWSER_EMULATION", fileName, GetBrowserEmulationMode());
Alternatively, you can read this blog post. And this forum post, that contains some hacks for your trouble.
Related Topics
How to Save Dlls in a Different Folder When Compiling in Visual Studio
C# List - Removing Items While Looping/Iterating
How to Implement the Equivalent of SQL In() Using .Net
Why Is ASP.NET Identity Identitydbcontext a Black-Box
Writing File to Web Server - ASP.NET
Assemblytitle' Attribute in the .Net Framework
System.Data.Sqlclient.Sqlexception: Invalid Column Name 'Phone_Types_Phone_Type_Id'
How to Set PDF Paragraph or Font Line-Height with Itextsharp
JSON Serialize List<Keyvaluepair<String, Object>>
Creating Simple C++.Net Wrapper. Step-By-Step
Entity Framework - Capitalizing First Property Name Letter
Why Can't I Retrieve an Item from a Hashset Without Enumeration
Entity Framework 4.1 Inverseproperty Attribute and Foreignkey
Entity Framework Initialization Is Slow -- What How to Do to Bootstrap It Faster
How to Open the Physical File Operating System Error 32
How to Add a Custom View Dynamically to a View in Visual Studio for MAC C#