简体   繁体   中英

Dynamically Change Css Styles in C# (Sharp) Without Asp

Hi all hopefully this is a quick one.
I'm working on ac# web browser that, amongst other things, changes css styles of web pages like google and facebook. for example it will make the background on facebook and google red instead of white. i've had success but its not at all consistent and i have no idea why.

HtmlDocument doc = Browser1.Document;
HtmlElement textElem = doc.CreateElement("DIV");
textElem.InnerHtml = "<STYLE>body{background-color:red!important}</STYLE>";
doc.Body.AppendChild(textElem);

that code works on www.google.com but not www.rationality.tk however..

HtmlElement head = Browser1.Document.GetElementsByTagName("body")[0];
head.SetAttribute("bgcolor", "red");

That code works on www.rationality.tk but not www.google.com and both codes do not work on www.facebook.com which i cannot get anything to work on.

I'm probably doing something wrong and I just moved on to c# after giving up on c++ and I have found it a lot easier but still getting the hang of it. thank you in advance.

EDIT: PROBLEM SOLVED

HtmlElement head = Browser1.Document.GetElementsByTagName("body")[0];
head.Style = "background-color:red";

this works on facebook, google and rationality.tk

I am not very sure but the problem might be due to embedded iframes and igoogle like widgets. I have faced the same problem while implementing a similar functionality in COM/ATL for my Browser Helper Object. The approach that helped me (to some extent) was to wait for DOCUMENT_COMPLETE event for each iframe and then try to get their body . Again I am not sure how you can achieve the same in C#. This is just some food for your thoughts.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM