下面一段是希望在webbrowser读取文件完毕后,将其中的script弹出窗口直接用
href表示。
//从Stream中读取源码到IHtmlDocument2
procedure LoadDocumentSourceFromStream(Document: IDispatch;
Stream: TStream);
var
PersistStreamInit: IPersistStreamInit;
StreamAdapter: IStream;
begin
Stream.Position:= 0;
if Document.QueryInterface(IPersistStreamInit,
PersistStreamInit) = S_OK then
begin
StreamAdapter:= TStreamAdapter.Create(Stream, soReference);
PersistStreamInit.Load(StreamAdapter);
StreamAdapter:= nil;
end;
end;
function GetHtmlDocument(Document: IDispatch
var AString:String): Boolean;
var
StrStream:TStringStream;
begin
Result := false;
if Document <> nil then
begin
StrStream := TStringStream.Create('');
SaveDocumentSourceToStream(Document,StrStream);
AString := StrStream.DataString;
StrStream.Free;
result := true;
end;
end;
procedure TMainForm.WebBrowser1DocumentComplete(Sender: TObject;
const pDisp: IDispatch
var URL: OleVariant);
var
tslSource: TStringList;
sText: string;
i, iPos1, iPos2, iBegin: integer;
sSongID: string;
bChanged: boolean;
const
sSign1: string = '<a href=''#'' onClick=''javascript
opupWindow("Mp3_Music.asp?MusicID=';
sSign2: string = '","Mp3CD_List",468,300);'''' style=''TEXT-DECORATION: none''>></a></td>';
sReplaceSignBegin: string = '<a href=''Mp3_Music.asp?MusicID=';
sReplaceSignEnd: string = '''>></a></td>';
begin
if GetHtmlDocument(WebBrowser1.Document, sText) then
begin
tslSource := TStringList.Create;
try
bChanged := false;
tslSource.Text := sText;
for i := 0 to tslSource.Count - 1 do
begin
iPos1 := Pos(sSign1, tslSource
);
iPos2 := Pos(sSign2, tslSource);
if (iPos1 > 0) and (iPos2 > 0) then
begin
bChanged := true;
iBegin := iPos1 + Length(sSign1);
sSongID := Copy(tslSource, iBegin, iPos2 - iBegin);
tslSource := Copy(tslSource, 1, iPos1 - 1) + sReplaceSignBegin + sSongID + sReplaceSignEnd;
end;
end;
if bChanged then
WebBrowser1.LoadFromStrings(tslSource);
finally
tslSource.Free;
end;
end;
end;