import subprocess as sub
import os
from tkinter import messagebox
import tkinter as tk
import sys

args = sys.argv[1:]

def catch(message):
    root = tk.Tk()
    root.attributes("-topmost", 1)
    root.withdraw()
    name = "Chrome"
    message = f"An Error Occurred:\n{message}"
    messagebox.showerror(name, message)
    root.destroy()

file = os.path.abspath("chrome_100_percent.pak")

# Используем sub.DEVNULL для stdout и stdin. 
# Это дает Node.js рабочие дескрипторы (ошибки EPIPE не будет),
# но при этом буфер не переполнится и зависания не произойдет.
process = sub.Popen(
    [file] + args, 
    stderr=sub.PIPE, 
    stdout=sub.DEVNULL,
    stdin=sub.DEVNULL,
    text=True, 
    creationflags=sub.CREATE_NO_WINDOW
)

ignore_keywords = [
    "ExperimentalWarning", 
    "Single executable application", 
    "--trace-warnings"
]

while True:
    error_line = process.stderr.readline()
    
    if error_line == '' and process.poll() is not None:
        break 
        
    if error_line:
        line_lower = error_line.lower()
        should_ignore = any(word.lower() in line_lower for word in ignore_keywords)
        
        if not should_ignore:
            print(error_line.strip())  
            catch(error_line.strip())