AlanMar

Inserting javascript into a DOM with Delphi

Jul 14th, 2013
245
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Delphi 1.11 KB | None | 0 0
  1. function InsertJavascript(const HTMLDocument2: IHTMLDocument2): Boolean;
  2. // In Delphi variables are declared at the top of a function in a "var" section
  3. var
  4.   HeadDOM: IHTMLDOMNode;
  5.   HTMLScriptElement: IHTMLScriptElement;
  6.   HTMLScriptDOM: IHTMLDOMNode;
  7. begin
  8.   // <snip> Get a IHTMLDOMNode representing the HEAD section of the document
  9.   // which is assigned to "HeadDOM" </snip>
  10.  
  11.   // e.g., insert JQuery script text into document
  12.   HTMLScriptElement := HTMLDocument2.createElement('SCRIPT') as IHTMLScriptElement;
  13.   HTMLScriptElement.setAttribute('id', 'text/javascript', 1);
  14.  
  15.   // GetDOMNodeFromHTMLElement calls QueryInterface on HTMLScriptElement, passing IHTMLDOMNode
  16.   HTMLScriptDOM := GetDOMNodeFromHTMLElement(HTMLScriptElement);
  17.  
  18.   // GetResourceAsString calls Win32 API function "FindResource" and returns a string
  19.   // that is the text of the jquery.js file (which is compiled into the BHO DLL)
  20.   HTMLScriptElement.text := GetResourceAsString(PChar(scJQueryJS), PChar('TEXT'));
  21.  
  22.   // Add the newly created IHTMLDOMNode to the HEAD section of the document
  23.   HeadDOM.appendChild(HTMLScriptDOM);
  24. end;
Advertisement
Add Comment
Please, Sign In to add comment