2010-11-30 12 views
1

現在、私はInternet Explorer用のプラグインをC#で作成しています。 これは、.NETとInternet Explorerプラグインで初めての試みです。 C#には、Javaに比べて優れた言語機能があります。Internet Explorerプラグインの作成:ブラウザヘルパーオブジェクトでDOMを変更する際の問題

しかし、私は立ち往生しています。 DOMを変更するための一般的な方法は簡単ではありません。

マイプラグインには、サイトにHTMLヘッダーを表示する機能が必要です。 JavaScriptで

私はこのようなものだろう:私はBHOおよびInternet Explorerの開発に慣れるためにwww.codeproject.com/KB/cs/Attach_BHO_with_C_.aspxでチュートリアルを使用

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); 

を。しかし、このプラグインではパッケージmshtmlを使用してdomにアクセスします。私はDOMに新しい要素を追加するためのAPIを介して良い方法を見つけることができません。 ネットを検索しているうちに、System.Windows.Forms.HtmlDocumentにappendChild関数があることがわかりました。しかし、プログラムをSystem.Windows.Formsに変換すると、全く動作しません。

誰かが私がどのように身体の冒頭にhtml要素を挿入するようにDOMを修正することができますか?

私のプログラムのスケルトンへのリンクhttps://gist.github.com/fd4459dc65acd7d167b6 最初は、OnDocumentComplete関数でボディの先頭にどのように追加できるかを示すだけで十分です。

ありがとうございました

答えて

1

検索と検索の結果、解決策が見つかりました。 mshtml経由でDOMを変更する方法は見つけられませんでしたが、javascriptを使用しました。 Javascriptが、私はこの問題を解決するために、私の既存のJavaScriptを再利用することができ

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

を介して注入することができます。

1

複数行のJavascriptコードがある場合は、複数行のexecScriptを使用できます。例:

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)"); 
関連する問題