Compare commits

...

3 Commits

Author SHA1 Message Date
Fierelier 25d141d7f8 Add HTML parser, properly remove img tags 2021-03-19 03:20:20 +01:00
Fierelier c3f71086d1 Add status bar 2021-03-19 03:18:42 +01:00
Fierelier d468d76b54 Time stuff for debug 2021-03-19 03:18:18 +01:00
2 changed files with 66 additions and 7 deletions

View File

@ -26,6 +26,7 @@ from qtpy.QtCore import *
from qtpy.QtMultimedia import QSound
import configparser
import json
import time
os.chdir(sp)
distro = os.path.splitext(s.rsplit(os.path.sep)[-1])[0]
@ -97,6 +98,8 @@ class browserWindow(QMainWindow):
self.cDoc = browserDoc(self)
self.cStatusBar = QStatusBar(self)
self.cResizeElements()
self.show()
self.cNavigate()
@ -110,7 +113,11 @@ class browserWindow(QMainWindow):
self.cButtonGo.resize(barSize,barSize)
self.cDoc.move(0,mb + barSize)
self.cDoc.resize(self.cWidth,self.cHeight - barSize - mb)
self.cDoc.resize(self.cWidth,self.cHeight - barSize - mb - mb)
self.cStatusBar.move(0,self.cHeight - mb)
self.cStatusBar.resize(self.cWidth,mb)
self.cStatusBar.showMessage("Status")
def resizeEvent(self,event):
self.cWidth = self.width()
@ -119,12 +126,20 @@ class browserWindow(QMainWindow):
def cNavigate(self,event = None):
try:
print(prettyJson(parseUrl(self.cUrlBar.text())))
#print(prettyJson(parseUrl(self.cUrlBar.text())))
start = time.time()
response = downloadPage(self.cUrlBar.text(),{"User-Agent": self.cUserAgent})
end = time.time()
print("Downloading page: " +str(end - start))
infoFetcher(response)
self.cDocumentInfo = response
start = time.time()
self.cDoc.cRenderHtml(response["body"].decode("utf-8",errors="ignore"))
print(prettyJson(response["headers"]))
end = time.time()
print("Rendering page: " +str(end - start))
#print(prettyJson(response["headers"]))
except Exception as e:
self.cDoc.cRenderHtml(str(e))
raise

View File

@ -1,3 +1,9 @@
global HTMLParser
from html.parser import HTMLParser
global html
import html
global browserDoc
class browserDoc(QTextBrowser):
def __init__(self,*args,**kwargs):
@ -13,9 +19,46 @@ class browserDoc(QTextBrowser):
self.setStyleSheet("QTextBrowser { " +style+ " }")
self.anchorClicked.connect(self.cAnchorClicked)
def cRenderHtml(self,html):
class cHtmlParser(HTMLParser):
def __init__(self):
HTMLParser.__init__(self)
self.output = ""
def handle_starttag(self,tag,attrs):
if tag == "img":
altText = False
for attr in attrs:
if attr[0] == "alt":
altText = html.escape(attr[1])
break
for attr in attrs:
if attr[0] == "src":
url = attr[1]
if not altText: altText = url.rsplit("/")[-1]
self.output += '<a href="' +html.escape(url)+ '">image: ' +html.escape(altText)+ '</a>'
return
return
self.output += "<" +tag
for attr in attrs:
self.output += ' ' +html.escape(attr[0])+ '="' +html.escape(attr[1])+ '"'
self.output += ">"
def handle_endtag(self,tag):
if tag in ["img"]: return
self.output += "</" +html.escape(tag)+ ">"
def handle_data(self,data):
self.output += data
def cRenderHtml(self,htm):
parser = self.cHtmlParser()
parser.feed(htm)
self.clear()
self.insertHtml(html.replace("<img","<dummy")) # half-assed slowdown fix
self.insertHtml(parser.output)
self.update()
def cAnchorClicked(self,url):
@ -24,8 +67,9 @@ class browserDoc(QTextBrowser):
curUrl = browserWindow.cDocumentInfo["url"]
curUrlParsed = parseUrl(curUrl)
urlParsed = parseUrl(url)
#print("navigating from: " +curUrl)
#print("to: " +url)
print("---")
print("navigating from: " +curUrl)
print("to: " +url)
if urlParsed["protocol"] == "": # is relative
if url[0] == "#":
curUrlParsed["anchor"] = url[1:]