This repository has been archived by the owner on May 28, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 56
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Showing
11 changed files
with
212 additions
and
116 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
# -*- coding: utf-8 -*- | ||
|
||
# Form implementation generated from reading ui file 'designer/loginDialog.ui' | ||
# | ||
# Created by: PyQt5 UI code generator 5.12.1 | ||
# | ||
# WARNING! All changes made in this file will be lost! | ||
|
||
from PyQt5 import QtCore, QtGui, QtWidgets | ||
|
||
|
||
class Ui_LoginDialog(object): | ||
def setupUi(self, LoginDialog): | ||
LoginDialog.setObjectName("LoginDialog") | ||
LoginDialog.resize(505, 480) | ||
self.gridLayout = QtWidgets.QGridLayout(LoginDialog) | ||
self.gridLayout.setObjectName("gridLayout") | ||
self.reloadBtn = QtWidgets.QPushButton(LoginDialog) | ||
self.reloadBtn.setObjectName("reloadBtn") | ||
self.gridLayout.addWidget(self.reloadBtn, 0, 1, 1, 1) | ||
self.pageContainer = QtWidgets.QVBoxLayout() | ||
self.pageContainer.setObjectName("pageContainer") | ||
self.gridLayout.addLayout(self.pageContainer, 1, 0, 1, 2) | ||
self.address = QtWidgets.QLineEdit(LoginDialog) | ||
self.address.setClearButtonEnabled(True) | ||
self.address.setObjectName("address") | ||
self.gridLayout.addWidget(self.address, 0, 0, 1, 1) | ||
|
||
self.retranslateUi(LoginDialog) | ||
QtCore.QMetaObject.connectSlotsByName(LoginDialog) | ||
|
||
def retranslateUi(self, LoginDialog): | ||
_translate = QtCore.QCoreApplication.translate | ||
LoginDialog.setWindowTitle(_translate("LoginDialog", "Login")) | ||
self.reloadBtn.setText(_translate("LoginDialog", "reload")) | ||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
<?xml version="1.0" encoding="UTF-8"?> | ||
<ui version="4.0"> | ||
<class>LoginDialog</class> | ||
<widget class="QDialog" name="LoginDialog"> | ||
<property name="geometry"> | ||
<rect> | ||
<x>0</x> | ||
<y>0</y> | ||
<width>505</width> | ||
<height>480</height> | ||
</rect> | ||
</property> | ||
<property name="windowTitle"> | ||
<string>Login</string> | ||
</property> | ||
<layout class="QGridLayout" name="gridLayout" rowstretch="0,0"> | ||
<item row="0" column="1"> | ||
<widget class="QPushButton" name="reloadBtn"> | ||
<property name="text"> | ||
<string>reload</string> | ||
</property> | ||
</widget> | ||
</item> | ||
<item row="1" column="0" colspan="2"> | ||
<layout class="QVBoxLayout" name="pageContainer"/> | ||
</item> | ||
<item row="0" column="0"> | ||
<widget class="QLineEdit" name="address"> | ||
<property name="clearButtonEnabled"> | ||
<bool>true</bool> | ||
</property> | ||
</widget> | ||
</item> | ||
</layout> | ||
</widget> | ||
<resources/> | ||
<connections/> | ||
</ui> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,73 @@ | ||
import json | ||
import sys | ||
import logging | ||
from PyQt5.QtCore import QUrl, pyqtSignal | ||
from .UIForm import loginDialog | ||
from PyQt5.QtWidgets import QDialog | ||
from PyQt5.QtWebEngineWidgets import QWebEngineView, QWebEngineProfile | ||
|
||
logger = logging.getLogger('dict2Anki') | ||
|
||
|
||
class LoginDialog(QDialog, loginDialog.Ui_LoginDialog): | ||
loginSucceed = pyqtSignal(str) | ||
|
||
def __init__(self, loginUrl, loginCheckCallbackFn, parent=None): | ||
super().__init__(parent) | ||
self.url = QUrl(loginUrl) | ||
self.loginCheckCallbackFn = loginCheckCallbackFn | ||
self.setupUi(self) | ||
self.page = LoginWebEngineView(self) | ||
self.pageContainer.addWidget(self.page) | ||
self.page.load(self.url) | ||
self.makeConnection() | ||
|
||
def makeConnection(self): | ||
self.reloadBtn.clicked.connect(self._reload) | ||
self.page.loadFinished.connect(self.checkLoginState) | ||
|
||
def _reload(self): | ||
logger.debug('Reload page') | ||
self.page.cookieStore.deleteAllCookies() | ||
self.page.load(QUrl(self.address.text())) | ||
|
||
def checkLoginState(self): | ||
def contentLoaded(content): | ||
logger.debug(f'Cookie:{self.page.cookie}') | ||
logger.debug(f'Content{content}') | ||
if self.loginCheckCallbackFn(cookie=self.page.cookie, content=content): | ||
logger.info(f'Login Success!') | ||
self.onLoginSucceed() | ||
logger.info(f'Login Fail!') | ||
|
||
self.page.page().toHtml(contentLoaded) | ||
|
||
def onLoginSucceed(self): | ||
logger.info('Destruct login dialog') | ||
self.close() | ||
logger.debug('emit cookie') | ||
self.loginSucceed.emit(json.dumps(self.page.cookie)) | ||
|
||
|
||
class LoginWebEngineView(QWebEngineView): | ||
def __init__(self, *args, **kwargs): | ||
super().__init__(*args, **kwargs) | ||
# 绑定cookie被添加的信号槽 | ||
self.profile = QWebEngineProfile.defaultProfile() | ||
self.profile.setHttpUserAgent( | ||
'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_13_6) AppleWebKit/537.36 (KHTML, like Gecko)' | ||
' Chrome/69.0.3497.100 Safari/537.36' | ||
) | ||
self.cookieStore = self.profile.cookieStore() | ||
self.cookieStore.cookieAdded.connect(self.onCookieAdd) | ||
self._cookies = {} | ||
self.show() | ||
|
||
def onCookieAdd(self, cookie): | ||
name = cookie.name().data().decode('utf-8') | ||
value = cookie.value().data().decode('utf-8') | ||
self._cookies[name] = value | ||
|
||
@property | ||
def cookie(self) -> dict: | ||
return self._cookies |
Oops, something went wrong.