-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.py
122 lines (97 loc) · 3.76 KB
/
app.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
import time
import chromedriver_autoinstaller
from selenium import webdriver
import threading
import tkinter as tk
from tkinter import messagebox, simpledialog
import os
class TabRotator:
def __init__(self):
self.urls = []
self.interval = 10 # seconds
self.driver = None
self.running = False
def start_browser(self):
chromedriver_autoinstaller.install()
self.driver = webdriver.Chrome()
self.driver.maximize_window()
if self.urls:
self.driver.get(self.urls[0])
time.sleep(1)
for url in self.urls[1:]:
self.driver.execute_script("window.open(arguments[0], '_blank');", url)
time.sleep(1)
def rotate_tabs(self):
while self.running:
for index in range(len(self.driver.window_handles)):
if not self.running:
break
self.driver.switch_to.window(self.driver.window_handles[index])
self.driver.refresh() # Refresh the current tab/page
time.sleep(self.interval)
def run(self):
self.running = True
self.start_browser()
self.rotate_tabs()
def stop(self):
self.running = False
if self.driver:
self.driver.quit()
self.driver = None # Clear driver reference
class App:
def __init__(self, master):
self.master = master
self.master.title("Tab Rotator")
self.tab_rotator = TabRotator()
self.thread = None
self.start_button = tk.Button(master, text="Start Rotating Tabs", command=self.start_rotation)
self.start_button.pack(pady=10)
self.stop_button = tk.Button(master, text="Stop", command=self.stop_rotation)
self.stop_button.pack(pady=10)
self.add_button = tk.Button(master, text="Add URL", command=self.add_url)
self.add_button.pack(pady=10)
self.remove_button = tk.Button(master, text="Remove URL", command=self.remove_url)
self.remove_button.pack(pady=10)
self.url_listbox = tk.Listbox(master, width=50)
self.url_listbox.pack(pady=10)
self.load_urls() # Load URLs on startup
self.master.protocol("WM_DELETE_WINDOW", self.on_close)
def start_rotation(self):
self.tab_rotator.urls = self.url_listbox.get(0, tk.END)
if not self.tab_rotator.urls:
messagebox.showwarning("No URLs", "Please add at least one URL.")
return
if self.thread is None or not self.thread.is_alive():
self.thread = threading.Thread(target=self.tab_rotator.run, daemon=True)
self.thread.start()
def stop_rotation(self):
self.tab_rotator.stop()
if self.thread:
self.thread.join() # Wait for the thread to finish
def add_url(self):
url = simpledialog.askstring("Add URL", "Enter URL:")
if url:
self.url_listbox.insert(tk.END, url)
def remove_url(self):
selected = self.url_listbox.curselection()
if selected:
self.url_listbox.delete(selected)
def on_close(self):
self.save_urls() # Save URLs before closing
self.stop_rotation() # Stop rotation before closing
if messagebox.askokcancel("Quit", "Do you really want to quit?"):
self.master.destroy()
def save_urls(self):
with open("urls.txt", "w") as f:
for url in self.url_listbox.get(0, tk.END):
f.write(url + "\n")
def load_urls(self):
if os.path.exists("urls.txt"):
with open("urls.txt", "r") as f:
for line in f:
self.url_listbox.insert(tk.END, line.strip())
# Create the main window
root = tk.Tk()
app = App(root)
# Run the application
root.mainloop()