开发者

Writing Internet Explorer Plugin: Problem modifying DOM with Browser Helper Object

currently I am writing an Plugin for the Internet Explorer in C#. It is my first try with .net and the Internet Explorer Plugin. C# has nice language features in comparison to Java.

However, I am stuck. I can't find an easy/or a way in gen开发者_StackOverflow社区eral to modify the DOM.

My Plugin should have a function to display a Html header to a site.

In Javascript I would do something like this:

var a = document.createElement('a');
var text_node = document.createTextNode(text);
var href = document.createAttribute("href");
href.nodeValue = url;
a.setAttributeNode(href);
a.appendChild(text_node); 
var my_dom = document.createElement('div');
my_dom.appendChild(a);
my_dom.style.background = '#36b';;
document.body.insertBefore(my_dom, document.body.firstChild);

I used the tutorial at www.codeproject.com/KB/cs/Attach_BHO_with_C_.aspx to get familiar with BHO and Internet Explorer Development. However in this plugin the package mshtml is used to access the dom. I can't find a good way through the api to add new elements to the dom. While searching through the net I discovered that the System.Windows.Forms.HtmlDocument has a appendChild Function. However when I transform my program to System.Windows.Forms it does not work at all.

Can somebody show me a way how I can modify (insert at the beginning of the body an html element) the dom?

Here is a link to the skeleton of my programm https://gist.github.com/fd4459dc65acd7d167b6 For the beginning it is enough to show me a way how i can add an to the beginning of the body in the OnDocumentComplete function.

Thank you


After searching and searching I found a solution. I did not find a way to modify the DOM via mshtml, but via javascript. Javascript can be injected via

document.parentWindow.execScript("alert('hello world')");

I could reuse my existing javascripts to solve this issue.


if you have more than one line of Javascript code, You can have multiple lines of execScript. Example:

document.parentWindow.execScript("var trends_dom = document.createElement('div')");
document.parentWindow.execScript("var title_dom = document.createElement('strong')");
document.parentWindow.execScript("var text_dom = document.createTextNode('test')");
document.parentWindow.execScript("title_dom.innerText = 'This text is placed over a web page'");
document.parentWindow.execScript("trends_dom.appendChild(title_dom)");
document.parentWindow.execScript("trends_dom.appendChild(text_dom)");
document.parentWindow.execScript("trends_dom.style.background = '#36b'");
document.parentWindow.execScript("trends_dom.style.color = '#fff'");
document.parentWindow.execScript("trends_dom.style.padding = '10px'");
document.parentWindow.execScript("trends_dom.style.position = 'fixed'");
document.parentWindow.execScript("trends_dom.style.zIndex = '123456'");
document.parentWindow.execScript("trends_dom.style.top = '20px'");
document.parentWindow.execScript("trends_dom.style.font = '14px Arial'");
//document.body.appendChild(trends_dom);
document.parentWindow.execScript("document.body.insertBefore(trends_dom, document.body.firstChild)");
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