Last active
February 12, 2022 21:54
-
-
Save tokland/f2f4a0b05fde8a48dbc48a28e70f831d to your computer and use it in GitHub Desktop.
Get all requests of a page in Python using gtk webkit2
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
#!/usr/bin/python3 | |
import os | |
import sys | |
from contextlib import contextmanager | |
import gi | |
gi.require_version('Gtk', '3.0') | |
gi.require_version('WebKit2', '4.0') | |
from gi.repository import Gtk, WebKit2 | |
@contextmanager | |
def exit_on_broken_pipe(statuscode=0): | |
try: | |
yield | |
except BrokenPipeError: | |
# https://docs.python.org/3/library/signal.html#note-on-sigpipe | |
devnull = os.open(os.devnull, os.O_WRONLY) | |
os.dup2(devnull, sys.stdout.fileno()) | |
sys.exit(statuscode) | |
def on_resource(_webview, _resource, request): | |
with exit_on_broken_pipe(): | |
uri = request.get_uri() | |
print(uri, flush=True) | |
return True | |
def load(url): | |
window = Gtk.Window() | |
window.set_default_size(800, 600) | |
window.connect("destroy", Gtk.main_quit) | |
webview = WebKit2.WebView() | |
webview.connect("resource-load-started", on_resource) | |
webview.load_uri(url) | |
window.add(webview) | |
window.show_all() | |
Gtk.main() | |
if __name__ == "__main__": | |
load(sys.argv[1]) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment