Created
January 13, 2017 11:21
-
-
Save vfaronov/f1c1faa969c58b5990d02f5d9890b180 to your computer and use it in GitHub Desktop.
Problem when handling form submission with WebKitGTK+ 2.14
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# To run: install (Ubuntu package names) | |
# ``python3-gi``, ``gir1.2-gtk-3.0``, ``gir1.2-webkit2-4.0``, | |
# then do ``python3 webkitgtk_form.py`` | |
import ctypes | |
import gi | |
gi.require_version('Gtk', '3.0') | |
gi.require_version('WebKit2', '4.0') | |
from gi.repository import Gtk, WebKit2 | |
def main(): | |
window = Gtk.Window() | |
window.connect('delete-event', lambda *_: Gtk.main_quit()) | |
view = WebKit2.WebView() | |
window.add(view) | |
view.connect('submit-form', on_submit_form, []) | |
view.load_html('<form> <input name=foo> <input type=submit> </form>') | |
window.show_all() | |
Gtk.main() | |
def on_submit_form(view, request, refs): | |
# Handle the form submission (= do something with the submitted data). | |
for k, v in request.get_text_fields().items(): | |
# Note how I have to do the extra `ctypes.string_at` step, | |
# because `k` and `v` are pointers (Python integers, not bytestrings). | |
# I think this may be a bug, but it's unrelated to my problem. | |
print(ctypes.string_at(k) + b'=' + ctypes.string_at(v)) | |
# After handling the form, we want to prevent its further submission. | |
# This works with WebKitGTK+ 2.12, but under 2.14 the WebView just "hangs". | |
refs.append(request) | |
if __name__ == '__main__': | |
main() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment