1

現在、C# で Internet Explorer 用のプラグインを作成しています。これは、.net と Internet Explorer プラグインを使用した初めての試みです。C# には、Java に比べて優れた言語機能があります。

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

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

Javascript では、次のようにします。

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

私は www.codeproject.com/KB/cs/Attach_BHO_with_C_.aspx のチュートリアルを使用して、BHO と Internet Explorer の開発に慣れました。ただし、このプラグインでは、パッケージ mshtml を使用して dom にアクセスします。API を使用して新しい要素を dom に追加する良い方法が見つかりません。ネットを検索しているときに、System.Windows.Forms.HtmlDocument に appendChild 関数があることを発見しました。ただし、プログラムを System.Windows.Forms に変換すると、まったく機能しません。

dom を変更する方法 (本文の先頭に html 要素を挿入する方法) を教えてもらえますか?

ここに私のプログラムのスケルトンへのリンクがありますhttps://gist.github.com/fd4459dc65acd7d167b6 最初は、 OnDocumentComplete 関数の本文の先頭に を追加する方法を示すだけで十分です。

ありがとうございました

4

2 に答える 2

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)");
于 2011-05-02T17:04:11.757 に答える
1

検索して検索した後、解決策を見つけました。mshtml 経由ではなく、javascript 経由で DOM を変更する方法が見つかりませんでした。Javascriptは経由で注入できます

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

この問題を解決するために、既存の JavaScript を再利用できます。

于 2010-12-01T19:39:32.423 に答える