webkit-disappointment/webkit-inabox-qt5.py

185 lines
5.8 KiB
Python
Raw Normal View History

2021-12-19 03:59:42 +00:00
#!/usr/bin/env python3
"""
This uses the abandoned QWebKit, which was abandoned by the soydevs making Qt5. I made this before I realized it was fucked.
2021-12-20 05:39:23 +00:00
Not as fat as the GTK version of "my" browser, but outdated. Needs some old Qt5 idk. Use this if you are restricted in RAM and don't need the newest of newest features to access your pages, but do note that this will eventually stop working.
2021-12-19 03:59:42 +00:00
Prerequisites (Debian and cousins):
sudo apt install python3-pyqt5 python3-qtpy
2021-12-20 05:41:56 +00:00
Below "# Script start" you can find a setting for software mode, which you should comment out if you use a modern PC. I don't use the software rendering that comes included with WebKit, as it's unstable in my experience.
2021-12-19 03:59:42 +00:00
"""
import sys
oldexcepthook = sys.excepthook
def newexcepthook(type,value,traceback):
oldexcepthook(type,value,traceback)
#input("Press ENTER to quit.") # - this causes issues with Qt's input hook
sys.excepthook = newexcepthook
import os
p = os.path.join
pUp = os.path.dirname
s = False
if getattr(sys, 'frozen', False) and hasattr(sys, '_MEIPASS'):
s = os.path.realpath(sys.executable)
else:
s = os.path.realpath(__file__)
sp = pUp(s)
# Script start
2021-12-20 05:41:56 +00:00
# Comment the following line for hardware acceleration
os.environ["LIBGL_ALWAYS_SOFTWARE"] = "1"
2021-12-19 03:59:42 +00:00
import qtpy
import qtpy.QtGui as QtGui
from qtpy.QtGui import *
from qtpy.QtWidgets import *
from qtpy.QtCore import *
from PyQt5.QtWebKitWidgets import *
2021-12-20 05:41:56 +00:00
from PyQt5.QtWebKit import *
2021-12-19 03:59:42 +00:00
defaultProtocol = "https"
browserWindows = []
def parseUrl(url):
if url.startswith("data:"): return "data",[],"",""
if url.startswith("blob:"): return "blob",[],"",""
urlSplit = url.split(":",1)
protocol = urlSplit[0]
urlSplit = urlSplit[1].lstrip("/").split("/",1)
if len(urlSplit) < 2: urlSplit.append("")
domain = urlSplit[0].split(".")
urlSplit = urlSplit[1].split("?",1)
if len(urlSplit) < 2: urlSplit.append("")
path = urlSplit[0]
arguments = urlSplit[1]
return protocol,domain,path,arguments
class browserPage(QWebPage):
def __init__(self,*args,**kwargs):
super().__init__(*args,**kwargs)
2021-12-20 05:39:23 +00:00
self.defaultUserAgent = ""
ua = super().userAgentForUrl(QUrl("https://example.com")).split(" ")
skip = False
for word in ua:
if skip == True:
skip = False
continue
if word == "Gecko)": skip = True
self.defaultUserAgent += " " + word
self.defaultUserAgent = self.defaultUserAgent[1:]
#self.defaultUserAgent = "Opera/9.80 (J2ME/MIDP; Opera Mini/4.0.10992/35.5561; U; hr) Presto/2.8.119 Version/11.10"
#self.settings().setAttribute(QWebSettings.AutoLoadImages,False)
self.settings().setMaximumPagesInCache(0)
self.settings().setAttribute(QWebSettings.DnsPrefetchEnabled,False)
# idk if these work, folders need to exist
#self.settings().setAttribute(QWebSettings.LocalStorageEnabled,True)
#self.settings().setLocalStoragePath(p(sp,"LocalStorage"))
#self.settings().setAttribute(QWebSettings.OfflineStorageDatabaseEnabled,True)
#self.settings().setOfflineStorageDefaultQuota(500000000)
#self.settings().setOfflineStoragePath(p(sp,"StorageOffline"))
#self.settings().setAttribute(QWebSettings.OfflineWebApplicationCacheEnabled,True)
#self.settings().setOfflineWebApplicationCacheQuota(500000000)
#self.settings().setOfflineWebApplicationCachePath(p(sp,"AppStorageOffline"))
2021-12-19 03:59:42 +00:00
def userAgentForUrl(self,url):
protocol,domain,path,arguments = parseUrl(url.toString())
2021-12-20 05:39:23 +00:00
if len(domain) < 2: return self.defaultUserAgent
2021-12-19 03:59:42 +00:00
mainDomain = (domain[-2] + "." + domain[-1]).lower()
if domain[-2].lower() == "google": return ""
2021-12-20 05:39:23 +00:00
return self.defaultUserAgent
2021-12-19 03:59:42 +00:00
class browserView(QWebView):
def __init__(self,*args,**kwargs):
super().__init__(*args,**kwargs)
self.window = args[0]
2021-12-20 05:39:23 +00:00
self.cPage = browserPage()
self.setPage(self.cPage)
2021-12-19 03:59:42 +00:00
self.load(QUrl("about:blank"))
self.titleChanged.connect(self.cTitleChanged)
self.urlChanged.connect(self.cUrlChanged)
def cTitleChanged(self):
2021-12-20 05:39:23 +00:00
self.window.setWindowTitle(self.title()+ " - " +self.window.cTitle)
2021-12-19 03:59:42 +00:00
def cUrlChanged(self,url):
self.window.cUrlBar.setText(url.toString())
self.window.cUrlBar.setCursorPosition(0)
2021-12-20 05:39:23 +00:00
self.cPage.settings().clearMemoryCaches()
2021-12-19 03:59:42 +00:00
class browserWindow(QMainWindow):
def __init__(self,*args,**kwargs):
super().__init__(*args,**kwargs)
2021-12-20 05:39:23 +00:00
self.cTitle = "webkit-inabox-qt5"
2021-12-19 03:59:42 +00:00
self.setWindowTitle(self.cTitle)
self.cWidth = 640
self.cHeight = 480
self.resize(self.cWidth,self.cHeight)
2021-12-20 05:39:23 +00:00
self.cTaskTimer = QTimer()
self.cTaskTimer.setInterval(10000)
self.cTaskTimer.timeout.connect(self.cRunTasks)
self.cTaskTimer.start()
2021-12-19 03:59:42 +00:00
self.cCreateElements()
def cCreateElements(self):
self.cMenuBar = self.menuBar()
self.cFileMenu = self.cMenuBar.addMenu("&File")
self.cNewWindowButton = self.cFileMenu.addAction("New &window")
self.cNewWindowButton.triggered.connect(self.cNewWindow)
self.cUrlBar = QLineEdit("about:blank",self)
self.cUrlBar.returnPressed.connect(self.cNavigate)
self.cBrowserView = browserView(self)
self.cResizeElements()
self.show()
def cResizeElements(self):
barSize = 22
mb = self.cMenuBar.height()
self.cUrlBar.move(0,mb)
self.cUrlBar.resize(self.cWidth,barSize)
self.cBrowserView.move(0,mb + barSize)
self.cBrowserView.resize(self.cWidth,self.cHeight - mb - barSize)
def resizeEvent(self,event):
self.cWidth = self.width()
self.cHeight = self.height()
self.cResizeElements()
def closeEvent(self,event):
browserWindows.remove(self)
def cNewWindow(self):
browserWindows.append(browserWindow())
def cNavigate(self):
self.cUrlBar.setCursorPosition(0)
url = self.cUrlBar.text()
if len(url.split(":",1)) < 2:
url = defaultProtocol + "://" + url
self.cBrowserView.setFocus()
self.cBrowserView.load(QUrl(url))
2021-12-20 05:39:23 +00:00
def cRunTasks(self):
self.cBrowserView.cPage.settings().clearMemoryCaches()
2021-12-19 03:59:42 +00:00
app = QApplication(sys.argv)
browserWindows.append(browserWindow())
app.exec_()