software / BROWSER.py
shethjenil's picture
Upload 2 files
eee727d verified
# importing required libraries
from PyQt5.QtCore import *
from PyQt5.QtWidgets import *
from PyQt5.QtGui import *
from PyQt5.QtWebEngineWidgets import *
from PyQt5.QtPrintSupport import *
import sys
class MainWindow(QMainWindow):
def __init__(self, *args, **kwargs):
super(MainWindow, self).__init__(*args, **kwargs)
user_agent = "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/125.0.0.0 Safari/537.36"
profile = QWebEngineProfile.defaultProfile()
profile.setHttpUserAgent(user_agent)
self.browser = QWebEngineView()
self.browser.setUrl(QUrl("http://google.com"))
self.browser.urlChanged.connect(self.update_urlbar)
self.browser.loadFinished.connect(self.update_title)
self.setCentralWidget(self.browser)
self.status = QStatusBar()
self.setStatusBar(self.status)
navtb = QToolBar("Navigation")
self.addToolBar(navtb)
back_btn = QAction("Back", self)
back_btn.setStatusTip("Back to previous page")
back_btn.triggered.connect(self.browser.back)
navtb.addAction(back_btn)
next_btn = QAction("Forward", self)
next_btn.setStatusTip("Forward to next page")
next_btn.triggered.connect(self.browser.forward)
navtb.addAction(next_btn)
reload_btn = QAction("Reload", self)
reload_btn.setStatusTip("Reload page")
reload_btn.triggered.connect(self.browser.reload)
navtb.addAction(reload_btn)
home_btn = QAction("Home", self)
home_btn.setStatusTip("Go home")
home_btn.triggered.connect(self.navigate_home)
navtb.addAction(home_btn)
navtb.addSeparator()
self.urlbar = QLineEdit()
self.urlbar.returnPressed.connect(self.navigate_to_url)
navtb.addWidget(self.urlbar)
stop_btn = QAction("Stop", self)
stop_btn.setStatusTip("Stop loading current page")
stop_btn.triggered.connect(self.browser.stop)
navtb.addAction(stop_btn)
exec_js_btn = QAction("Execute JavaScript", self)
exec_js_btn.setStatusTip("Execute JavaScript in current page")
exec_js_btn.triggered.connect(self.execute_javascript)
navtb.addAction(exec_js_btn)
self.js_editor = QPlainTextEdit()
self.js_editor.setPlaceholderText("Enter JavaScript code here...")
self.js_editor.setMaximumHeight(100) # Set maximum height for the editor
self.js_editor.setStyleSheet("font-family: Courier; font-size: 12px;") # Set font and size
self.js_editor.setTabStopWidth(20) # Set tab width
self.js_editor.setTabChangesFocus(True) # Pressing Tab moves focus to the next widget
self.js_editor.setWordWrapMode(QTextOption.NoWrap) # Disable word wrapping
self.js_editor.setLineWrapMode(QPlainTextEdit.NoWrap) # Disable line wrapping
self.js_editor.setPlainText("") # Set initial text
self.js_editor.setToolTip("Enter JavaScript code to execute") # Set tooltip
self.js_editor.setFocusPolicy(Qt.StrongFocus) # Set focus policy
self.js_editor.setFocus() # Set focus to the text editor
self.js_editor_cursor = self.js_editor.textCursor() # Get text cursor
navtb.addWidget(self.js_editor)
self.show()
def update_title(self):
title = self.browser.page().title()
self.setWindowTitle("% s - Geek Browser" % title)
def navigate_home(self):
self.browser.setUrl(QUrl("http://www.google.com"))
def navigate_to_url(self):
q = QUrl(self.urlbar.text())
if q.scheme() == "":
q.setScheme("http")
self.browser.setUrl(q)
def update_urlbar(self, q):
self.urlbar.setText(q.toString())
self.urlbar.setCursorPosition(0)
def execute_javascript(self):
javascript_code = self.js_editor.toPlainText()
if javascript_code.strip():
self.browser.page().runJavaScript(javascript_code)
else:
QMessageBox.warning(self, "Empty JavaScript Code", "Please enter JavaScript code to execute.")
app = QApplication(sys.argv)
app.setApplicationName("Geek Browser")
window = MainWindow()
app.exec_()