[SOLVED] Need help with CefV8Context::Eval()

Having problems with building or using CEF's C/C++ APIs? This forum is here to help. Please do not post bug reports or feature requests here.

[SOLVED] Need help with CefV8Context::Eval()

Postby fasecero » Tue Jul 05, 2016 6:36 pm

Hi, I need to get the current vertical scroll value. The code below works fine, but as soon as the zoom change, the return value is always ZERO.

Code: Select all
   CefRefPtr<CefFrame> FocusedFrame = browser->GetMainFrame(); // GetFocusedFrame

   if (FocusedFrame)
   {
      CefRefPtr<CefV8Context> v8Context = FocusedFrame->GetV8Context();

      if (v8Context)
      {
         // CURRENT SCROLL VALUE
         v8Context->Eval("window.pageYOffset", ret, excep);
         int value = ret->GetIntValue();
      }
   }


value integer is greater than zero only when no zoom is applied to the page. What could be the problem?

--- --- ---

Just one more hint, maybe it can help. Saving the code below as a .HTM file, open it with cefclient, and make sure the vertical scrollbar is visible, the page will correctly display an alert with the correct scroll value, regardless of the zoom factor. But, even on this page, the code above will get ZERO for the current scroll value (if zoom is not the default).

So I dont understand why window.pageYOffset works OK after a zoom change in the code below, but it fails in the code above.

Code: Select all
<html>
<head>
    <script type="text/javascript">
        function GetScrollPosition () {
            var scrollTop = window.pageYOffset;
            alert ("The current vertical scroll amount: " + scrollTop + "px");
        }
    </script>
</head>
<body>
    <div style="height:200px; width:600px; background-color:#e0a0a0;">
   Please resize the browser window until the vertical scrollbar appears.
        Please scroll the window vertically and click on the button.
    </div>

    <button onclick="GetScrollPosition ();">Get current scroll amount</button>

    <div style="height:200px; width:600px; background-color:#e0a0a0;">
   Please resize the browser window until the vertical scrollbar appears.
        Please scroll the window vertically and click on the button.
    </div>
</body>
</html>
Last edited by fasecero on Tue Jul 12, 2016 3:35 pm, edited 3 times in total.
fasecero
Mentor
 
Posts: 60
Joined: Mon May 12, 2014 2:53 pm

Re: Need help with CefV8Context::Eval()

Postby fasecero » Tue Jul 05, 2016 10:20 pm

Got it. It took me a while though. After zoom scroll values have decimals. The working code is:

Code: Select all
int value = (int) ret->GetDoubleValue();


which works well in all cases.
Last edited by fasecero on Sat Jul 09, 2016 5:25 pm, edited 1 time in total.
fasecero
Mentor
 
Posts: 60
Joined: Mon May 12, 2014 2:53 pm

Need help with CefV8Context::Eval()

Postby fasecero » Sat Jul 09, 2016 5:23 pm

Unfortunately in some websites it seems not to work any javascript function. For example, http://html5test.com/ or https://github.com/.

By using v8Context->Eval()

Code: Select all
if (v8Context->Eval("window.pageYOffset", ret, excep) == false)
   CefString text = excep->GetMessageW();


I get the following exception message:

Image

When looking at this error on the web, there are many results but I can not find any CEF-related.
fasecero
Mentor
 
Posts: 60
Joined: Mon May 12, 2014 2:53 pm

Re: Need help with CefV8Context::Eval()

Postby Czarek » Sun Jul 10, 2016 1:37 am

Content security policy was probably added in response headers. I think you could modify it using CefRequestHandler.
Maintainer of the CEF Python, PHP Desktop and CEF C API projects. My LinkedIn.
User avatar
Czarek
Virtuoso
 
Posts: 1927
Joined: Sun Nov 06, 2011 2:12 am

Re: Need help with CefV8Context::Eval()

Postby fasecero » Sun Jul 10, 2016 2:07 pm

Thanks, I will check to see where I can arrive with CefRequestHandler. I understand that these pages prevent javascript evaluation for security but I just want some scroll info.

If javascript is the only way and some pages reject javascript evaluation it seems like a dead end :)
fasecero
Mentor
 
Posts: 60
Joined: Mon May 12, 2014 2:53 pm

Re: Need help with CefV8Context::Eval()

