Last active
April 17, 2020 10:03
-
-
Save ahmubashshir/74afb3b88c0ce5ef1f3414a87d80cc99 to your computer and use it in GitHub Desktop.
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 | |
##### sync_to_async def | |
import os | |
import sys | |
import threading | |
import gi, platform, subprocess | |
gi.require_version('WebKit', '3.0') | |
gi.require_version('Gtk', '3.0') | |
from os import environ | |
import re, requests | |
from gi.repository import Gtk, Gdk, WebKit,Soup,GLib,Pango | |
#http://useragentstring.com/ | |
################################################################################################## | |
# * http://xcal1.vodafone.co.uk/ # | |
# * https://www.thinkbroadband.com/download # | |
# * https://www.engineerhammad.com/2015/04/Download-Test-Files.html # | |
# * https://speed.hetzner.de/ # | |
# * https://www.futurehosting.com/network-test-files/ # | |
# * http://ovh.net/files/ # | |
# * https://www.adam.com.au/support/blank-test-files # | |
# * https://github.com/jamesward/play-load-tests/tree/master/public # | |
# * http://speedtest.tele2.net/ # | |
# * https://www.sample-videos.com/ # | |
# * https://deskish.com/andreas/fEyyljFp/ # | |
# * https://www.ctrlswitches.com/test-network # | |
################################################################################################## | |
estr=subprocess.getoutput | |
downloading=0 | |
downloads=[] | |
cdownloads=[] | |
cookiejar=None | |
class dlTab(Gtk.Window): | |
def __init__(self,DlObj,TITLE=None,*args, **kwargs): | |
super(dlTab, self).__init__(*args, **kwargs) | |
self.Dl=DlObj | |
self.box=Gtk.VBox() | |
self.hbox=Gtk.HBox() | |
self.bar=Gtk.ProgressBar() | |
self.ret=0 | |
self.pd=0 | |
self.delay=625 | |
self.cancel=Gtk.Button.new_from_stock("gtk-cancel") | |
self.open=Gtk.Button.new_from_stock("gtk-open") | |
self.open.connect('clicked',lambda x:os.system("xdg-open "+self.Dl.get_destination_uri().replace("file://",""))) | |
self.cancel.connect('clicked',self.stop) | |
self.bar.set_show_text(True) | |
self.connect("delete-event",self.destroy_f) | |
self.sz=Gtk.Label() | |
self.sz.set_halign(Gtk.Align.START) | |
#self.sz.set_justify(Gtk.Justification.LEFT) | |
self.open.set_no_show_all(True) | |
self.skipped=False | |
fd=Gtk.FileChooserDialog( | |
title="Save..", | |
action=Gtk.FileChooserAction.SAVE, | |
buttons=( | |
"gtk-cancel", | |
Gtk.ResponseType.CANCEL, | |
"gtk-save", | |
Gtk.ResponseType.OK | |
) | |
) | |
fd.set_current_name(self.Dl.get_suggested_filename()) | |
self.ret=fd.run() | |
fd.resize(75,65) | |
if self.ret==Gtk.ResponseType.OK: | |
self.Dl.set_destination_uri(fd.get_uri()) | |
fd.destroy() | |
else: | |
fd.destroy() | |
if (self.ret==Gtk.ResponseType.OK or self.check()) and not self.skipped: | |
self.label=Gtk.Label(self.Dl.get_destination_uri().split('/')[-1]) | |
self.label.set_tooltip_text(self.Dl.get_destination_uri().split("://")[1]) | |
self.box.pack_start(self.label,True,True,0) | |
self.box.pack_start(self.sz,True,True,0) | |
self.box.pack_start(self.bar,True,True,0) | |
self.hbox.pack_start(self.box,True,True,0) | |
self.hbox.pack_start(self.open,True,True,0) | |
self.hbox.pack_start(self.cancel,True,True,0) | |
self.add(self.hbox) | |
self.show_all() | |
def start(self): | |
global downloading | |
global downloads | |
if self.Dl.get_destination_uri() not in downloads and self.Dl.get_destination_uri() not in cdownloads and (self.ret==Gtk.ResponseType.OK or self.check()) and not self.skipped: | |
self.timeout_id=GLib.timeout_add(self.delay, self.download) | |
GLib.timeout_add(100, self.seticon) | |
downloading+=1 | |
downloads.append(self.Dl.get_destination_uri()) | |
print(downloads) | |
print("Starting") | |
print(self.Dl.get_destination_uri()) | |
print(self.Dl.get_status()) | |
else: | |
self.stop() | |
def seticon(self,*args): | |
if self.Dl.get_progress()>0: | |
icontype=estr("file -b --mime-type "+self.label.get_tooltip_text()).split('/')[0] | |
self.set_icon_name ( | |
{ | |
'application':'applications-accessories', | |
'audio':'audio-x-generic', | |
'image':'image-x-generic', | |
'text':'text-x-generic', | |
'video':'video-x-generic', | |
'model':'application-certificate', | |
'font':'font-x-generic'}[icontype] | |
) | |
print(icontype) | |
return False | |
return True | |
def stop(self,*args): | |
print(self.Dl) | |
self.Dl.cancel() | |
self.destroy() | |
def destroy_f(self,*arg): | |
if self.Dl.get_destination_uri() in downloads: | |
return True | |
else: | |
return False | |
def time_hr(self,sec): | |
STR='' | |
for unit in [ 's','m','h' ]: | |
if unit=='s': | |
STR="%d%s"%(int(sec%60),unit) | |
else: | |
STR="%d%s %s"%(int(sec%60),unit,STR) | |
sec/=60 | |
if int(sec) == 0: | |
break | |
STR.strip() | |
return STR | |
def size_hr(self,size_byte,SUFF='B'): | |
if SUFF=='b': | |
units=['','k','M','G','T','P','E','Z','Y'] | |
div=1000.00 | |
else: | |
units=['','Ki','Mi','Gi','Ti','Pi','Ei','Zi','Yi'] | |
div=1024.00 | |
for unit in units: | |
if abs(size_byte) < div: | |
if size_byte-int(size_byte)==0: | |
return "%d%s%s" % (int(size_byte), unit, SUFF) | |
else: | |
return "%3.1f%s%s" % (size_byte, unit, SUFF) | |
size_byte /= div | |
def download(self,*args): | |
global downloading | |
global downloads | |
if self.Dl.get_status().value_name=='WEBKIT_DOWNLOAD_STATUS_CREATED' or self.Dl.get_status().value_name=='WEBKIT_DOWNLOAD_STATUS_STARTED': | |
pr=self.Dl.get_progress() | |
self.bar.set_fraction(pr) | |
sz=self.Dl.get_total_size() | |
rsz=sz-sz*pr | |
dsz=sz*pr | |
delta=(dsz-self.pd)/(self.delay/1000) | |
if delta>0: | |
self.sz.set_text("Size: %s/%s/%s\nETA:%s"%(self.size_hr(dsz),self.size_hr(rsz),self.size_hr(sz),self.time_hr(sz/delta))) | |
self.set_title("%.2f%%:%s:%s"%(pr*100,self.time_hr(sz/delta),self.label.get_text())) | |
else: | |
self.sz.set_text("Size: %s/%s/%s\nETA:∞"%(self.size_hr(dsz),self.size_hr(rsz),self.size_hr(sz))) | |
self.set_title("%.2f%%: ∞ :%s"%(pr*100,self.label.get_text())) | |
self.bar.set_text("%sps %.2f%%"%(self.size_hr(delta*8,SUFF='b'),pr*100)) | |
self.pd=sz*pr | |
else: | |
self.bar.hide() | |
self.cancel.hide() | |
self.open.show() | |
sz=self.Dl.get_total_size() | |
self.sz.set_text("Size: %s"%(self.size_hr(sz))) | |
downloading-=1 | |
downloads.remove(self.Dl.get_destination_uri()) | |
cdownloads.append(self.Dl.get_destination_uri()) | |
self.set_title("Completed:%d%%:%s"%(100,self.label.get_text())) | |
return False | |
return True | |
class BrowserTab(Gtk.VBox): | |
def __init__(self,URI=None, *args, **kwargs): | |
super(BrowserTab, self).__init__(*args, **kwargs) | |
self.webview = WebKit.WebView() | |
self.webviewsettings = self.webview.get_settings() | |
self.Default_UA=self.webviewsettings.get_property('user-agent') | |
#'Mozilla/5.0 (X11; Linux '+platform.machine()+') AppleWebKit/537.36 (KHTML, like Gecko) Ubuntu Chromium/68.0.3440.106 Chrome/68.0.3440.106 Safari/537.36' | |
# | |
#'Mozilla/5.0 (X11; Ubuntu; Linux '+platform.machine()+'; rv:40.0) Gecko/20100101 Firefox/61.0.2' | |
#self.Mobile_UA='Opera/9.80 (J2ME/MIDP; Opera Mini/4.2/28.3590; U; en) Presto/2.8.119 Version/11.10' | |
#self.Mobile_UA='Lynx/2.8.9dev.16 libwww-FM/2.14 SSL-MM/1.4.1 GNUTLS/3.5.17' | |
self.Mobile_UA='Mozilla/5.0 (Symbian/3; Series60/5.2 NokiaN8-00/012.002; Profile/MIDP-2.1 Configuration/CLDC-1.1 ) AppleWebKit/533.4 (KHTML, like Gecko) NokiaBrowser/7.3.0 Mobile Safari/533.4 3gpp-gba' | |
#self.Mobile_UA='Lynx/2.8.9dev.16 libwww-FM/2.14 SSL-MM/1.4.1 GNUTLS/3.5.17' | |
self.Current_UA=self.Default_UA | |
self.webviewsettings.set_property('user-agent', self.Current_UA) | |
self.webviewsettings.set_property('enable-page-cache',True) | |
self.webviewsettings.set_property('enable-running-of-insecure-content',True) | |
self.webviewsettings.set_property('html5-local-storage-database-path',os.path.join(os.environ['HOME'],".local/pybrowser/db")) | |
self.webviewsettings.set_property('javascript-can-access-clipboard',False) | |
#self.webviewsettings.set_property('',) | |
#self.webviewsettings.set_property('',) | |
#self.webviewsettings.set_property('',) | |
#self.webviewsettings.set_property('',) | |
self.webview.connect("load-error",self.checkerror) | |
self.webview.connect("notify::load-status", self.load_changed) | |
self.webview.connect("download-requested",self.download_manager) | |
self.webview.connect("navigation-policy-decision-requested",self.decision) | |
self.webview.connect("resource-request-starting",self.resource_intercept) | |
self.webview.connect('mime-type-policy-decision-requested',self.check_mime) | |
self.info=Gtk.InfoBar() | |
self.info.set_show_close_button(True) | |
self.info.connect("response",self.res) | |
self.infolabel=Gtk.Label() | |
self.infolabel.set_width_chars(25) | |
self.infolabel.set_max_width_chars(50) | |
self.infolabel.set_ellipsize(Pango.EllipsizeMode.MIDDLE) | |
self.info.set_no_show_all(True) | |
go_button = Gtk.Button.new_from_icon_name('go-next',Gtk.IconSize.MENU) | |
go_button.connect("clicked", self._load_url) | |
self.url_bar = Gtk.Entry() | |
self.url_bar.connect("activate", self._load_url) | |
self.info.get_content_area().add(self.infolabel) | |
self.infolabel.show() | |
self.show() | |
self.go_back = Gtk.Button.new_from_icon_name('go-previous',Gtk.IconSize.MENU) | |
self.go_back.connect("clicked", lambda x: self.webview.go_back()) | |
self.go_forward = Gtk.Button.new_from_icon_name('go-next',Gtk.IconSize.MENU) | |
self.go_forward.connect("clicked", lambda x: self.webview.go_forward()) | |
scrolled_window = Gtk.ScrolledWindow() | |
scrolled_window.add(self.webview) | |
find_box = Gtk.HBox() | |
close_button = Gtk.Button("Close") | |
close_button.connect("clicked", lambda x: find_box.hide()) | |
self.find_entry = Gtk.Entry() | |
self.find_entry.connect("activate", | |
lambda x: self.webview.search_text(self.find_entry.get_text(),False, True, True) | |
) | |
prev_button = Gtk.Button("Previous") | |
next_button = Gtk.Button("Next") | |
prev_button.connect("clicked", | |
lambda x: self.webview.search_text(self.find_entry.get_text(),False, False, True) | |
) | |
next_button.connect("clicked", | |
lambda x: self.webview.search_text(self.find_entry.get_text(),False, True, True) | |
) | |
find_box.pack_start(close_button, False, False, 0) | |
find_box.pack_start(self.find_entry, False, False, 0) | |
find_box.pack_start(prev_button, False, False, 0) | |
find_box.pack_start(next_button, False, False, 0) | |
self.find_box = find_box | |
url_box = Gtk.HBox() | |
url_box.pack_start(self.go_back, False, False, 0) | |
url_box.pack_start(self.go_forward, False, False, 0) | |
url_box.pack_start(self.info, True, True, 0) | |
url_box.pack_start(self.url_bar, True, True, 0) | |
url_box.pack_start(go_button, False, False, 0) | |
self.pack_start(url_box, False, False, 0) | |
self.pack_start(scrolled_window, True, True, 0) | |
self.pack_start(find_box, False, False, 0) | |
url_box.show_all() | |
scrolled_window.show_all() | |
if not URI==None: | |
if not "://" in URI: | |
self.webview.load_uri("http://" + URI) | |
self.url_bar.set_text("http://" + URI) | |
else: | |
self.webview.load_uri(URI) | |
self.url_bar.set_text(URI) | |
def check_mime(self,web_view,frame,request,mimetype,policy_decision): | |
msg=request.get_message().response_headers | |
req_type=msg.get_content_disposition() | |
if (not req_type[0] and not req_type[1]==None) or "application/" in mimetype or "video/" in mimetype or "audio/" in mimetype: | |
policy_decision.download() | |
print(msg.get_content_disposition()) | |
#self.webview.load_uri(self.url_bar.get_text()) | |
self.info.hide() | |
self.url_bar.show() | |
self.stop_loading() | |
self.info.hide() | |
self.url_bar.show() | |
return True | |
else: | |
return False | |
def stop_loading(self): | |
if self.is_loading(): | |
self.webview.stop_loading() | |
def fetch(self,uri, proxy, cookies): | |
print("Test: ", uri) | |
try: | |
if proxy is not None: | |
proxies = { | |
'http' : proxy, | |
'https': proxy | |
} | |
else: | |
proxies = {} | |
r = requests.get(uri, cookies=cookies, proxies=proxies) | |
print("Test: ",r.json()) | |
self.download_manager(None,WebKit.Download.new(WebKit.NetworkRequest.new(r.json()['URL']))) | |
except: | |
print("Test: ",sys.exc_info()) | |
return True | |
def resource_intercept(self,view,frame,res,req,*args): | |
if re.search('https?://[-\w.]+/video-download/[0-9]+',req.get_uri()): | |
try: | |
threading.Thread( | |
target = self.fetch, | |
args = ( | |
req.get_uri(), | |
WebKit.get_default_session().get_property('proxy-uri').to_string(False), | |
{ n.name: n.get_value() for n in cookiejar.get_cookie_list(Soup.URI.new(req.get_uri()), True)}, | |
) | |
).start() | |
return True | |
except: | |
print("Test: ",sys.exc_info()[0]) | |
def decision(self,view,frame,req,act,dec,*args): | |
#help(frame) | |
#help(req) | |
#help(act) | |
#help(dec) | |
if "magnet:" in req.get_uri(): | |
os.system("xdg-open '"+uri+"'") | |
dec.ignore() | |
return True | |
else: | |
return False | |
def _load_url(self, widget): | |
url = self.url_bar.get_text() | |
if not "://" in url: | |
url = "http://" + url | |
self.stop_loading() | |
self.webview.load_uri(url) | |
def reload(self): | |
self.stop_loading() | |
if self.webview.get_uri() is None and self.url_bar.get_text() != "": | |
self._load_url(self.url_bar) | |
else: | |
self.webview.reload() | |
def is_loading(self): | |
return not self.webview.get_load_status() is (WebKit.LoadStatus.FINISHED or WebKit.LoadStatus.FAILED) | |
def get_status(self): | |
return self.webview.get_load_status() | |
def load_changed(self, webview, *args): | |
if self.is_loading(): | |
self.url_bar.set_progress_fraction(self.webview.get_progress ()) | |
if not self.url_bar.get_text()==self.webview.get_uri() and self.get_status() is WebKit.LoadStatus.COMMITTED: | |
self.url_bar.set_text(self.webview.get_uri()) | |
return 0 | |
else: | |
self.url_bar.set_progress_fraction(0) | |
return 0 | |
def chUA(self): | |
if self.Current_UA == self.Mobile_UA: | |
self.Current_UA=self.Default_UA | |
else: | |
self.Current_UA=self.Mobile_UA | |
self.webviewsettings.set_property('user-agent', self.Current_UA) | |
def toggle_js(self): | |
tmp=self.webviewsettings.get_property('enable-scripts') | |
self.webviewsettings.set_property('enable-scripts',not tmp) | |
return "dis" if tmp else "en" | |
def res(self,bar,rid,*args): | |
if rid==-7: | |
bar.hide() | |
self.url_bar.show() | |
def msg(self,TYPE,message): | |
self.infolabel.set_text(message) | |
self.info.set_message_type(TYPE) | |
self.url_bar.hide() | |
self.info.show() | |
GLib.timeout_add_seconds(2,lambda:self.info.response(-7)) | |
def checkerror(self,web_view,web_frame,uri,web_error,*args): | |
if "magnet:" in uri: | |
os.system("xdg-open '"+uri+"'") | |
print(uri) | |
self.webview.go_back() | |
else: | |
self.msg(Gtk.MessageType.ERROR,"Error Occured while navigating") | |
self.webview.load_html_string(""" | |
<!DOCTYPE html> | |
<html> | |
<head><title>Navigation Failed</title><head> | |
<h2>Failed to load</h2> | |
<table> | |
<tr> | |
<td> | |
<p> | |
"""+uri+""" | |
<br /> | |
<a href='"""+uri+"""'><button>Try again</button></a> | |
<br /><a href='"""+self.url_bar.get_text()+"""'><button>Back</button></a> | |
</p> | |
</td> | |
</tr> | |
</table> | |
</html> | |
""",uri) | |
return True | |
def download_manager(self,wv, dl): | |
uri=dl.get_uri() | |
print("Hello,A download is requested as "+uri) | |
dlTab(dl,TITLE=self.webview.get_title()).start() | |
return True | |
class Browser(Gtk.Window): | |
def __init__(self,URL=None, *args, **kwargs): | |
super(Browser, self).__init__(*args, **kwargs) | |
# create notebook and tabs | |
self.set_icon_name('applications-internet') | |
self.notebook = Gtk.Notebook() | |
self.notebook.set_scrollable(True) | |
#self.notebook.style_set_property('tab-curvature',3) | |
#self.notebook.style_set_property('tab-overlap',3) | |
#self.notebook.style_set_property('tab-fill',False) | |
#self.notebook.set_property('',) | |
# basic stuff | |
self.tabs = [] | |
self.set_size_request(400,400) | |
# create a first, empty browser tab | |
button=Gtk.Button.new_from_icon_name('window-close',Gtk.IconSize.MENU) | |
button.set_relief(Gtk.ReliefStyle.NONE) | |
button.set_focus_on_click(False) | |
button.connect('clicked',self.close_activated) | |
if not URL==None: | |
page_tuple=(self._create_tab(URI=URL), Gtk.Image.new_from_icon_name('x-office-document',Gtk.IconSize.MENU) ,Gtk.Label("New Tab"),button,[0]) | |
else: | |
page_tuple=(self._create_tab(), Gtk.Image.new_from_icon_name('x-office-document',Gtk.IconSize.MENU) ,Gtk.Label("New Tab"),button,[0]) | |
page_tuple[2].set_width_chars(13) | |
page_tuple[2].set_max_width_chars(18) | |
page_tuple[2].set_ellipsize(Pango.EllipsizeMode.MIDDLE) | |
self.tabs.append(page_tuple) | |
box=Gtk.Box(Gtk.Orientation.HORIZONTAL) | |
box.pack_start(page_tuple[1],False,False,2) | |
box.pack_start(page_tuple[2],False,True,0) | |
box.pack_start(page_tuple[3],False,False,0) | |
box.show_all() | |
self.notebook.append_page(self.tabs[0][0],box) | |
self.notebook.set_tab_reorderable(self.tabs[0][0],True) | |
self.add(self.notebook) | |
if 'PROXY' in environ.keys(): | |
self._use_proxy(True) | |
self.proxy=False | |
# connect signals | |
self.connect("delete-event",self.quit) | |
self.connect("destroy",Gtk.main_quit) | |
self.connect("key-press-event", self._key_pressed) | |
self.notebook.connect("switch-page", self._tab_changed) | |
self.notebook.connect("page-reordered",self.reordar_tab) | |
self.notebook.show() | |
self.show() | |
def quit(self,*args): | |
if downloading==0 and downloads==[]: | |
return False | |
else: | |
return True | |
def close_activated(self,button): | |
for tab, icon , label,cbutton, index in self.tabs: | |
if button is cbutton and self.notebook.get_n_pages()>1: | |
print(index[0]) | |
if self.notebook.get_nth_page(index[0])==tab: | |
current_tab = self.tabs.pop(index[0]) | |
self.notebook.remove(current_tab[0]) | |
elif button is cbutton and self.notebook.get_n_pages()==1: | |
self.destroy() | |
self.reordar_tabs() | |
def icon_changed(self,webview,uri,*args): | |
for tab, icon , label,cbutton, index in self.tabs: | |
if tab.webview is webview: | |
icon.set_from_pixbuf(webview.try_get_favicon_pixbuf(16,16)) | |
else: | |
icon.set_from_icon_name('x-office-document',Gtk.IconSize.MENU) | |
self.set_icon_name('x-office-document') | |
if self.tabs[self.notebook.get_current_page()][0].webview.try_get_favicon_pixbuf(16,16)==None: | |
self.set_icon_name('x-office-document') | |
else: | |
self.set_icon(self.tabs[self.notebook.get_current_page()][0].webview.try_get_favicon_pixbuf(16,16)) | |
def _tab_changed(self, notebook, current_page, index): | |
if self.tabs[index][0].webview.get_load_status().value_name=='WEBKIT_LOAD_FINISHED': | |
title = self.tabs[index][0].webview.get_title() | |
if title[:15] < title: | |
self.set_title(title[:12]+"...") | |
else: | |
self.set_title(title) | |
if self.tabs[index][0].webview.try_get_favicon_pixbuf(16,16)==None: | |
self.set_icon_name('x-office-document') | |
else: | |
self.set_icon(self.tabs[index][0].webview.try_get_favicon_pixbuf(16,16)) | |
def _create_tab(self,**kwargs): | |
tab = BrowserTab(**kwargs) | |
tab.webview.connect("load-progress-changed", self.load_changed) | |
tab.webview.connect("icon-loaded", self.icon_changed) | |
return tab | |
def reordar_tabs(self,*args): | |
for n in self.tabs: | |
index=self.tabs.index(n) | |
self.tabs[index][4][0]=index | |
def reordar_tab(self,notebook,child,page_num,*args): | |
for n in self.tabs: | |
if child is n[0]: | |
break | |
self.tabs.insert(page_num,self.tabs.pop(n[4][0])) | |
self.reordar_tabs() | |
def _reload_tab(self): | |
ctab=self.tabs[self.notebook.get_current_page()][0] | |
ctab.reload() | |
def load_changed(self, webview, *args): | |
current_page = self.notebook.get_current_page() | |
if webview.get_load_status()==WebKit.LoadStatus.FIRST_VISUALLY_NON_EMPTY_LAYOUT: | |
counter = 0 | |
for tab, icon , label,cbutton, index in self.tabs: | |
if tab.webview is webview: | |
if not webview.get_view_source_mode(): | |
try: | |
label.set_text(webview.get_title()) | |
except TypeError: | |
uri=webview.get_uri().split("://")[1] | |
label.set_text(uri) | |
label.set_tooltip_text (webview.get_uri()) | |
else: | |
uri=webview.get_uri().split("://")[1] | |
label.set_text("source-"+uri) | |
label.set_tooltip_text("source-"+webview.get_uri()) | |
if webview.try_get_favicon_pixbuf(16,16)==None: | |
icon.set_from_icon_name('x-office-document',Gtk.IconSize.MENU) | |
if counter == current_page: | |
self._tab_changed(None, None, counter) | |
break | |
counter += 1 | |
self.set_title(self.tabs[current_page][2].get_text()) | |
else: | |
for tab, icon , label,cbutton, index in self.tabs: | |
if tab.webview is webview: | |
uri='' | |
for n in tab.url_bar.get_text().split("://")[1:]: | |
uri+=n | |
if uri[:15] < uri: | |
label.set_text(uri[:12]+"...") | |
elif not webview.get_view_source_mode(): | |
label.set_text(uri) | |
label.set_tooltip_text (webview.get_uri()) | |
def _close_current_tab(self): | |
if self.notebook.get_n_pages() == 1: | |
return | |
page = self.notebook.get_current_page() | |
current_tab = self.tabs.pop(page) | |
self.notebook.remove(current_tab[0]) | |
def _use_proxy(self,choice): | |
current_page = self.notebook.get_current_page() | |
ctab=self.tabs[current_page][0] | |
ctab.stop_loading() | |
session = WebKit.get_default_session() | |
url = 'socks5://5.133.195.161:3617' if not 'PROXY' in environ.keys() else ( environ['PROXY'] if '://' in environ['PROXY'] else 'http://%s'%environ['PROXY'] ) | |
proxy = Soup.URI.new(url) #('http://35.177.127.142:3128') | |
if choice: | |
session.set_property('proxy-uri',proxy) | |
else: | |
session.set_property('proxy-uri',None) | |
ctab.msg(Gtk.MessageType.INFO,"Proxy %sabled"%('en' if choice else 'dis')) | |
ctab.reload() | |
self.proxy=choice | |
def _open_new_tab(self): | |
current_page = self.notebook.get_current_page() | |
button=Gtk.Button.new_from_icon_name('window-close',Gtk.IconSize.MENU) | |
button.set_relief(Gtk.ReliefStyle.NONE) | |
button.set_focus_on_click(False) | |
button.connect('clicked',self.close_activated) | |
page_tuple=(self._create_tab(), Gtk.Image.new_from_icon_name('x-office-document',Gtk.IconSize.MENU) ,Gtk.Label("New Tab"),button,[current_page+1]) | |
page_tuple[2].set_width_chars(13) | |
page_tuple[2].set_max_width_chars(18) | |
page_tuple[2].set_ellipsize(Pango.EllipsizeMode.MIDDLE) | |
self.tabs.insert(current_page+1, page_tuple) | |
box=Gtk.Box(Gtk.Orientation.HORIZONTAL) | |
box.pack_start(page_tuple[1],False,False,2) | |
box.pack_start(page_tuple[2],False,True,0) | |
box.pack_start(page_tuple[3],False,False,0) | |
box.show_all() | |
self.notebook.insert_page(page_tuple[0], box, current_page+1) | |
self.notebook.set_tab_reorderable(page_tuple[0],True) | |
self.notebook.set_current_page(current_page+1) | |
self.reordar_tabs() | |
def _focus_url_bar(self): | |
current_page = self.notebook.get_current_page() | |
self.tabs[current_page][0].url_bar.grab_focus() | |
def _raise_find_dialog(self): | |
current_page = self.notebook.get_current_page() | |
self.tabs[current_page][0].find_box.show_all() | |
self.tabs[current_page][0].find_entry.grab_focus() | |
def chUA(self): | |
current_page = self.notebook.get_current_page() | |
ctab=self.tabs[current_page][0] | |
ctab.chUA() | |
ctab.msg(Gtk.MessageType.INFO,"UA Changed") | |
def toggle_js(self): | |
current_page = self.notebook.get_current_page() | |
ctab=self.tabs[current_page][0] | |
ctab.msg(Gtk.MessageType.INFO,"Javascript %sabled"%ctab.toggle_js()) | |
def vsrc(self): | |
view=self.tabs[self.notebook.get_current_page()][0].webview | |
view.set_view_source_mode(not view.get_view_source_mode()) | |
view.reload() | |
def _key_pressed(self, widget, event): | |
modifiers = Gtk.accelerator_get_default_mod_mask() | |
mapping = {Gdk.KEY_r: self._reload_tab, | |
Gdk.KEY_w: self._close_current_tab, | |
Gdk.KEY_t: self._open_new_tab, | |
Gdk.KEY_l: self._focus_url_bar, | |
Gdk.KEY_f: self._raise_find_dialog, | |
Gdk.KEY_q: Gtk.main_quit, | |
Gdk.KEY_u: self.chUA, | |
Gdk.KEY_p: lambda:self._use_proxy(not self.proxy), | |
Gdk.KEY_j: self.toggle_js, | |
Gdk.KEY_s: self.vsrc | |
} | |
if event.state & modifiers == Gdk.ModifierType.CONTROL_MASK \ | |
and event.keyval in mapping: | |
mapping[event.keyval]() | |
if __name__ == "__main__": | |
Gtk.init(sys.argv) | |
cookiejar = Soup.CookieJarText.new(os.path.join(os.environ['HOME'],".local/pybrowser/creds"), False) | |
cookiejar.set_accept_policy(Soup.CookieJarAcceptPolicy.ALWAYS) | |
session = WebKit.get_default_session() | |
session.add_feature(cookiejar) | |
try: | |
uri=sys.argv[1] | |
except: | |
uri=None | |
browser = Browser(URL=uri) | |
Gtk.main() | |
sys.exit(0) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment