Add anchor support

This commit is contained in:
Fierelier 2021-03-25 22:29:38 +01:00
parent 7730d2af3e
commit 4eb9358006
4 changed files with 98 additions and 35 deletions

View File

@ -108,7 +108,8 @@ class browserWindow(QMainWindow):
self.cNewWindowButton.triggered.connect(self.cNewWindow)
self.cViewMenu = self.cMenuBar.addMenu("&View")
self.cUrlBar = QLineEdit(config["default"]["home"],self)
self.cUrl,self.cAnchor = urlAnchorPairTo(config["default"]["home"])
self.cUrlBar = QLineEdit(urlAnchorPairFrom(self.cUrl,self.cAnchor),self)
self.cUrlBar.returnPressed.connect(self.cNavigate)
self.cButtonGo = QPushButton("Go",self)
self.cButtonGo.clicked.connect(self.cNavigate)
@ -120,7 +121,7 @@ class browserWindow(QMainWindow):
self.cResizeElements()
self.show()
self.cNavigate()
self.cNavigate(forceRefresh = True)
self.cUrlBar.setFocus()
def cResizeElements(self):
@ -155,33 +156,58 @@ class browserWindow(QMainWindow):
self.cTasks = []
self.cTaskLock.release()
def cNavigate(self,event = None):
def cSetUrl(self,url):
print("url set to: " +url)
self.cUrl = url
self.cUrlBar.setText(urlAnchorPairFrom(url,self.cAnchor))
def cSetAnchor(self,anchor):
print("anchor set to: " +str(anchor))
self.cAnchor = anchor
self.cUrlBar.setText(urlAnchorPairFrom(self.cUrl,anchor))
def cNavigate(self,event = None,forceRefresh = False):
try:
#print(prettyJson(parseUrl(self.cUrlBar.text())))
url,anchor = urlAnchorPairTo(self.cUrlBar.text())
self.cSetAnchor(anchor)
url = url.replace(" ","%20") # dirty
if url == self.cUrl and forceRefresh == False:
if anchor != False: self.cDoc.cScrollToAnchor(anchor)
return
self.cStatusBar.showMessage("Downloading...")
self.cStatusBar.repaint()
self.cTaskLock.acquire()
self.cDownloadId += 1
url = self.cUrlBar.text().replace(" ","%20") # dirty
urlParsed = parseUrl(url)
if urlParsed["protocol"] == "":
while len(url) > 0 and url[0] == "/":
url = url[1:]
url = config["default"]["defaultProtocol"]+ "://" +url
self.cUrlBar.setText(url)
self.cSetUrl(url)
threading.Thread(target=downloadPage, args=(self,self.cDownloadId,url,[("User-Agent",self.cUserAgent)])).start()
self.cTaskLock.release()
self.cDoc.setFocus()
#print(prettyJson(response["headers"]))
except Exception as e:
self.cDoc.cRenderHtml(html.escape(str(e)))
self.cDoc.cRenderHtml(str(e),"text")
raise
def cHandleResponse(self,response):
infoFetcher(response)
self.cUrlBar.setText(response["url"])
self.cDocumentInfo = response
if response["redirected"] == True:
print("redirected.")
url,anchor = urlAnchorPairTo(response["url"])
self.cSetUrl(url)
self.cSetAnchor(anchor)
self.cDocumentInfo = response
self.cStatusBar.showMessage("Rendering...")
self.cStatusBar.repaint()
start = time.time()

View File

