Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get and set TChromium scrollbar positions?

How to get and set TChromium scrollbar positions in Delphi ?

like image 673
Pongpitak Rattanawicharn Avatar asked Dec 17 '12 12:12

Pongpitak Rattanawicharn


People also ask

How do you get the scrollbar position?

To get or set the scroll position of an element, you follow these steps: First, select the element using the selecting methods such as querySelector() . Second, access the scroll position of the element via the scrollLeft and scrollTop properties.

How do I change the scroll position to top in CSS?

If you want to set the scroll position of document. body , you can scroll the entire window altogether using window. scrollTo() ; it takes either a pair of coordinates (x,y) or an options object – if you just want to scroll nicely to the top, try window. scrollTo({top:0,behavior:'smooth'}); .


2 Answers

It's possible to work with javascript objects directly. Just use CefV8Context of the frame.

Here is an example:

var
    val: ICefV8Value;
    context: ICefv8Context;
    excp: ICefV8Exception;
    scroll: TPoint;
begin
    if (Chromium1.Browser.MainFrame = nil) then
      exit;

    //this will work only with exact frame
    context := Chromium1.Browser.MainFrame.GetV8Context;

    if (context <> nil) then
    begin
        context.Eval('window.pageXOffset', val, excp);
        scroll.x := val.GetIntValue;
        context.Eval('window.pageYOffset', val, excp);
        scroll.y := val.GetIntValue;
    end
    else
      exit;

    //todo: do something with scroll here
end;
like image 104
kormizz Avatar answered Oct 10 '22 05:10

kormizz


Currently playing with CefSharp, I do think that this is similar than in Delphi. Here is my solution:

public int GetVerticalScrollPosition()
{
    var r = _webView.EvaluateScript(@"document.body.scrollTop");
    return Convert.ToInt32(r);
}

public void SetVerticalScrollPosition(int pos)
{
    _webView.ExecuteScript(
        string.Format(@"document.body.scrollTop = {0}", pos));
}

I'm not that Delphi expert anymore, hope you can understand my code; basically I use JavaScript to read/write the scroll positions and execute these small JavaScript snippets through the EvaluateScript and ExecuteScript methods.

like image 33
Uwe Keim Avatar answered Oct 10 '22 04:10

Uwe Keim