Delphi-PRAXiS

Delphi-PRAXiS (https://www.delphipraxis.net/forum.php)
-   Netzwerke (https://www.delphipraxis.net/14-netzwerke/)
-   -   Delphi javaScript add to webbrowser (https://www.delphipraxis.net/54190-javascript-add-webbrowser.html)

qicki 29. Sep 2005 12:40


javaScript add to webbrowser
 
Hallo ich hab da ein problem mit zugriffsverletzungen in dem Code


Procedur zum hinzufügen von Javascript in den Browser
Delphi-Quellcode:
procedure AppendToWB(document: IHTMLDocument2);
var
  novoScript : IHTMLDOMNode;
  i: integer;
  ole_index: OleVariant;
  frame_dispatch: IDispatch;
  frame_win: IHTMLWindow2;
  frame_doc: IHTMLDocument2;
begin
  if Assigned(document) then
  begin

    novoScript := document.createElement('script') as IHTMLDOMNode;
    (novoScript as IHTMLScriptElement).text := 'function setTimeout(){};function window.focus(){};function focus(){};function window.alert(){};function alert(){}; function window.confirm(){};function confirm(){}; function window.prompt(){};function prompt(){};function window.open(){};';
    (document.body as IHTMLDOMNode).appendChild(novoScript);

    for i:=0 to (document.Frames.Length -1) do
    begin
      ole_index := i;
      frame_dispatch := document.Frames.Item(ole_index);
      if frame_dispatch <> nil then
      begin
        frame_win := frame_dispatch as IHTMLWindow2;
        frame_doc := frame_win.document;
        if Assigned(frame_doc) then
        begin
          novoScript := frame_doc.createElement('script') as IHTMLDOMNode;
          (novoScript as IHTMLScriptElement).text := 'function setTimeout(){};function window.focus(){};function focus(){};function window.alert(){};function alert(){}; function window.confirm(){};function confirm(){}; function window.prompt(){};function prompt(){};function window.open(){};';
          (frame_doc.body as IHTMLDOMNode).appendChild(novoScript);
        end;
      end;
    end;
  end;
end;
ondocomentcomplete Ereigniss
Delphi-Quellcode:
procedure TForm1.WBLoad(ASender: TObject; const pDisp: IDispatch;var URL: OleVariant);
var
  currentBrowser: IWebBrowser2;
  topBrowser: IWebBrowser2;
  document: IHTMLDocument2;
  windowName: string;
begin
  currentBrowser := pDisp as IWebBrowser2;
  topBrowser := (ASender as TWebBrowser).DefaultInterface;
  if currentBrowser = topBrowser then
  begin
    document := currentBrowser.Document as IHTMLDocument2;
    AppendToWB(document);
  end
end;
Also das ondocomentcomplete Ereigniss wird ja jedesmal aufgerufen wenn Seite/Frame geladen wurde und mit dem Verleich stellt man ja fest ob alle Frames der Seite geladen wurden. Wenn das geschieht soll er die procedur AppendToWB(document) aufrufen , was er auch tut. In der Prozedur soll er dann zu der Seite mit allen Frames die Javascriptelemente hinzufügen. Für die Hauptseite ist das niemals das problem , nur bei dem Frames erhalte ich bei frame_doc := frame_win.document; die Zugriffsverletzung . Ich weis nicht was ich falsche mache , hat jemand einen Tip ?

qicki 29. Sep 2005 14:38

Re: javaScript add to webbrowser
 
ok habs jetzt doch selber herausgefunden


Delphi-Quellcode:
procedure AppendToWBA(document: IHTMLDocument2);
var
  novoScript : IHTMLDOMNode;
begin
  if Assigned(document) then
  begin
     novoScript := document.createElement('script') as IHTMLDOMNode;
    (novoScript as IHTMLScriptElement).text := 'function setTimeout(){};function window.focus(){};function focus(){};function window.alert(){};function alert(){}; function window.confirm(){};function confirm(){}; function window.prompt(){};function prompt(){};function window.open(){};';
    (document.body as IHTMLDOMNode).appendChild(novoScript);
  end;
end;

procedure TForm1.WBLoad(ASender: TObject; const pDisp: IDispatch;var URL: OleVariant);
var
  currentBrowser: IWebBrowser2;
  document: IHTMLDocument2;
begin
  currentBrowser := pDisp as IWebBrowser2;
  document := currentBrowser.Document as IHTMLDocument2;
  AppendToWBA(document);
end;
manchmal ich weniger halt doch mehr :)


Alle Zeitangaben in WEZ +1. Es ist jetzt 04:18 Uhr.

Powered by vBulletin® Copyright ©2000 - 2024, Jelsoft Enterprises Ltd.
LinkBacks Enabled by vBSEO © 2011, Crawlability, Inc.
Delphi-PRAXiS (c) 2002 - 2023 by Daniel R. Wolf, 2024 by Thomas Breitkreuz