@ -72,27 +72,11 @@ class browserDoc(QTextBrowser):
url = str(url.toEncoded(),"utf-8")
browserWindow = self.parent()
curUrl = browserWindow.cDocumentInfo["url"]
curUrlParsed = parseUrl(curUrl)
urlParsed = parseUrl(url)
print("---")
print("navigating from: " +curUrl)
print("to: " +url)
if urlParsed["protocol"] == "": # is relative
if url[0] == "#":
curUrlParsed["anchor"] = url[1:]
url = unparseUrl(curUrlParsed)
elif url[:2] == "//":
url = curUrlParsed["protocol"] + ":" + url
elif url[0] == "/":
urlParsed["protocol"] = curUrlParsed["protocol"]
urlParsed["domain"] = curUrlParsed["domain"]
urlParsed["path"] = urlParsed["path"][1:]
url = unparseUrl(urlParsed)
else:
urlParsed["protocol"] = curUrlParsed["protocol"]
urlParsed["domain"] = curUrlParsed["domain"]
while len(curUrlParsed["path"]) > 0 and curUrlParsed["path"][-1] == "/": curUrlParsed["path"] = curUrlParsed["path"][:-1]
urlParsed["path"] = curUrlParsed["path"] + "/" + url
url = unparseUrl(urlParsed)
navType, url, urlParsed = navigateLink(curUrl,url)
refresh = True
if navType == 0: refresh = False
browserWindow.cUrlBar.setText(url)
browserWindow.cNavigate()
browserWindow.cNavigate(forceRefresh = refresh)
def cScrollToAnchor(self,anchor):
self.scrollToAnchor(anchor)

View File

@ -9,12 +9,14 @@ def downloadPage(window,downloadId,url,headers = False):
response = {
"url": url,
"body": "",
"headers": []
"headers": [],
"redirected": False
}
class rdrh(urllib.request.HTTPRedirectHandler):
def redirect_request(self,req,fp,code,msg,hdrs,newurl):
response["url"] = newurl
response["redirected"] = True
try:
return urllib.request.HTTPRedirectHandler.redirect_request(self, req, fp, code, msg, hdrs, newurl)
except urllib.error.HTTPError as e:

View File

@ -23,7 +23,7 @@ def unparseUrl(parsedUrl):
if parameter[1] != None:
url += "=" + parameter[1]
if parsedUrl["anchor"] != "":
if parsedUrl["anchor"] != False:
url += "#" + parsedUrl["anchor"]
return url
@ -35,7 +35,7 @@ def parseUrl(url):
"domain": "",
"path": "",
"parameters": [],
"anchor": ""
"anchor": False
}
# get anchor
@ -102,6 +102,21 @@ def urlJoin(*args):
return outUrl
global urlAnchorPairTo
def urlAnchorPairTo(url):
urlSplit = url.split("#",1)
url = urlSplit[0]
anchor = False
if len(urlSplit) > 1:
anchor = urlSplit[1]
return url,anchor
global urlAnchorPairFrom
def urlAnchorPairFrom(url,anchor):
if anchor != False:
url += "#" +anchor
return url
global getContentType
def getContentType(headers,fallback):
contentType = fallback
@ -133,6 +148,42 @@ def getContentType(headers,fallback):
return contentType, contentTypeArguments
global navigateLink
def navigateLink(curUrl,url):
urlParsed = parseUrl(url)
curUrlParsed = parseUrl(curUrl)
navType = -1
print("---")
print("navigating from: " +curUrl)
print("to: " +url)
if urlParsed["protocol"] == "": # is relative
if url[0] == "#": # scroll to anchor
if len(url) < 2:
curUrlParsed["anchor"] = ""
else:
curUrlParsed["anchor"] = url[1:]
url = unparseUrl(curUrlParsed)
navType = 0
elif url[:2] == "//": # navigate to another domain with the same protocol
url = curUrlParsed["protocol"] + ":" + url
urlParsed = parseUrl(url)
navType = 1
elif url[0] == "/": # navigate to another path, relative to the current domain
urlParsed["protocol"] = curUrlParsed["protocol"]
urlParsed["domain"] = curUrlParsed["domain"]
urlParsed["path"] = urlParsed["path"][1:]
url = unparseUrl(urlParsed)
navType = 2
else: # navigate to another path, relative to the current path
urlParsed["protocol"] = curUrlParsed["protocol"]
urlParsed["domain"] = curUrlParsed["domain"]
while len(curUrlParsed["path"]) > 0 and curUrlParsed["path"][-1] == "/": curUrlParsed["path"] = curUrlParsed["path"][:-1]
urlParsed["path"] = curUrlParsed["path"] + "/" + url
url = unparseUrl(urlParsed)
navType = 3
return navType, url, urlParsed
global infoFetcher
def infoFetcher(info):
''' if "Content-Base" in info["headers"]: