Add existing scripts.

This commit is contained in:
2021-10-12 21:58:56 -04:00
parent 38d50f38ab
commit ba8dfef3fc
4 changed files with 180 additions and 2 deletions

48
blocker.py Normal file
View File

@@ -0,0 +1,48 @@
#!/usr/bin/env python3
import os
import psutil
import re
import subprocess
import sys
import time
XDOTOOL_CMD = ["xdotool", "getactivewindow", "getwindowname", "getwindowpid"]
BLOCKED_BROWSER_WORDS = []
def is_window_blocked(window_name, blocked):
for b in blocked:
if type(b) is str and b == window_name:
return True
elif type(b) is re.Pattern and b.findall(window_name):
return True
return False
def main():
blocked = [re.compile(f"{word}.*(Firefox|Chromium)", flags=re.IGNORECASE)
for word in BLOCKED_BROWSER_WORDS]
while True:
time.sleep(1)
p = subprocess.run(XDOTOOL_CMD, capture_output=True)
if p.returncode != 0:
continue
window_name, window_pid, _ = p.stdout.decode().split("\n")
if is_window_blocked(window_name, blocked):
p = psutil.Process(int(window_pid))
p.kill()
print(f"Kill {window_name}")
if __name__ == "__main__":
if os.geteuid() == 0:
newpid = os.fork()
if newpid == 0:
main()
else:
cmd = ["sudo"] + sys.argv
subprocess.Popen(cmd, start_new_session=True)