-
Notifications
You must be signed in to change notification settings - Fork 3
/
main.py
377 lines (328 loc) · 14.6 KB
/
main.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
# light, reliable, extensible web browser
BN_VERSION = "0.0.1"
import sys, os, json, requests, zipfile, io
from PyQt5.QtCore import *
from PyQt5.QtWidgets import *
from PyQt5.QtWebEngineWidgets import *
from settings import SettingsWindow
conf_path = os.path.join(os.path.dirname(os.path.realpath(__file__)), "config.json")
conf = json.load(open(conf_path))
bookmarks = []
def formatUrl(url):
if url.startswith("http://") or url.startswith("https://"):
return url
else:
return "http://" + url
def deepcopy(obj):
new_obj = {}
for key, value in obj.items():
if isinstance(value, dict):
new_obj[key] = deepcopy(value)
else:
new_obj[key] = value
return new_obj
class MainWindow(QMainWindow):
def __init__(self):
super().__init__()
self.setWindowTitle("Blazenet")
self.setGeometry(100, 100, 800, 600)
self.browsers = [QWebEngineView()]
self.browser_index = 0
# Open DuckDuckGo
self.browser().setUrl(QUrl(conf["homepage"]))
self.setCentralWidget(self.browser())
# navbar
self.navbar = QToolBar()
self.navbar.setMovable(False)
# make it 50px high
self.navbar.setIconSize(QSize(50, 50))
self.addToolBar(self.navbar)
# back button
back_btn = QAction("🠔", self)
back_btn.triggered.connect(self.backProcess)
self.navbar.addAction(back_btn)
# forward button
forward_btn = QAction("🠖", self)
forward_btn.triggered.connect(self.forwardProcess)
self.navbar.addAction(forward_btn)
# reload button
self.reload_btn = QAction("⭮", self)
self.reload_btn.triggered.connect(self.reloadProcess)
self.navbar.addAction(self.reload_btn)
# home button
home_btn = QAction("⌂", self)
home_btn.triggered.connect(self.homeProcess)
self.navbar.addAction(home_btn)
# address bar
self.address_bar = QLineEdit()
self.address_bar.setPlaceholderText("Enter URL")
self.address_bar.returnPressed.connect(lambda: self.browser().setUrl(QUrl(formatUrl(self.address_bar.text()))))
self.navbar.addWidget(self.address_bar)
# search bar
self.search_bar = QLineEdit()
self.search_bar.setPlaceholderText("Search")
self.search_bar.returnPressed.connect(lambda: self.browser().setUrl(QUrl(conf["search_engine"].format(query = self.search_bar.text()))))
self.navbar.addWidget(self.search_bar)
# tab switcher
self.tab_switcher = QComboBox()
self.tab_switcher.currentIndexChanged.connect(self.tabSwitcherProcess)
self.navbar.addWidget(self.tab_switcher)
self.tab_switcher.setFixedWidth(250)
# add new tab to the tab switcher
self.tab_switcher.addItem("New tab")
self.tab_switcher.setCurrentIndex(0)
# new tab button
new_tab_btn = QAction("➕", self)
new_tab_btn.triggered.connect(self.newTabProcess)
self.navbar.addAction(new_tab_btn)
# close tab button
close_tab_btn = QAction("➖", self)
close_tab_btn.triggered.connect(self.closeTabProcess)
self.navbar.addAction(close_tab_btn)
# load extensions
self.extensions = []
self.found_extensions = []
for ext_fldr in os.listdir(os.path.join(os.path.dirname(__file__), "ext")):
ext_path = os.path.join(os.path.dirname(__file__), "ext", ext_fldr)
if os.path.isdir(ext_path):
self.found_extensions.append(__import__("ext." + ext_fldr + ".main", fromlist=["ext"]))
# if the extension folder is in config["extensions"]["blacklist"], skip it
if ext_fldr in conf["extensions"]["blacklist"]:
print("Skipped extension: " + ext_fldr)
continue
self.extensions.append(__import__("ext." + ext_fldr + ".main", fromlist=["ext"]))
print("Loaded extension: " + ext_fldr)
# for each extension, call blazeOnApplicationLoad(app, browser, name)
for ext in self.extensions:
# check if the extension has the function blazeOnApplicationLoad()
if hasattr(ext, "blazeOnApplicationLoad"):
ext.blazeOnApplicationLoad(self, self.browser(), ext.__name__.lstrip("ext.").rstrip(".main"))
# Blazenet menu
# will have options like quit, about, etc.
blazenet_menu = self.menuBar().addMenu("Blazenet")
# about
about_action = QAction("About", self)
about_action.triggered.connect(self.aboutProcess)
blazenet_menu.addAction(about_action)
# settings
settings_action = QAction("Settings", self)
settings_action.triggered.connect(self.settingsProcess)
blazenet_menu.addAction(settings_action)
# quit
quit_action = QAction("Quit", self)
quit_action.triggered.connect(self.quitProcess)
blazenet_menu.addAction(quit_action)
# favourites menu
self.favourites_menu = self.menuBar().addMenu("Favourites")
# add favourite
add_favourite_action = QAction("Add", self)
add_favourite_action.triggered.connect(self.addFavouriteProcess)
self.favourites_menu.addAction(add_favourite_action)
# remove favourite
remove_favourite_action = QAction("Remove", self)
remove_favourite_action.triggered.connect(self.removeFavouriteProcess)
self.favourites_menu.addAction(remove_favourite_action)
# separator
self.favourites_menu.addSeparator()
# favourites
for favourite in conf["favourites"]:
fav_action = QAction(favourite["name"], self)
fav_action.triggered.connect(lambda: self.browser().setUrl(QUrl(favourite["url"])))
self.favourites_menu.addAction(fav_action)
# when the page changes, call onPageChangedProcess()
self.browser().urlChanged.connect(self.onPageChangedProcess)
# when the browser loads a page, call onPageLoadProcess()
self.browser().loadFinished.connect(self.onPageLoadProcess)
self.showMaximized()
def browser(self):
return self.browsers[self.browser_index]
def tabSwitcherProcess(self):
self.browser_index = self.tab_switcher.currentIndex()
# show the correct browser
self.takeCentralWidget()
self.setCentralWidget(self.browsers[self.browser_index])
# set the window title to "Blazenet • " + page title + " • " + url
self.setWindowTitle("Blazenet • " + self.browser().page().title() + " • " + self.browser().url().toString())
# set the address bar to the url
self.address_bar.setText(self.browser().url().toString())
def newTabProcess(self):
self.browsers.append(QWebEngineView())
self.tab_switcher.addItem("New tab")
self.tab_switcher.setCurrentIndex(len(self.browsers) - 1)
self.tabSwitcherProcess()
# when the page changes, call onPageChangedProcess()
self.browser().urlChanged.connect(self.onPageChangedProcess)
# when the browser loads a page, call onPageLoadProcess()
self.browser().loadFinished.connect(self.onPageLoadProcess)
# homepage
try: self.homeProcess()
except: pass
def closeTabProcess(self):
if len(self.browsers) > 1:
self.browsers.pop(self.browser_index)
self.tab_switcher.removeItem(self.browser_index)
if self.browser_index >= len(self.browsers):
self.browser_index = len(self.browsers) - 1
self.tabSwitcherProcess()
def onPageChangedProcess(self):
# if the url is special (starting with "http://localhost/BLAZECMD/"), call the function associated with the url
if self.browser().url().toString().startswith("http://localhost/BLAZECMD/"):
print("Executing command: " + self.browser().url().toString().replace("http://localhost/BLAZECMD/", ""))
# get the function name
func_name = self.browser().url().toString().lstrip("http://localhost/BLAZECMD/").split("/")[0]
args = self.browser().url().toString().lstrip("http://localhost/BLAZECMD/").split("/")[1:]
# if name = "install-extension", install the extension
if func_name == "install-extension":
if conf["blaze_commands"]["install-extension"]:
print("Installing extension: " + args[0])
self.browser().load(QUrl("https://rexxt.github.io/blazenet/ext/installing"))
# edit window title
self.setWindowTitle("Blazenet • Installing extension " + args[0] + "...")
# make url
url = f"https://github.com/Rexxt/blazenet/raw/gh-pages/ext/{args[0]}/package.zip"
# download the zip file
r = requests.get(url, stream = True)
# extract the zip file to ext/args[0]
with zipfile.ZipFile(io.BytesIO(r.content)) as zip_file:
zip_file.extractall(os.path.join(os.path.dirname(__file__), "ext", args[0]))
# load the single extension
self.extensions.append(__import__("ext." + args[0] + ".main", fromlist=["ext"]))
self.found_extensions.append(self.extensions[-1])
# call blazeOnApplicationLoad()
if hasattr(self.extensions[-1], "blazeOnApplicationLoad"):
self.extensions[-1].blazeOnApplicationLoad(self, self.browser(), args[0])
# show info dialog
QMessageBox.information(self, "Extension installed", f"Extension {args[0]} installed successfully.")
self.browser().back()
else:
self.browser().back()
QMessageBox.warning(self, "Extension not installed", "Live extension installation is disabled in the settings.")
self.browser().back()
# for each extension, call blazeOnPageChanged(app, browser, url, page)
for ext in self.extensions:
# check if the extension has the function blazeOnPageChanged()
if hasattr(ext, "blazeOnPageChanged"):
ext.blazeOnPageChanged(self, self.browser(), self.browser().url().toString(), self.browser().page())
# set the window title to "Blazenet • " + page title + " • " + url
self.setWindowTitle("Blazenet • " + self.browser().page().title() + " • " + self.browser().url().toString())
# set the reload button text to ✖
self.reload_btn.setText("✖")
# set the address bar to the url
self.address_bar.setText(self.browser().url().toString())
# set tab text to page title
self.tab_switcher.setItemText(self.browser_index, self.browser().page().title())
def onPageLoadProcess(self):
# for each extension, call blazeOnPageLoad(app, browser, url, page)
for ext in self.extensions:
# check if the extension has the function blazeOnPageLoad()
if hasattr(ext, "blazeOnPageLoad"):
ext.blazeOnPageLoad(self, self.browser(), self.browser().url().toString(), self.browser().page())
# set the reload button text to ⭮
self.reload_btn.setText("⭮")
# set the window title to "Blazenet • " + page title + " • " + url
self.setWindowTitle("Blazenet • " + self.browser().page().title() + " • " + self.browser().url().toString())
# set tab text to page title
self.tab_switcher.setItemText(self.browser_index, self.browser().page().title())
def backProcess(self):
self.browser().back()
# for each extension, call blazeOnBack(app, browser, url, page)
for ext in self.extensions:
# check if the extension has the function blazeOnBack()
if hasattr(ext, "blazeOnBack"):
ext.blazeOnBack(self, self.browser(), self.browser().url().toString(), self.browser().page())
def forwardProcess(self):
self.browser().forward()
# for each extension, call blazeOnForward(app, browser, url, page)
for ext in self.extensions:
# check if the extension has the function blazeOnForward()
if hasattr(ext, "blazeOnForward"):
ext.blazeOnForward(self, self.browser(), self.browser().url().toString(), self.browser().page())
def reloadProcess(self):
self.browser().reload()
# for each extension, call blazeOnReload(app, browser, url, page)
for ext in self.extensions:
# check if the extension has the function blazeOnReload()
if hasattr(ext, "blazeOnReload"):
ext.blazeOnReload(self, self.browser(), self.browser().url().toString(), self.browser().page())
def homeProcess(self):
self.browser().setUrl(QUrl(conf["homepage"]))
# for each extension, call blazeOnHome(app, browser, url, page)
for ext in self.extensions:
# check if the extension has the function blazeOnHome()
if hasattr(ext, "blazeOnHome"):
ext.blazeOnHome(self, self.browser(), self.browser().url().toString(), self.browser().page())
def aboutProcess(self):
# display the about dialog
# aka information message box
QMessageBox.about(self, "About Blazenet", "Blazenet is a light, reliable, extensible and free web browser.\n\nBlazenet is open source software, licensed under the GNU General Public License v3.0.\n\nBlazenet is developed by Mizu.\n\nYou are running version " + BN_VERSION + ".")
def settingsProcess(self):
# display the settings dialog
# aka settings window
self.settings_window = SettingsWindow(self, self.browser, self.found_extensions)
self.settings_window.show()
def quitProcess(self):
# for each extension, call blazeOnQuit(app, browser, url, page)
for ext in self.extensions:
# check if the extension has the function blazeOnQuit()
if hasattr(ext, "blazeOnQuit"):
ext.blazeOnQuit(self, self.browser(), self.browser().url().toString(), self.browser().page())
self.close()
def reloadFavourites(self):
# remove all favourites
self.favourites_menu.clear()
# add favourite
add_favourite_action = QAction("Add", self)
add_favourite_action.triggered.connect(self.addFavouriteProcess)
self.favourites_menu.addAction(add_favourite_action)
# remove favourite
remove_favourite_action = QAction("Remove", self)
remove_favourite_action.triggered.connect(self.removeFavouriteProcess)
self.favourites_menu.addAction(remove_favourite_action)
# separator
self.favourites_menu.addSeparator()
# favourites
for favourite in conf["favourites"]:
fav_action = QAction(favourite["name"], self)
fav_action.triggered.connect(lambda: self.browser().setUrl(QUrl(favourite["url"])))
self.favourites_menu.addAction(fav_action)
def addFavouriteProcess(self):
# get the name and url from the user
name, ok = QInputDialog.getText(self, "Add Favourite", "Name:")
if ok:
url, ok = QInputDialog.getText(self, "Add Favourite", "URL:")
if ok:
# add the favourite to the config
conf["favourites"].append({"name": name, "url": formatUrl(url)})
# save the config
with open(conf_path, "w") as f:
json.dump(conf, f, indent=4)
# reload the favourites
self.reloadFavourites()
# for each extension, call blazeOnAddFavourite(app, browser, url, page)
for ext in self.extensions:
# check if the extension has the function blazeOnAddFavourite()
if hasattr(ext, "blazeOnAddFavourite"):
ext.blazeOnAddFavourite(self, self.browser(), url, name)
def removeFavouriteProcess(self):
# get the name and url from the user
name, ok = QInputDialog.getText(self, "Remove Favourite", "Name:")
if ok:
# remove the favourite from the config
for favourite in conf["favourites"]:
if favourite["name"] == name:
conf["favourites"].remove(favourite)
break
# save the config
with open(conf_path, "w") as f:
json.dump(conf, f, indent=4)
# reload the favourites
self.reloadFavourites()
# for each extension, call blazeOnRemoveFavourite(app, browser, url, page)
for ext in self.extensions:
# check if the extension has the function blazeOnRemoveFavourite()
if hasattr(ext, "blazeOnRemoveFavourite"):
ext.blazeOnRemoveFavourite(self, self.browser(), name)
app = QApplication(sys.argv)
QApplication.setApplicationName("Blazenet")
window = MainWindow()
app.exec_()