Last active
June 2, 2024 17:52
-
-
Save NguyenKhong/a0502eff97d1e1607a374e2fe89d3d9c to your computer and use it in GitHub Desktop.
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
import sys | |
from PyQt5.QtCore import QUrl, QSize | |
from PyQt5.QtWebEngineWidgets import QWebEngineView, QWebEngineScript, QWebEnginePage, QWebEngineProfile | |
from PyQt5.QtWidgets import QToolBar, QAction, QLineEdit, QLabel, QMainWindow, QTabWidget, QApplication | |
from PyQt5.QtNetwork import QNetworkCookie | |
import http.cookiejar | |
def create_script(name, src, injection_point = QWebEngineScript.DocumentCreation, world = QWebEngineScript.MainWorld, on_subframes = True): | |
script = QWebEngineScript() | |
script.setSourceCode(src) | |
script.setName(name) | |
script.setWorldId(world) | |
script.setInjectionPoint(injection_point) | |
script.setRunsOnSubFrames(on_subframes) | |
return script | |
class WebProfile(QWebEngineProfile): | |
def __init__(self, *args, **kwargs): | |
super(WebProfile, self).__init__(*args, **kwargs) | |
self.setPersistentCookiesPolicy(0) | |
self.setHttpCacheMaximumSize(100 << 20) | |
self.setHttpUserAgent("Mozilla/5.0 (Windows NT 6.1; Win64; x64; rv:77.0) Gecko/20100101 Firefox/77.0") | |
def cookieFromString(self, raw, domain): | |
cookies = {} | |
for cookie in raw.split(";"): | |
cookie = cookie.strip() | |
if not cookie: continue | |
key, value = cookie.split("=", 1) | |
self.setCookie(key.strip(), value.strip(), domain) | |
# load cookie which format Netscape cookie | |
def cookieFromFile(self, path): | |
if not os.path.exists(path): return | |
cookieJar = http.cookiejar.MozillaCookieJar(path) | |
cookieJar.load(ignore_discard = True, ignore_expires = True) | |
for cookie in cookieJar: | |
self.setCookie(cookie.name, cookie.value, cookie.domain) | |
def setCookie(self, key, value, domain): | |
c = QNetworkCookie() | |
c.setDomain(domain) | |
c.setName(bytes(key, "utf-8")) | |
c.setValue(bytes(value, "utf-8")) | |
self.cookieStore().setCookie(c) | |
def injectScript(self, name, code): | |
self.scripts().insert(create_script(name, code)) | |
class BrowserTab(QMainWindow): | |
def __init__(self, parent): | |
super(BrowserTab, self).__init__(parent) | |
self._tabIndex = -1 | |
self.mainWindow = self.parent() | |
self.webView = QWebEngineView(self) | |
self.page = QWebEnginePage(self.mainWindow.profile, self) | |
self.webView.setPage(self.page) | |
self.webView.load(QUrl("about:blank")) | |
self.setCentralWidget(self.webView) | |
self.navigation_bar = QToolBar('Navigation') | |
self.navigation_bar.setIconSize(QSize(24, 24)) | |
self.navigation_bar.setMovable(False) | |
self.addToolBar(self.navigation_bar) | |
self.back_button = QAction('Back', self) | |
self.next_button = QAction('Forward', self) | |
self.refresh_button = QAction('Reload', self) | |
self.home_button = QAction('Home', self) | |
self.enter_button = QAction('Go', self) | |
self.add_button = QAction('New', self) | |
self.ssl_status = QLabel(self) | |
self.url_text_bar = QLineEdit(self) | |
self.url_text_bar.setMinimumWidth(300) | |
self.navigation_bar.addAction(self.back_button) | |
self.navigation_bar.addAction(self.next_button) | |
self.navigation_bar.addAction(self.refresh_button) | |
self.navigation_bar.addAction(self.home_button) | |
self.navigation_bar.addAction(self.add_button) | |
self.navigation_bar.addSeparator() | |
self.navigation_bar.addWidget(self.ssl_status) | |
self.navigation_bar.addWidget(self.url_text_bar) | |
self.navigation_bar.addAction(self.enter_button) | |
self.trigger() | |
self.IsLoading = False | |
def setTabIndex(self, index): | |
self._tabIndex = index | |
def trigger(self): | |
self.back_button.triggered.connect(self.webView.back) | |
self.next_button.triggered.connect(self.webView.forward) | |
self.refresh_button.triggered.connect(self.onRefresh) | |
self.home_button.triggered.connect(self.navigateHome) | |
self.enter_button.triggered.connect(self.navigateUrl) | |
self.url_text_bar.returnPressed.connect(self.navigateUrl) | |
self.webView.iconChanged.connect(lambda x: self.mainWindow.tabs.setTabIcon(self._tabIndex, self.webView.icon())) | |
self.webView.urlChanged.connect(self.urlChanged) | |
self.webView.loadProgress.connect(self.onLoading) | |
self.add_button.triggered.connect(lambda x: self.mainWindow.AddNewTab(BrowserTab(self.mainWindow))) | |
self.webView.titleChanged.connect(self.onWebViewTitleChange) | |
self.webView.createWindow = self.webViewCreateWindow # kết nối hàm tạo tab mới khi click vào | |
# hình ảnh hoặc 1 đường dẫn bất kỳ. | |
def webViewCreateWindow(self, *args): | |
tab = BrowserTab(self.mainWindow) | |
self.mainWindow.AddNewTab(tab) | |
return tab.webView | |
def onWebViewTitleChange(self, title): | |
self.mainWindow.setWindowTitle(title) | |
self.mainWindow.tabs.setTabToolTip(self._tabIndex, title) | |
if len(title) >= 10: | |
title = title[:10] + "..." | |
self.mainWindow.tabs.setTabText(self._tabIndex, title) | |
def navigateUrl(self): | |
s = QUrl(self.url_text_bar.text()) | |
if s.scheme() == '': | |
s.setScheme('http') | |
self.webView.load(s) | |
def navigateHome(self): | |
s = QUrl("about:blank") | |
self.webView.load(s) | |
# Xác định xem link đó là https hay http | |
def urlChanged(self, s): | |
prec = s.scheme() | |
if prec == 'http': | |
self.ssl_status.setText(" unsafe ") | |
self.ssl_status.setStyleSheet("color:red;") | |
elif prec == 'https': | |
self.ssl_status.setText(" safe ") | |
self.ssl_status.setStyleSheet("color:green;") | |
self.url_text_bar.setText(s.toString()) | |
self.url_text_bar.setCursorPosition(0) | |
def onLoading(self, p): | |
if p < 100 and self.IsLoading == False: | |
self.refresh_button.setText("Loading") | |
self.IsLoading = True | |
elif p == 100 and self.IsLoading: | |
self.refresh_button.setText("Reload") | |
self.IsLoading = False | |
def onRefresh(self): | |
if self.IsLoading: | |
self.webView.stop() | |
else: | |
self.webView.reload() | |
def close(self): | |
self.webView.close() | |
super(BrowserTab, self).close() | |
class MainWindow(QMainWindow): | |
"""docstring for MainWindow""" | |
def __init__(self, *args, **kwargs): | |
super(MainWindow, self).__init__(*args, **kwargs) | |
self.tabs = QTabWidget(tabsClosable=True, movable=True) | |
self.tabs.setTabShape(0) | |
# self.resize(800, 600) | |
#self.showMaximized() | |
self.setCentralWidget(self.tabs) | |
self.tabs.tabCloseRequested.connect(self.CloseCurrentTab) | |
self.tabs.currentChanged.connect(lambda i: self.setWindowTitle(self.tabs.tabText(i))) | |
self.profile = WebProfile(self) | |
def AddNewTab(self, tab): | |
i = self.tabs.addTab(tab, "") | |
tab.setTabIndex(i) | |
self.tabs.setCurrentIndex(i) | |
def CloseCurrentTab(self, tabIndex): | |
if self.tabs.count() > 1: | |
self.tabs.widget(tabIndex).close() | |
self.tabs.removeTab(tabIndex) | |
else: | |
self.close() | |
def NewTab(self, url): | |
tab = BrowserTab(self) | |
tab.webView.load(QUrl(url)) | |
self.AddNewTab(tab) | |
def InjectScript(self, name, code): | |
self.profile.injectScript(name, code) | |
def CookieFromString(self, raw, domain): | |
self.profile.cookieFromString(raw, domain) | |
COOKIES = {} | |
COOKIES[".google.com"] = ''' | |
cookie is here | |
''' | |
SCRIPTS = {} | |
SCRIPTS["colabAlive.js"] = ''' | |
// ==UserScript== | |
// @name Colab Alive | |
// @namespace colab | |
// @version 0.1 | |
// @description Bypass limit 90 min google colab | |
// @author NguyenKhong | |
// @match https://colab.research.google.com/drive/* | |
// @run-at document-start | |
// ==/UserScript== | |
document.addEventListener('DOMContentLoaded', function() { | |
function ClickConnect(){ | |
try{ | |
document.querySelector("colab-connect-button").click(); | |
}catch(e){} | |
} | |
setInterval(ClickConnect,60000); | |
}); | |
''' | |
def main(): | |
app = QApplication(sys.argv) | |
mainWin = MainWindow() | |
for cookie_domain, cookie_value in COOKIES.items(): | |
mainWin.CookieFromString(cookie_value, cookie_domain) | |
for script_name, script_code in SCRIPTS.items(): | |
mainWin.InjectScript(script_name, script_code) | |
mainWin.NewTab("url colab is here") | |
mainWin.resize(1280, 720) | |
mainWin.show() | |
sys.exit(app.exec_()) | |
if __name__ == '__main__': | |
main() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment