diff options
| -rw-r--r-- | .gitignore | 1 | ||||
| -rw-r--r-- | Makefile | 10 | ||||
| -rw-r--r-- | README.md | 20 | ||||
| -rw-r--r-- | config.mk | 2 | ||||
| -rw-r--r-- | sb.cpp | 130 |
5 files changed, 163 insertions, 0 deletions
diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..beff141 --- /dev/null +++ b/.gitignore @@ -0,0 +1 @@ +sb diff --git a/Makefile b/Makefile new file mode 100644 index 0000000..07988bc --- /dev/null +++ b/Makefile @@ -0,0 +1,10 @@ +include config.mk + +sb: sb.cpp + g++ sb.cpp -o sb $(LDFLAGS) + +install: + install sb /usr/local/bin/ + +uninstall: + rm /usr/local/bin/sb diff --git a/README.md b/README.md new file mode 100644 index 0000000..a4368e7 --- /dev/null +++ b/README.md @@ -0,0 +1,20 @@ +# Simple Browser + +This is a little browser which depends on the QtWebEngine. + +In Contrast to the GtkWebkit the QtWebEngine runs under Chromium and +is lighter and faster and supports more modern websites. + +Because of the fact that the Qt library is only provided for C++ +this browser is written in c++. + +## Keybindings + +- k : scroll up +- j : scroll down +- o : open url + +## TODO + +- Execute Shell Commands +- add config-header-file diff --git a/config.mk b/config.mk new file mode 100644 index 0000000..16cf0f0 --- /dev/null +++ b/config.mk @@ -0,0 +1,2 @@ +CC=c++ +LDFLAGS=`pkg-config --libs --cflags Qt5WebEngineWidgets` -lQt5X11Extras -lX11 @@ -0,0 +1,130 @@ +#include <iostream> +#include <regex> + +#include <QtWidgets/QApplication> +#include <QtGui/QWindow> +#include <QtWidgets/QMainWindow> +#include <QtWebEngineWidgets/QWebEngineView> +#include <QtWidgets/QWidget> +#include <QtWidgets/QLayout> +#include <QtWidgets/QLabel> +#include <QtWidgets/QLineEdit> +#include <QtGui/QKeyEvent> +#include <QtX11Extras/QX11Info> +#include <X11/Xlib.h> + + +bool is_valid_url(const std::string & url); +void open_url(const std::string & url); +void search(); +void page_scroll(int scroll); +class Main; +void get_window_id(); +uint32_t id; + +bool is_valid_url(const std::string & url) { + std::regex r(".*://.*\\..*"); + std::smatch m; + + return std::regex_match(url, m, r); +} + +std::string style = "QWidget { background: #181818; }" + "QLineEdit { color: #547f62; border: none; } " + "QLabel { color: #547f62; } "; + +QWebEngineView * web; +Main * view; + +class Main : public QMainWindow { + protected: + void keyPressEvent(QKeyEvent * event) { + if (event->text() == "o") { + + search(); + } else if (event->text() == "j") { + page_scroll(20); + } else if (event->text() == "k") + page_scroll(-20); + } +}; + +std::string read_from_dmenu() { + std::string output; + + std::string cmd = "printf '' | dmenu -p 'open: ' -w "; + cmd += std::to_string(view->winId()); + + FILE * f = popen(cmd.c_str(), "r"); + + char string[8092] = {0}; + fgets(string, 8092, f); + fclose(f); + + if (string[0] != 0) { + output = string; + output.pop_back(); + } + + return output; +} + +void open_url(const std::string & url) { + web->load(QString::fromStdString(url)); +} + +void search() { + auto url = read_from_dmenu(); + + if (url.empty()) + return; + + if (is_valid_url(url)) { + open_url(url); + return; + } + + if (is_valid_url("https://" + url)) { + open_url("https://" + url); + return; + } + + open_url("https://duckduckgo.com/?q=" + url); +} + +void page_scroll(int scroll) { + std::string js = "window.scrollBy(0, " + std::to_string(scroll) + ");"; + web->page()->runJavaScript(QString::fromStdString(js)); +} + +void help(char *arg0) { + std::cout << arg0 << '\n'; + std::cout << "-e [xid] - embed in other X window\n"; +} + +int +main(int argc, char **argv) { + QApplication app(argc, argv); + app.setStyleSheet(QString::fromStdString(style)); + view = new Main(); + web = new QWebEngineView(); + web->load(QUrl("")); + + if (argc == 3) { + if (argv[1] == std::string("-e")) { + Display * dpy = XOpenDisplay(NULL); + int eid = std::stoi(argv[2]); + XReparentWindow(dpy, view->winId(), eid, 0, 0); + XFlush(dpy); + } else { + help(argv[0]); + } + } else if (argc != 1) { + help(argv[0]); + } + + view->show(); + view->setCentralWidget(web); + + return app.exec(); +} |