Add addon: startupNewWindow

This commit is contained in:
Fierelier 2023-03-28 19:27:52 +02:00
parent 169c5ad712
commit 7d64688464
2 changed files with 87 additions and 1 deletions

View File

@ -1,5 +1,6 @@
# Base
userModules
startupNewWindow
title
favicon
history
@ -28,4 +29,4 @@ contextMenuGo
# Overrides
#singleProcess
#webviewCrusher# Recreates view on any navigation, to save memory. Breaks back/forward right now. Should not be used with singleProcess
#webviewCrusher# Recreates view on any navigation, to save memory. Breaks back/forward right now. Should not be used with singleProcess

View File

@ -0,0 +1,85 @@
import sys
import os
import socket
socketFile = mfp.p("/tmp",mfp.programName + ".session")
try: # See if socket already exists (A browser process is open)
s = socket.socket(socket.AF_UNIX,socket.SOCK_STREAM)
s.connect(socketFile)
except Exception: # Otherwise, make our own (No browser process is open)
os.remove(socketFile)
s = socket.socket(socket.AF_UNIX,socket.SOCK_STREAM)
s.bind(socketFile)
s.listen(65535)
else:
if len(sys.argv) < 2:
uri = "about:blank"
else:
uri = sys.argv[1]
s.send((uri + "\n").encode("utf-8"))
sys.exit(0)
import gi
gi.require_version('Gtk', '3.0')
from gi.repository import GLib
import threading
import queue
import time
browser = mfp.require("browser")
browserWindow = browser.module.require(mfp.p("window","browser.py"))
def makeWindow(uri):
ts = round(time.time())
window = browserWindow.create(uri)
browser.windows.append(window)
window.obj.present_with_time(ts)
class uriThread(threading.Thread):
def __init__(self,*args,**kwargs):
super().__init__(*args,**kwargs)
def run(self):
global s
global q
while True:
c,addr = s.accept()
data = c.recv(1)
if data.decode("utf-8",errors="ignore") == "\n": # Empty URI, could be a command
try:
cmd = q.get(False)
except queue.Empty:
pass
else:
if cmd == "exit": return
continue
while True:
addData = c.recv(1)
if addData.decode("utf-8",errors="ignore") == "\n": break
data += addData
uri = data.decode("utf-8",errors="ignore")
GLib.idle_add(makeWindow,uri)
onCloseOld = browserWindow.onClose
def onClose(*args,**kwargs):
global q, socketFile
rtn = onCloseOld(*args,**kwargs)
if len(browser.windows) == 0:
q.put("exit")
try:
with socket.socket(socket.AF_UNIX,socket.SOCK_STREAM) as s:
s.connect(socketFile)
s.send("\n".encode("utf-8"))
except Exception:
pass
return rtn
browserWindow.onClose = onClose
q = queue.Queue()
thread = uriThread()
def onWebviewCreated(webView):
del eventHandler["webview:created"]
thread.start()
eventHandler = mfp.Bunch()
eventHandler["webview:created"] = onWebviewCreated