|
import sys |
|
|
|
from PyQt5 import QtNetwork |
|
from PyQt5.QtCore import Qt, QUrl |
|
from PyQt5.QtNetwork import QNetworkCookie |
|
from PyQt5.QtWebEngineCore import QWebEngineUrlRequestInterceptor |
|
from PyQt5.QtWebEngineWidgets import QWebEnginePage, QWebEngineProfile, QWebEngineView |
|
from PyQt5.QtWidgets import QApplication, QVBoxLayout, QWidget |
|
|
|
app = QApplication([]) |
|
|
|
# And a window |
|
main_window = QWidget() |
|
main_window.setWindowTitle('Overleaf Cookie fetcher') |
|
|
|
# And give it a layout |
|
layout = QVBoxLayout() |
|
main_window.setLayout(layout) |
|
|
|
# Create and fill a QWebView |
|
web_view = QWebEngineView() |
|
|
|
cookies = [] |
|
|
|
|
|
def on_cookie_added(cookie): |
|
for c in cookies: |
|
if c.hasSameIdentifier(cookie): |
|
return |
|
cookies.append(QNetworkCookie(cookie)) |
|
|
|
|
|
def to_json(cooked): |
|
cookies_list_info = [] |
|
for c in cooked: |
|
data = {"name": bytearray(c.name()).decode(), "domain": c.domain(), |
|
"value": bytearray(c.value()).decode(), |
|
"path": c.path(), "expirationDate": c.expirationDate().toString(Qt.ISODate), |
|
"secure": c.isSecure(), |
|
"httponly": c.isHttpOnly()} |
|
cookies_list_info.append(data) |
|
return cookies_list_info |
|
|
|
|
|
profile = QWebEngineProfile("storage", web_view) |
|
|
|
# Don't persist cookies on machine |
|
network_manager = QtNetwork.QNetworkAccessManager() |
|
cookie_jar = QtNetwork.QNetworkCookieJar() |
|
network_manager.setCookieJar(cookie_jar) |
|
profile.setPersistentCookiesPolicy(QWebEngineProfile.NoPersistentCookies) |
|
|
|
|
|
class WebEngineUrlRequestInterceptor(QWebEngineUrlRequestInterceptor): |
|
def interceptRequest(self, info): |
|
# check if the user has been redirected to project page, it means that user is now logged in. |
|
if info.requestUrl().url() == 'https://www.overleaf.com/project': |
|
print(to_json(cookies)) |
|
sys.exit(app.exec()) |
|
|
|
|
|
interceptor = WebEngineUrlRequestInterceptor() |
|
|
|
profile.setUrlRequestInterceptor(interceptor) |
|
cookie_store = profile.cookieStore() |
|
cookie_store.cookieAdded.connect(on_cookie_added) |
|
|
|
webpage = QWebEnginePage(profile, web_view) |
|
webpage.load(QUrl('https://www.overleaf.com/login')) |
|
web_view.setPage(webpage) |
|
|
|
# Add the QWebView and button to the layout |
|
layout.addWidget(web_view) |
|
|
|
if __name__ == "__main__": |
|
main_window.resize(600, 900) |
|
main_window.show() |
|
sys.exit(app.exec()) |