ant84
asked on
MSHTML edit the <head> section of a document
I have an mshtml.IHTMLDocument which I would like to add a new stylesheet to.
The only way to add a stylesheet is in the <head> section, which it seems I cannot edit in anyway.
Does anyone have any recommendations on how it can be done?
The only way to add a stylesheet is in the <head> section, which it seems I cannot edit in anyway.
Does anyone have any recommendations on how it can be done?
it can be edit.
ASKER
Thanks i'll give it a go and let you know how i get on.
ASKER
That looks like it'll do the trick, i am running into a couple of problems though with finding a method to edit the inner html of the <style> node. Is there such a way you can think of?
ASKER
Here is my code:
mshtml.HTMLDocument htmlDocument;
htmlDocument = (mshtml.HTMLDocument)this. wbr2.Docum ent;
foreach (mshtml.IHTMLDOMNode node in (htmlDocument.childNodes as mshtml.IHTMLDOMChildrenCol lection))
{
ProcessNode(node);
}
Then:
private void ProcessNode(mshtml.IHTMLDO MNode node)
{
if (node is mshtml.HTMLHeadElement)
{
foreach (mshtml.IHTMLDOMNode cNode in (node.childNodes as mshtml.IHTMLDOMChildrenCol lection))
{
//there doesn't seem to be a cNode.innerHTML property
}
//it doesnt let me instatiate a new mshtml.IHTMLDOMNode like this!
mshtml.IHTMLDOMNode newNode = new mshtml.IHTMLDOMNode();
((mshtml.HTMLLinkElement)n ewNode).se tAttribute ("rel", "stylesheet", 0);
((mshtml.HTMLLinkElement)n ewNode).se tAttribute ("href", "stylesheets/paviors.css", 0);
node.appendChild((mshtml.I HTMLDOMNod e)newNode) ;
}
if ((node.childNodes as mshtml.IHTMLDOMChildrenCol lection).l ength == 0)
{
return;
}
foreach (mshtml.IHTMLDOMNode childNode in (node.childNodes as mshtml.IHTMLDOMChildrenCol lection))
{
ProcessNode(childNode);
}
}
I raised the points on this question, I've been experimenting for much of the day and still can't get it working
mshtml.HTMLDocument htmlDocument;
htmlDocument = (mshtml.HTMLDocument)this.
foreach (mshtml.IHTMLDOMNode node in (htmlDocument.childNodes as mshtml.IHTMLDOMChildrenCol
{
ProcessNode(node);
}
Then:
private void ProcessNode(mshtml.IHTMLDO
{
if (node is mshtml.HTMLHeadElement)
{
foreach (mshtml.IHTMLDOMNode cNode in (node.childNodes as mshtml.IHTMLDOMChildrenCol
{
//there doesn't seem to be a cNode.innerHTML property
}
//it doesnt let me instatiate a new mshtml.IHTMLDOMNode like this!
mshtml.IHTMLDOMNode newNode = new mshtml.IHTMLDOMNode();
((mshtml.HTMLLinkElement)n
((mshtml.HTMLLinkElement)n
node.appendChild((mshtml.I
}
if ((node.childNodes as mshtml.IHTMLDOMChildrenCol
{
return;
}
foreach (mshtml.IHTMLDOMNode childNode in (node.childNodes as mshtml.IHTMLDOMChildrenCol
{
ProcessNode(childNode);
}
}
I raised the points on this question, I've been experimenting for much of the day and still can't get it working
maybe this works for you:
private void axWebBrowser1_NavigateComp lete2(obje ct sender, AxSHDocVw.DWebBrowserEvent s2_Navigat eComplete2 Event e)
{
mshtml.HTMLDocument htmlDocument = (mshtml.HTMLDocument)this. axWebBrows er1.Docume nt;
foreach (mshtml.IHTMLStyleSheet styleSheet in (htmlDocument.styleSheets as mshtml.HTMLStyleSheetsColl ection))
{
//check the styleSheet ref here
}
}
in this case i think, the ProcessNode method will not fit to your needs.
another problem could be, that the webpage is loaded and remains in memory, from were it will be rendered in the browser.
so it could be, that you will not see the changes of your stylesheet.
therefore i would recommend, to temporary save the modified document and load it again in the browser.
but therefore, you will need a flag to check, in which state you are.
maybesomething like this:
private bool loadTempDocument = false;
if you have modified your document with your stylesheet, save it to your harddisk, set the flag to true, load the saved document again and set the flag back to false.
private void axWebBrowser1_NavigateComp
{
mshtml.HTMLDocument htmlDocument = (mshtml.HTMLDocument)this.
foreach (mshtml.IHTMLStyleSheet styleSheet in (htmlDocument.styleSheets as mshtml.HTMLStyleSheetsColl
{
//check the styleSheet ref here
}
}
in this case i think, the ProcessNode method will not fit to your needs.
another problem could be, that the webpage is loaded and remains in memory, from were it will be rendered in the browser.
so it could be, that you will not see the changes of your stylesheet.
therefore i would recommend, to temporary save the modified document and load it again in the browser.
but therefore, you will need a flag to check, in which state you are.
maybesomething like this:
private bool loadTempDocument = false;
if you have modified your document with your stylesheet, save it to your harddisk, set the flag to true, load the saved document again and set the flag back to false.
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
Many thanks for your help, vo1d. That was just the trick and I can now reliably edit the <head> section.
https://www.experts-exchange.com/questions/21823323/Using-MSHTML-to-modify-HTML-Elements.html