您Qustions:
答案:
- 是的,它是可能的,而不改變鏈接!
- 不,它不會變慢!
- 是的,這是可能的,沒有必要先導航到約:空白
我們先從代碼和第一個過程(只顯示其中約:......)的由來。
{$R *.DFM}
var
Doc: IHTMLDocument2;
TempFile: string;
xBody : IHTMLElement;
xLoaded : Boolean;
onlyOnce: Boolean;
procedure TForm1.WB_LoadHTML(HTMLCode: string);
var
sl: TStringList;
ms: TMemoryStream;
begin
xLoaded := False;
WebBrowser.Navigate('about:blank');
while WebBrowser.ReadyState < READYSTATE_INTERACTIVE do
Application.ProcessMessages;
if Assigned(WebBrowser.Document) then
begin
sl := TStringList.Create;
try
ms := TMemoryStream.Create;
try
sl.Text := HTMLCode;
sl.SaveToStream(ms);
ms.Seek(0, 0);
(WebBrowser.Document as IPersistStreamInit).Load(TStreamAdapter.Create(ms));
finally
ms.Free;
end;
finally
sl.Free;
Doc := WebBrowser.Document as IHTMLDocument2;
end;
end;
end;
procedure TForm1.LoadHTMLBtnClick(Sender: TObject);
begin
WB_LoadHTML(Memo1.Text);
end;
procedure TForm1.LoadFileBtnClick(Sender: TObject);
begin
Memo1.Lines.LoadFromFile('G:\Programme\Apache Group\Apache\htdocs\bearbeiten1.html');
end;
我們創建了2個文件(相同)只有腳本不同才能在加載時獲得警報。
bearbeiten1.html
<script type="text/javascript">
alert ("bearbeiten1.html");
</script>
bearbeiten3.html
<script type="text/javascript">
alert ("bearbeiten3.html");
</script>
:點擊加載文件我們加載 「bearbeiten1.html」 文件
與WB_LoadHTML我們把它加載到內存。
我們得到網址:關於:空白
和警報
現在我們創建了一個鏈接:
我們選擇藍色部分,然後點擊createlink
鏈接創建
,也是新的 「Doc.body.innerHTML」
procedure TForm1.createlinkBtnClick(Sender: TObject);
begin
Doc.execCommand('createlink', false,'bearbeiten3.html');
Memo1.Text := Doc.body.innerHTML;
end;
到目前爲止好!但它會工作...? 沒有
我們得到的鏈接上的所有點擊後是一個空白網站與網址:
現在我們嘗試新的的EditText()代碼
procedure TForm1.EditText(CONST HTMLPath: string);
begin
TempFile := HTMLPath;
xLoaded := False;
WebBrowser.Navigate(TempFile);
Doc := WebBrowser.Document as IHTMLDocument2;
if Doc <> nil then xLoaded := True;
end;
procedure TForm1.EditTextBtnClick(Sender: TObject);
begin
EditText('G:\Programme\Apache Group\Apache\htdocs\bearbeiten1.html');
end;
點擊加載文件,我們再次加載「bearbeiten1.html」文件 ,並使用EditTextBtnClick直接加載它。 看起來好多了!它會工作...?
讓我們點擊鏈接!我們得到警報!來自Nr。 ... 3.html」
和.html文件被加載沒有問題。
你的其他問題
if Doc <> NIL
then Doc.Body.SetAttribute('contentEditable', 'true', 0);
//crash here when I load complex html files
你做它在錯誤的地方機構只有在網站加載後纔可用!
所以我把它放在事件WebBrowserNavigateComplete2
只有快速的解決方案可以提高
procedure TForm1.WebBrowserNavigateComplete2(Sender: TObject;
const pDisp: IDispatch; var URL: OleVariant);
begin
if xLoaded = True then begin
xBody := Doc.Get_body;
if xBody <> nil then begin
xBody.SetAttribute('contentEditable', 'true', 0);
Memo1.Text := Doc.body.innerHTML;
xLoaded := False;
end;
end;
label2.Caption := URL;
end;
完整的代碼。
type
TForm1 = class(TForm)
WebBrowser: TWebBrowser;
Label1: TLabel;
Label2: TLabel;
Memo1: TMemo;
LoadHTMLBtn: TButton;
LoadFileBtn: TButton;
EditTextBtn: TButton;
createlinkBtn: TButton;
innerHTMLBtn: TButton;
procedure LoadHTMLBtnClick(Sender: TObject);
procedure LoadFileBtnClick(Sender: TObject);
procedure EditTextBtnClick(Sender: TObject);
procedure createlinkBtnClick(Sender: TObject);
procedure WebBrowserNavigateComplete2(Sender: TObject;
const pDisp: IDispatch; var URL: OleVariant);
procedure innerHTMLBtnClick(Sender: TObject);
procedure FormCreate(Sender: TObject);
private
procedure WB_LoadHTML(HTMLCode: string);
procedure EditText(CONST HTMLPath: string);
public
{ Public-Deklarationen }
end;
var
Form1: TForm1;
implementation
{$R *.DFM}
var
Doc: IHTMLDocument2;
TempFile: string;
xBody : IHTMLElement;
xLoaded : Boolean;
onlyOnce: Boolean;
procedure TForm1.WB_LoadHTML(HTMLCode: string);
var
sl: TStringList;
ms: TMemoryStream;
begin
xLoaded := False;
WebBrowser.Navigate('about:blank');
while WebBrowser.ReadyState < READYSTATE_INTERACTIVE do
Application.ProcessMessages;
if Assigned(WebBrowser.Document) then
begin
sl := TStringList.Create;
try
ms := TMemoryStream.Create;
try
sl.Text := HTMLCode;
sl.SaveToStream(ms);
ms.Seek(0, 0);
(WebBrowser.Document as IPersistStreamInit).Load(TStreamAdapter.Create(ms));
finally
ms.Free;
end;
finally
sl.Free;
Doc := WebBrowser.Document as IHTMLDocument2;
end;
end;
end;
procedure TForm1.LoadHTMLBtnClick(Sender: TObject);
begin
WB_LoadHTML(Memo1.Text);
end;
procedure TForm1.LoadFileBtnClick(Sender: TObject);
begin
Memo1.Lines.LoadFromFile('G:\Programme\Apache Group\Apache\htdocs\bearbeiten1.html');
end;
procedure TForm1.EditText(CONST HTMLPath: string);
begin
TempFile := HTMLPath;
xLoaded := False;
WebBrowser.Navigate(TempFile);
if onlyOnce then WebBrowser.Navigate(TempFile);
onlyOnce := False;
Doc := WebBrowser.Document as IHTMLDocument2;
if Doc <> nil then xLoaded := True;
end;
procedure TForm1.EditTextBtnClick(Sender: TObject);
begin
EditText('G:\Programme\Apache Group\Apache\htdocs\bearbeiten1.html');
end;
procedure TForm1.createlinkBtnClick(Sender: TObject);
begin
Doc.execCommand('createlink', false,'bearbeiten3.html');
Memo1.Text := Doc.body.innerHTML;
end;
procedure TForm1.WebBrowserNavigateComplete2(Sender: TObject;
const pDisp: IDispatch; var URL: OleVariant);
begin
if xLoaded then begin
xBody := Doc.Get_body;
if xBody <> nil then begin
xBody.SetAttribute('contentEditable', 'true', 0);
Memo1.Text := Doc.body.innerHTML;
xLoaded := False;
end;
end;
label2.Caption := URL;
end;
procedure TForm1.innerHTMLBtnClick(Sender: TObject);
begin
Memo1.Text := Doc.body.innerHTML;
end;
procedure TForm1.FormCreate(Sender: TObject);
begin
onlyOnce := True;
end;
end.
UPDATE:
我忘了在代碼中設置(複製粘貼錯誤)的將它視爲路徑。
另外FormCreate添加。
並且只有一次加載TempFile兩次! (見代碼)
重要在臨時文件的頭標籤必須爲鏈接
bearbeiten1.html一樣bearbeiten3.html只alert ("bearbeiten3.html");
必須適應!
bearbeiten1.html
<head>
<link href="file:///G:\Programme\Apache Group\Apache\htdocs\maor.css" rel="stylesheet" media="screen">
</head>
<body leftmargin="0" marginheight="0" marginwidth="0" topmargin="0" bgcolor="#1F2E53">
<script type="text/javascript">
alert ("bearbeiten1.html");
</script>
<table width="100%" border="0" cellspacing="0" cellpadding="0" >
<tr height="211">
<td width="2%" height="211"></td>
<td valign="top" width="36%" height="211">
<table width="448" border="0" cellspacing="0" cellpadding="0">
<tr height="21">
<td width="8" height="21"></td>
<td class="FormControlrechts" width="150" height="21"></td>
<td width="23" height="21"></td>
<td class="FormControl" width="213" height="21">
<p unselectable="on">Select any portion of the following blue text</p>
<p id="p1" style="color= #3366CC">My favorite Web site. Don't forget to click the button! createlink</p>
</td>
</tr>
</table>
</table>
</body>
maor.css
body {}
p {}
td {}
h1 { color: #f5c391; font-weight: normal; font-size: 20px; font-family: verdana, serif; margin-bottom: 0.2em }
h2 { color: #eaeaea; font-weight: normal; font-size: 16px; margin-top: 0; margin-bottom: 0 }
form { margin-top: 0px }
a:link { font-weight:bold; color:#36f; text-decoration:none; }
a:visited { font-weight:bold; color:silver; text-decoration:none; }
a:focus { font-weight:bold; color:#d4d4d4; text-decoration:underline; }
a:hover { font-weight:bold; color:#c0c0c0; text-decoration:none; }
a:active { font-weight:bold; color:lime; text-decoration:underline; }
textarea, input { font-size: 8pt }
select, option { font-size: 9pt }
td { color: #333; font-size: 9pt; font-family: verdana, sans-serif }
td.FormControl { color: #ffe78b; font-size: small; padding-top: 5px; padding-bottom: 5px; border-right: 1px solid #5dafb0; border-bottom: 1px solid #5dafb0 }
td.FormControlrechts { color: #a88664; font-size: 8pt; text-align: right; padding-top: 5px; padding-bottom: 5px; border-top: #5dafb0; border-right: #5dafb0; border-bottom: 1px solid #5dafb0; border-left: #5dafb0 }
.class { }
源:http://www.swissdelphicenter.ch/en/showcode.php?id=1096 – Ampere