Postby fasecero » Sun Jul 10, 2016 4:55 pm

According to http://content-security-policy.com/, I understand that I have to add 'unsafe-eval' in "Content-Security-Policy" header, but where I have to look at to find this header?

I've tried the following with no success:

Code: Select all
  virtual ReturnValue OnBeforeResourceLoad(
     CefRefPtr<CefBrowser> browser,
     CefRefPtr<CefFrame> frame,
     CefRefPtr<CefRequest> request,
     CefRefPtr<CefRequestCallback> callback) {

     CefRequest::HeaderMap map;

     request->GetHeaderMap(map);

      for (CefRequest::HeaderMap::iterator it = map.begin(); it != map.end(); it++) {
       std::wstring first = it->first.ToWString();
       std::wstring second = it->second.ToWString();

       MessageBox(0, second.c_str(), first.c_str(), 0); // no "Content-Security-Policy" key in here
      }

     return RV_CONTINUE;
  }
fasecero
Mentor
 
Posts: 60
Joined: Mon May 12, 2014 2:53 pm

Re: Need help with CefV8Context::Eval()

Postby amaitland » Sun Jul 10, 2016 6:15 pm

It will be a Response header, your looking in the Request.
Maintainer of the CefSharp project.
amaitland
Virtuoso
 
Posts: 1291
Joined: Wed Jan 14, 2015 2:35 am

Re: Need help with CefV8Context::Eval()

Postby fasecero » Sun Jul 10, 2016 11:01 pm

Thank you very much, amaitland. Now I can intercept "Content-Security-Policy"

Code: Select all
 virtual bool OnResourceResponse(CefRefPtr<CefBrowser> browser,
     CefRefPtr<CefFrame> frame,
     CefRefPtr<CefRequest> request,
     CefRefPtr<CefResponse> response) {

     CefRequest::HeaderMap map;

     response->GetHeaderMap(map);

      CefRequest::HeaderMap::iterator it = map.find("Content-Security-Policy");

       if (it != map.end()) {
         std::wstring first = it->first.ToWString();
         std::wstring second = it->second.ToWString();

         MessageBox(0, second.c_str(), first.c_str(), 0);
      }

     return false;
  }


but still it seems like there is no way to get this working, according to the header file:

Code: Select all
  // |response| object cannot be modified in this callback.
fasecero
Mentor
 
Posts: 60
Joined: Mon May 12, 2014 2:53 pm

Re: Need help with CefV8Context::Eval()

Postby Czarek » Mon Jul 11, 2016 2:18 am

Looks like you have to implement GetResourceHandler(). In CefResourceHandler::GetResponseHeaders() you can modify the response object. It is a lot of work to implement CefResourceHandler. I think there should a way to modify the response object using GetResourceResponseFilter() / CefResponseFilter.Filter(). Currently you can only modify content, not headers. Maybe an issue in the tracker should be created.
Maintainer of the CEF Python, PHP Desktop and CEF C API projects. My LinkedIn.
User avatar
Czarek
Virtuoso
 
Posts: 1927
Joined: Sun Nov 06, 2011 2:12 am

Re: Need help with CefV8Context::Eval()

Postby fasecero » Mon Jul 11, 2016 3:15 am

Yes... before reading your comment I started to implement CefResourceHandler and I realized how complex it would be.

I think the best I can do is forget about this idea. Perhaps it is best to just not modify the Security Policy established by another site owner...

It seems like if "Content Security Policy" becomes something "standard" Eval() will ceases to be useful for third-party websites and we could not get any info about them using the only tool we have, js.

---

As a side note, I also tried with standard winapi functions but they neither worked =/
Code: Select all
   HWND b_hwnd = browser->GetHost()->GetWindowHandle();
   HWND child = GetWindow(b_hwnd, GW_CHILD);

   SCROLLINFO si;

   si.cbSize = sizeof(si);
   si.fMask = SIF_ALL;
   GetScrollInfo(child, SB_VERT, &si);

   TCHAR buf[100];
   swprintf(buf, L"%d", si.nMax);
   MessageBox(0, L"MAX SCROLL: ", buf, 0); // NOT WORKING
fasecero
Mentor
 
Posts: 60
Joined: Mon May 12, 2014 2:53 pm

Next

Return to Support Forum

Who is online

Users browsing this forum: Google [Bot] and 37 guests