I tried to handle the webview resource_load_started and webresource finished signals to get the response body, but I ran into trouble.
Here are my python codes:
import gi
gi.require_version('WebKit2', '4.0')
gi.require_version("Gtk", "3.0")
from gi.repository import WebKit2, Gtk
window = Gtk.Window()
window.connect('delete-event', Gtk.main_quit)
view = WebKit2.WebView()
view.load_uri('https://yandex.com/')
def resorce_load_finished(resource):
raw_data = resource.get_data(cancellable=None, callback=None, user_data=None)
print("url: ", resource.get_response().get_uri(), "data_length: ", resource.get_response().get_content_length(), "\r\nraw_data: ", raw_data)
def resorce_load_started(view, resource, request):
resource.connect("finished", resorce_load_finished)
view.connect("resource_load_started", resorce_load_started)
window.add(view)
window.set_default_size(1024,1024)
window.show_all()
Gtk.main()
And the output as follow(https prefix removed):
/home/again/Documents/webkit2/venv/bin/python /home/again/Documents/webkit2/get_resource.py
url: //lazka.github.io/pgi-docs/#WebKit2-4.0 data_length: 1842
raw_data: None
url: //lazka.github.io/pgi-docs/sidebar.html data_length: 6344
raw_data: None
url: //lazka.github.io/pgi-docs/WebKit2-4.0/ data_length: 2216
raw_data: None
url: //lazka.github.io/pgi-docs/js.cookie-2.1.0.min.js data_length: 874
raw_data: None
url: //lazka.github.io/pgi-docs/_static/css/pgi.css data_length: 1047
raw_data: None
url: //lazka.github.io/pgi-docs/_static/graphviz.css data_length: 215
raw_data: None
url: //lazka.github.io/pgi-docs/pgi-config.js data_length: 388
raw_data: None
url: //lazka.github.io/pgi-docs/pgi-searchtools.js data_length: 2973
raw_data: None
url: //lazka.github.io/pgi-docs/_static/css/theme.css data_length: 19061
raw_data: None
url: //lazka.github.io/pgi-docs/_static/fonts/lato-bold.ttf data_length: 39816
raw_data: None
url: //lazka.github.io/pgi-docs/_static/fonts/fontawesome-webfont.ttf data_length: 83998
raw_data: None
url: //lazka.github.io/pgi-docs/_static/fonts/lato-regular.ttf data_length: 39653
raw_data: None
Process finished with exit code 0
My question is do I use correct signal handler? If used correctly, then, Why resource.get_data()
always return None, what happen there? I can only get the response URL and its length.
must be call get_data_finish
to get final result. something like this:
def res_finished(resource, result, error):
data = resource.get_data_finish(result)
print(resource.get_uri(), data)
def received_data(resource, data_length):
resource.get_data(cancellable=None, callback=res_finished, user_data=None)
def resource_load_started(view, resource, request):
resource.connect("received_data", received_data)
view.connect("resource_load_started", resource_load_started)