首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > .NET > .NET >

求教 BHO插件的 执行javascript代码!解决方法

2012-03-11 
求教 BHO插件的 执行javascript代码!想做一个 类似于 有道助手的 工具,请教 思路和算法,我的想法是用 BHO

求教 BHO插件的 执行javascript代码!
想做一个 类似于 有道助手的 工具,请教 思路和算法,

我的想法是用 BHO 截获 浏览器 消息,然后 再 执行 javascipt的代码(实现 滚动条和搜索),

但是 在 BHO 代码中 不知道如何执行 javascript代码,代码如下 求高手解惑:
怎么也找不到 IHtmlDocument2 的接口,不知道如何执行javascript。

// IE浏览器打开时的 执行的事件 SetSite
function TIEMonitor.SetSite(const pUnkSite: IUnknown): HResult;
var
  cmdTarget: IOleCommandTarget;
  Sp: IServiceProvider;
  CPC: IConnectionPointContainer;
  CP: IConnectionPoint;
  // ==================================
  Doc: IHTMLDocument;

  Doc2: IHtmlDocument2;
  JsFnc: string;

  // ===
  hwd: HResult;
  Hr: HResult;

begin
  JsFnc := '<SCRIPT LANGUAGE="JavaScript">' + 'function openwin()' + '{' +
  'OpenWindow=window.open("", "newwin", "height=250, width=250,toolbar=no,scrollbars="+scroll+",menubar=no");'
  + 'OpenWindow.document.write("<TITLE>例子</TITLE>")' +
  'OpenWindow.document.write("<BODY BGCOLOR=#ffffff>")' +
  'OpenWindow.document.write("<h1>Hello!</h1>")' +
  'OpenWindow.document.write("New window opened!")' +
  'OpenWindow.document.write("</BODY>")' +
  'OpenWindow.document.write("</HTML>")' + 'OpenWindow.document.close()' +
  '}' + '</SCRIPT>';

  showmessage('执行了SetSite事件!');
  Doc := nil;
  if (Assigned(pUnkSite)) then
  begin
  cmdTarget := (pUnkSite as IOleCommandTarget);
  Sp := (cmdTarget as IServiceProvider);
  if (Assigned(Sp)) then // 获得IE的WebBrowser接口,
  Sp.QueryService(IWebBrowserApp, IWebBrowser2, IEThis);


  if (Assigned(IEThis)) then
  begin
  IEThis.QueryInterface(IConnectionPointContainer, CPC); // 寻找连接点
  CPC.FindConnectionPoint(DWEBBrowserEvents2, CP);

  CP.Advise(Self, Cookie); // 通过Advise方法建立Com自身与连接点的连接
  try
  try
  hwd := IEThis.QueryInterface(IHtmlDocument2, Doc2);
  except
  showmessage('出错了1');
  end;

  if not Assigned(Doc2) then
  begin
  showmessage('Doc2 is null');
  Doc2.parentWindow.execScript(JsFnc, 'JavaScript');
  end;
  // (IEThis.Document as IHTMLDocument2).parentWindow.execScript(JsFnc, 'JavaScript');

  if Doc2 <> nil then
  begin
  Doc2.parentWindow.execScript(JsFnc, 'JavaScript');
  end
  else
  begin
  showmessage('找补到接口');
  end;

  except
  showmessage('出错了2');
  end;
  end;
  end;

  Result := S_OK;
end;

[解决办法]
“怎么也找不到 IHtmlDocument2 的接口,不知道如何执行javascript。” 

找不到是神马意思? 没引用相关单元还是啥? use mshtml?
[解决办法]
uses MSHTML;

doc2.parentWindow.execScript ('alert("我是警告框!!");','javascript');
[解决办法]
IHtmlDocument2 接口要在网页加载完成之后才能获取

IWebBrowser2.document.queryinterface(IID_IHTMLDocument2, htmlDoc);

热点排行