2

I have a webkit-sharp WebView which I am using to display HTMLvia the LoadString method.

The webview is placed in a ScrolledWindow the ScrolledWindow is placed in a Gtk Window.

I want to be able to tell the WebView to scroll to a specific part of the HTML. Normally one would do this using an anchor.

I have defined an anchor and some JavaScript to jump to that anchor, I call the JavaScript via the ExecuteScript method. This does nothing at all.

I have also tried adding a button to the HTML that calls the JavaScript. This also does nothing.

Is there something I can do to make this work, to make it so I can scroll to a known location in the page?

Update: I can make this work by saving the HTML to a file and then loading from there using a URL which tells it to scroll. However I would like to avoid doing that because of the performance hit of writing the page to disk before displaying it.

Owen Pauling
  • 11,349
  • 20
  • 53
  • 64
trampster
  • 8,598
  • 4
  • 37
  • 52

3 Answers3

0

How are you adding to ScrolledWindow? I am not GTK expert, but have discovered that it works if you call "add" method and does not work if you call add_with_viewport

Also you may not need to execute the javascript. It looks like you control the html page and can easily access the dom element and call click method on the element.Following sample python code looks for anchor with id "one" and scrolls to desired location without calling javascript.

from gi.repository import WebKit 
from gi.repository import Gtk 
from gi.repository import GLib, GObject

class WebkitApp:
    def exit(self, arg, a1):
        Gtk.main_quit()

    def onLoad(self, view, frame):
        doc = view .get_dom_document()
        a = doc.get_element_by_id("one")
        a.click()

    def __init__(self):
        win = Gtk.Window()
        self.view = WebKit.WebView()

        # Signal Connections
        self.view.connect("onload-event", self.onLoad)
        file = open("/tmp/epl-v10.html")
        html = file.read()
        self.view.load_string(html, "text/html", "UTF-8", "file:///tmp")
        sw = Gtk.ScrolledWindow() 
        # sw.add_with_viewport(self.view)
        sw.add(self.view);
        win.add(sw)
        win.maximize()
        win.connect("delete-event", self.exit)
        win.show_all()

app = WebkitApp()

Gtk.main()      
user937720
  • 182
  • 1
  • 1
  • 5
0

The JavaScript to jump to that anchor, doesn't work while the page is still loading, By waiting for the page load to finish (there is an event for it) then running the script I made it work.

trampster
  • 8,598
  • 4
  • 37
  • 52
0

Given an element:

<a id='myElement'>I want to scroll to this line</a>

This can be scrolled to with this JavaScript:

document.getElementById('myElement').scrollIntoView();

Which can be executed in a WebView with the ExecuteScript method, e.g.:

_webView.ExecuteScript("document.getElementById('myElement').scrollIntoView();");
Owen Pauling
  • 11,349
  • 20
  • 53
  • 64