#!/usr/bin/env python
# -*- coding: utf-8 -*-
import gi
gi.require_version('WebKit', '3.0')
from gi.repository import WebKit
from gi.repository import Soup
from gi.repository import Gtk
from os.path import expanduser
def go_clicked(btn):
url = text.get_text()
if url:
if not url.startswith('http'):
url = 'http://'+url
browser.open(url)
def refresh_clicked(btn):
browser.reload()
def title_changed(webview, frame, title):
text.set_text(frame.get_uri())
def load_progress_change(webview, amount):
progress.set_fraction(amount / 100.0)
def load_started(webview, frame):
progress.set_visible(True)
def load_finished(webview, frame):
html = webview.get_main_frame().get_data_source().get_data().str
progress.set_visible(False)
def on_destroy(widget):
Gtk.main_quit()
def resource_start(view, frame, resource, request, response):
# Stop Requests
if 'some-ad-or-site' in request.get_uri():
request.set_uri ('about:blank')
def resource_finish(view, frame, resource):
pass
window = Gtk.Window()
window.connect("destroy", on_destroy)
window.set_default_size(640, 480)
window.set_title('Browser')
go = Gtk.Button('Go!')
go.connect('clicked', go_clicked)
refresh = Gtk.Button(stock=Gtk.STOCK_REFRESH)
refresh.connect('clicked', refresh_clicked)
text = Gtk.Entry()
progress = Gtk.ProgressBar()
cookiejar = Soup.CookieJarText.new(expanduser("~")+'/cookies.txt', False)
cookiejar.set_accept_policy(Soup.CookieJarAcceptPolicy.ALWAYS)
browser = WebKit.WebView()
settings = WebKit.WebSettings()
user_agent = settings.get_property('user-agent')
settings.set_property("enable-plugins", True)
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')
browser.set_settings(settings)
session = WebKit.get_default_session()
session.add_feature(cookiejar)
browser.open('http://example.com')
browser.connect('title-changed', title_changed)
browser.connect('load-progress-changed', load_progress_change)
browser.connect('load-finished', load_finished)
browser.connect('load-started', load_started)
browser.connect('resource-request-starting', resource_start)
browser.connect('resource-load-finished', resource_finish)
scroller = Gtk.ScrolledWindow()
scroller.add(browser)
hbox = Gtk.HBox()
hbox.pack_start(text, True, True, 0)
hbox.pack_start(refresh, False, False, 0)
hbox.pack_start(go, False, False, 0)
vbox = Gtk.VBox()
vbox.pack_start(hbox, False,True, 0)
vbox.pack_start(scroller, True, True, 0)
vbox.pack_start(progress, False, False, 0)
window.add(vbox)
window.show_all()
Gtk.main()