Python Webkit GTK


SUBMITTED BY: Guest

DATE: April 20, 2013, 7:03 p.m.

FORMAT: Python

SIZE: 3.0 kB

HITS: 1023

  1. #!/usr/bin/env python
  2. # -*- coding: utf-8 -*-
  3. import gi
  4. gi.require_version('WebKit', '3.0')
  5. from gi.repository import WebKit
  6. from gi.repository import Soup
  7. from gi.repository import Gtk
  8. from os.path import expanduser
  9. def go_clicked(btn):
  10. url = text.get_text()
  11. if url:
  12. if not url.startswith('http'):
  13. url = 'http://'+url
  14. browser.open(url)
  15. def refresh_clicked(btn):
  16. browser.reload()
  17. def title_changed(webview, frame, title):
  18. text.set_text(frame.get_uri())
  19. def load_progress_change(webview, amount):
  20. progress.set_fraction(amount / 100.0)
  21. def load_started(webview, frame):
  22. progress.set_visible(True)
  23. def load_finished(webview, frame):
  24. html = webview.get_main_frame().get_data_source().get_data().str
  25. progress.set_visible(False)
  26. def on_destroy(widget):
  27. Gtk.main_quit()
  28. def resource_start(view, frame, resource, request, response):
  29. # Stop Requests
  30. if 'some-ad-or-site' in request.get_uri():
  31. request.set_uri ('about:blank')
  32. def resource_finish(view, frame, resource):
  33. pass
  34. window = Gtk.Window()
  35. window.connect("destroy", on_destroy)
  36. window.set_default_size(640, 480)
  37. window.set_title('Browser')
  38. go = Gtk.Button('Go!')
  39. go.connect('clicked', go_clicked)
  40. refresh = Gtk.Button(stock=Gtk.STOCK_REFRESH)
  41. refresh.connect('clicked', refresh_clicked)
  42. text = Gtk.Entry()
  43. progress = Gtk.ProgressBar()
  44. cookiejar = Soup.CookieJarText.new(expanduser("~")+'/cookies.txt', False)
  45. cookiejar.set_accept_policy(Soup.CookieJarAcceptPolicy.ALWAYS)
  46. browser = WebKit.WebView()
  47. settings = WebKit.WebSettings()
  48. user_agent = settings.get_property('user-agent')
  49. settings.set_property("enable-plugins", True)
  50. settings.set_property('user-agent', 'Mozilla/5.0 (iPad; U; CPU OS 3_2 like Mac OS X; en-us) AppleWebKit/531.21.10 (KHTML, like Gecko) Version/4.0.4 Mobile/7B334b Safari/531.21.10')
  51. browser.set_settings(settings)
  52. session = WebKit.get_default_session()
  53. session.add_feature(cookiejar)
  54. browser.open('http://example.com')
  55. browser.connect('title-changed', title_changed)
  56. browser.connect('load-progress-changed', load_progress_change)
  57. browser.connect('load-finished', load_finished)
  58. browser.connect('load-started', load_started)
  59. browser.connect('resource-request-starting', resource_start)
  60. browser.connect('resource-load-finished', resource_finish)
  61. scroller = Gtk.ScrolledWindow()
  62. scroller.add(browser)
  63. hbox = Gtk.HBox()
  64. hbox.pack_start(text, True, True, 0)
  65. hbox.pack_start(refresh, False, False, 0)
  66. hbox.pack_start(go, False, False, 0)
  67. vbox = Gtk.VBox()
  68. vbox.pack_start(hbox, False,True, 0)
  69. vbox.pack_start(scroller, True, True, 0)
  70. vbox.pack_start(progress, False, False, 0)
  71. window.add(vbox)
  72. window.show_all()
  73. Gtk.main()

comments powered by Disqus