好友
阅读权限20
听众
最后登录1970-1-1
|
import sys
import sqlite3
from PyQt5.QtWidgets import QApplication, QWidget, QVBoxLayout, QLabel, QLineEdit, QPushButton, QMessageBox, \
QTableWidget, QTableWidgetItem, QHBoxLayout, QInputDialog, QMenu, QAction
from PyQt5.QtGui import QClipboard
from PyQt5.QtCore import Qt
from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from selenium.common.exceptions import NoSuchWindowException
class EdgePasswordBox(QWidget):
def __init__(self):
super().__init__()
self.initUI()
self.driver = None
self.accounts = []
self.db_connection = sqlite3.connect('edge_passwords.db')
self.create_table()
self.load_accounts()
self.login_in_progress = False
self.max_login_attempts = 3
def initUI(self):
self.setWindowTitle(' 密码箱')
self.setGeometry(100, 100, 500, 400)
self.setWindowFlags(Qt.WindowStaysOnTopHint)
main_layout = QVBoxLayout()
self.account_table = QTableWidget()
self.account_table.setColumnCount(3)
self.account_table.setHorizontalHeaderLabels([' 用户名', '密码', '网址'])
self.account_table.setShowGrid(True)
self.account_table.setGridStyle(Qt.SolidLine)
self.account_table.setStyleSheet("gridline - color: black;")
self.account_table.setContextMenuPolicy(3)
self.account_table.customContextMenuRequested.connect(self.show_context_menu)
self.account_table.cellDoubleClicked.connect(self.double_click_login)
self.account_table.setEditTriggers(QTableWidget.NoEditTriggers)
main_layout.addWidget(self.account_table)
add_account_layout = self.create_add_account_layout()
main_layout.addLayout(add_account_layout)
self.setLayout(main_layout)
def create_add_account_layout(self):
add_account_layout = QHBoxLayout()
self.username_label = QLabel('用户名:')
add_account_layout.addWidget(self.username_label)
self.username_input = QLineEdit()
add_account_layout.addWidget(self.username_input)
self.password_label = QLabel('密码:')
add_account_layout.addWidget(self.password_label)
self.password_input = QLineEdit()
self.password_input.setEchoMode(QLineEdit.Password)
add_account_layout.addWidget(self.password_input)
self.url_label = QLabel('网页URL:')
add_account_layout.addWidget(self.url_label)
self.url_input = QLineEdit()
add_account_layout.addWidget(self.url_input)
self.add_button = QPushButton('添加账号')
self.add_button.clicked.connect(self.add_account)
add_account_layout.addWidget(self.add_button)
return add_account_layout
def create_table(self):
cursor = self.db_connection.cursor()
cursor.execute('''
CREATE TABLE IF NOT EXISTS accounts (
id INTEGER PRIMARY KEY AUTOINCREMENT,
username TEXT NOT NULL,
password TEXT NOT NULL,
url TEXT NOT NULL
)
''')
self.db_connection.commit()
def load_accounts(self):
cursor = self.db_connection.cursor()
cursor.execute('SELECT username, password, url FROM accounts')
rows = cursor.fetchall()
self.account_table.setRowCount(len(rows))
for row_index, row in enumerate(rows):
for column_index, value in enumerate(row):
item = QTableWidgetItem(str(value))
self.account_table.setItem(row_index, column_index, item)
self.accounts.append(row)
def add_account(self):
username = self.username_input.text()
password = self.password_input.text()
url = self.url_input.text()
if not username or not password or not url:
QMessageBox.warning(self, "输入错误", "请填写完整的用户名、密码和URL。")
return
cursor = self.db_connection.cursor()
cursor.execute('INSERT INTO accounts (username, password, url) VALUES (?,?,?)', (username, password, url))
self.db_connection.commit()
self.load_accounts()
self.username_input.clear()
self.password_input.clear()
self.url_input.clear()
def show_context_menu(self, pos):
context_menu = QMenu(self)
edit_action = QAction("编辑", self)
edit_action.triggered.connect(lambda: self.edit_account(self.account_table.currentRow()))
delete_action = QAction("删除", self)
delete_action.triggered.connect(lambda: self.delete_account(self.account_table.currentRow()))
login_action = QAction("登录", self)
login_action.triggered.connect(lambda: self.login_from_context_menu(self.account_table.currentRow()))
copy_username_action = QAction("复制用户名", self)
copy_username_action.triggered.connect(lambda: self.copy_username(self.account_table.currentRow()))
copy_password_action = QAction("复制密码", self)
copy_password_action.triggered.connect(lambda: self.copy_password(self.account_table.currentRow()))
copy_url_action = QAction("复制网址", self)
copy_url_action.triggered.connect(lambda: self.copy_url(self.account_table.currentRow()))
show_password_action = QAction("显示密码", self)
show_password_action.triggered.connect(lambda: self.show_password(self.account_table.currentRow()))
context_menu.addAction(edit_action)
context_menu.addAction(delete_action)
context_menu.addAction(login_action)
context_menu.addAction(copy_username_action)
context_menu.addAction(copy_password_action)
context_menu.addAction(copy_url_action)
context_menu.addAction(show_password_action)
context_menu.exec_(self.account_table.mapToGlobal(pos))
def edit_account(self, row_index):
if row_index >= 0:
# 假设这里可以获取到账号的ID,需要在数据库查询和表格显示中进行相应调整
account_id = self.accounts[row_index][3]
username = self.account_table.item(row_index, 0).text()
password = self.account_table.item(row_index, 1).text()
url = self.account_table.item(row_index, 2).text()
new_username, ok = QInputDialog.getText(self, "编辑账号", "请输入新的用户名:", text=username)
if ok:
new_password, ok = QInputDialog.getText(self, "编辑账号", "请输入新的密码:", text=password,
echo=QLineEdit.Password)
if ok:
new_url, ok = QInputDialog.getText(self, "编辑账号", "请输入新的网址:", text=url)
if ok:
cursor = self.db_connection.cursor()
cursor.execute('UPDATE accounts SET username =?, password =?, url =? WHERE id =?',
(new_username, new_password, new_url, account_id))
self.db_connection.commit()
self.load_accounts()
def delete_account(self, row_index):
if row_index >= 0:
# 假设这里可以获取到账号的ID,需要在数据库查询和表格显示中进行相应调整
account_id = self.accounts[row_index][3]
cursor = self.db_connection.cursor()
cursor.execute('DELETE FROM accounts WHERE id =?', (account_id,))
self.db_connection.commit()
self.load_accounts()
def fill_login_form(self):
if not self.accounts:
QMessageBox.warning(self, "错误", "请先添加账号信息。")
return
selected_index = self.account_table.currentRow()
if selected_index >= 0:
self.login(selected_index)
def double_click_login(self, row_index, column_index):
if row_index >= 0:
self.login(row_index)
def login_from_context_menu(self, row_index):
if row_index >= 0:
self.login(row_index)
def login(self, index):
if self.login_in_progress:
return
self.login_in_progress = True
username = self.accounts[index][0]
password = self.accounts[index][1]
url = self.accounts[index][2]
login_attempts = 0
success = False
while login_attempts < self.max_login_attempts and not success:
try:
if self.driver is None:
self.driver = webdriver.Edge()
self.driver.execute_script("window.open('about:blank', '_blank');")
self.driver.switch_to.window(self.driver.window_handles[-1])
self.driver.get(url)
username_input = self.driver.find_element(By.XPATH, "//input[@name='username']")
username_input.send_keys(username)
password_input = self.driver.find_element(By.XPATH, "//input[@name='password']")
password_input.send_keys(password)
login_button = self.driver.find_element(By.XPATH, "//button[contains(@class, \"login - button\")]")
login_button.click()
success = True
except Exception as e:
print(f"登录时发生异常: {type(e).__name__} - {str(e)}")
if isinstance(e, NoSuchWindowException):
if self.driver:
self.driver.quit()
self.driver = None
login_attempts += 1
if login_attempts == self.max_login_attempts:
QMessageBox.warning(self, "登录错误", "登录失败,请检查网络或网页。")
self.login_in_progress = False
def copy_username(self, row_index):
if row_index >= 0:
username = self.account_table.item(row_index, 0).text()
clipboard = QApplication.clipboard()
clipboard.setText(username)
def copy_password(self, row_index):
if row_index >= 0:
password = self.account_table.item(row_index, 1).text()
clipboard = QApplication.clipboard()
clipboard.setText(password)
def copy_url(self, row_index):
if row_index >= 0:
url = self.account_table.item(row_index, 2).text()
clipboard = QApplication.clipboard()
clipboard.setText(url)
def show_password(self, row_index):
if row_index >= 0:
password = self.account_table.item(row_index, 1).text()
QMessageBox.information(self, "密码显示", f"密码为: {password}")
if __name__ == "__main__":
app = QApplication(sys.argv)
window = EdgePasswordBox()
window.show()
app.exec_()
|
|