コード例 #1
0
ファイル: AnchoAddon.cpp プロジェクト: yuriMalakhov/ancho
//----------------------------------------------------------------------------
//  InitializeContentScripting
STDMETHODIMP CAnchoAddon::InitializeContentScripting(IWebBrowser2* pBrowser, BSTR bstrUrl, documentLoadPhase aPhase)
{
  IF_FAILED_RET(initializeEnvironment());

  // content script handling happens here

  // no frame handling
  // TODO: decide how to handle frames
  if (!m_pWebBrowser.IsEqualObject(pBrowser)) {
    return S_OK;
  }

  if (aPhase != documentLoadEnd) {
    return S_OK;
  }

  cleanupScripting();

  // get content our API
  IF_FAILED_RET(m_pAddonBackground->GetContentInfo(m_InstanceID, bstrUrl, &m_pContentInfo));

  CString s;
  s.Format(_T("Ancho content [%s] [%i]"), m_sExtensionName, m_InstanceID);
  IF_FAILED_RET(m_Magpie->Init((LPWSTR)(LPCWSTR)s));

  // add a loader for scripts in the extension filesystem
  IF_FAILED_RET(m_Magpie->AddFilesystemScriptLoader((LPWSTR)(LPCWSTR)m_sExtensionPath));

  // inject items: chrome, console and window with global members
  CComQIPtr<IWebBrowser2> pWebBrowser(pBrowser);
  ATLASSERT(pWebBrowser);

  CIDispatchHelper contentInfo(m_pContentInfo);
  CComVariant jsObj;
  IF_FAILED_RET((contentInfo.Get<CComVariant, VT_DISPATCH, IDispatch*>(L"api", jsObj)));
  IF_FAILED_RET(DOMWindowWrapper::createInstance(pWebBrowser, m_wrappedWindow));
  m_Magpie->AddNamedItem(L"chrome", jsObj.pdispVal, SCRIPTITEM_ISVISIBLE|SCRIPTITEM_CODEONLY);
  m_Magpie->AddNamedItem(L"window", m_wrappedWindow, SCRIPTITEM_ISVISIBLE|SCRIPTITEM_GLOBALMEMBERS);

  CIDispatchHelper window = m_wrappedWindow;
  CComPtr<IDispatchEx> pRequest;
  IF_FAILED_RET(pRequest.CoCreateInstance(__uuidof(AnchoXmlHttpRequest)));

  IF_FAILED_RET(window.SetProperty((LPOLESTR)L"XMLHttpRequest", CComVariant(pRequest.p)));
  m_Magpie->AddNamedItem(L"XMLHttpRequest", pRequest, SCRIPTITEM_ISVISIBLE|SCRIPTITEM_CODEONLY);

  // get the name(s) of content scripts from manifest and run them in order
  IF_FAILED_RET((contentInfo.Get<CComVariant, VT_DISPATCH, IDispatch*>(L"scripts", jsObj)));

  VariantVector scripts;
  IF_FAILED_RET(addJSArrayToVariantVector(jsObj.pdispVal, scripts));

  for(VariantVector::iterator it = scripts.begin(); it != scripts.end(); ++it) {
    if( it->vt == VT_BSTR ) {
      m_Magpie->ExecuteGlobal(it->bstrVal);
    }
  }
  return S_OK;
}
コード例 #2
0
ファイル: AnchoRuntime.cpp プロジェクト: msarvar/ancho
//----------------------------------------------------------------------------
//  OnBrowserBeforeNavigate2
STDMETHODIMP_(void) CAnchoRuntime::OnBrowserBeforeNavigate2(LPDISPATCH pDisp, VARIANT *pURL, VARIANT *Flags,
  VARIANT *TargetFrameName, VARIANT *PostData, VARIANT *Headers, BOOL *Cancel)
{
  static bool bFirstRun = true;

  // Add the frame to the frames map so we can retrieve the IWebBrowser2 object using the URL.
  ATLASSERT(pURL->vt == VT_BSTR && pURL->bstrVal != NULL);
  CComQIPtr<IWebBrowser2> pWebBrowser(pDisp);
  ATLASSERT(pWebBrowser != NULL);

  // Workaround to ensure that first request goes through PAPP
  if (bFirstRun) {
    bFirstRun = false;
    *Cancel = TRUE;
    pWebBrowser->Stop();
    pWebBrowser->Navigate2(pURL, Flags, TargetFrameName, PostData, Headers);
    return;
  }

  // Check if this is a new tab we are creating programmatically.
  // If so redirect it to the correct URL.
  std::wstring url(pURL->bstrVal, SysStringLen(pURL->bstrVal));

  boost::wregex expression(L"(.*)://\\$\\$([0-9]+)\\$\\$(.*)");
  boost::wsmatch what;
  //TODO - find a better way
  if (boost::regex_match(url, what, expression)) {
    int requestId = boost::lexical_cast<int>(what[2].str());
    url = boost::str(boost::wformat(L"%1%://%2%") % what[1] % what[3]);

    _variant_t vtUrl(url.c_str());
    *Cancel = TRUE;
    pWebBrowser->Stop();
    pWebBrowser->Navigate2(&vtUrl.GetVARIANT(), Flags, TargetFrameName, PostData, Headers);
    mTabManager->createTabNotification(m_TabID, requestId);
    return;
  }


  VARIANT_BOOL isTop;
  if (SUCCEEDED(pWebBrowser->get_TopLevelContainer(&isTop))) {
    if (isTop) {
      // Loading the main frame so reset the frame list.
      m_Frames.clear();
      m_NextFrameId = 0;
    }
  }
  CComBSTR bstrUrl;
  removeUrlFragment(pURL->bstrVal, &bstrUrl);
  m_Frames[(BSTR) bstrUrl] = FrameRecord(pWebBrowser, isTop != VARIANT_FALSE, m_NextFrameId++);

  pWebBrowser->PutProperty(CComBSTR(L"_anchoNavigateURL"), CComVariant(*pURL));

  SHANDLE_PTR hwndBrowser = NULL;
  pWebBrowser->get_HWND(&hwndBrowser);

  if (isTop) {
    //Fill information about current document
    CComPtr<IDispatch> tmp;
    pWebBrowser->get_Document(&tmp);
    CComQIPtr<IHTMLDocument2> doc = tmp;
    if (doc) {
      CComQIPtr<IOleWindow> docWin = doc;
      HWND docWinHWND = NULL;
      if (docWin) {
        docWin->GetWindow(&docWinHWND);
      }
      if (docWinHWND) {
        gWindowDocumentMap.put(WindowDocumentRecord(docWinHWND, m_TabID, m_pWebBrowser, pWebBrowser, doc));
      }
    }
    HWND tabWindow = getTabWindow();
    if (tabWindow) {
      gWindowDocumentMap.put(WindowDocumentRecord(tabWindow, m_TabID, m_pWebBrowser, pWebBrowser, doc));
    }
  }

}