From bbd6d152e7f22013d10572985784ea4f5fb7d013 Mon Sep 17 00:00:00 2001 From: Nathan Reiner Date: Tue, 24 Jan 2023 21:48:33 +0100 Subject: make it work --- Makefile | 3 +++ config.hpp | 4 ++++ sb.cpp | 56 +++++++++++++++++++++++++++++++++++++++----------------- 3 files changed, 46 insertions(+), 17 deletions(-) create mode 100644 config.hpp diff --git a/Makefile b/Makefile index 07988bc..bc8854e 100644 --- a/Makefile +++ b/Makefile @@ -3,6 +3,9 @@ include config.mk sb: sb.cpp g++ sb.cpp -o sb $(LDFLAGS) +clean: + rm sb + install: install sb /usr/local/bin/ diff --git a/config.hpp b/config.hpp new file mode 100644 index 0000000..e9c9fa1 --- /dev/null +++ b/config.hpp @@ -0,0 +1,4 @@ +const bool enable_smooth_scrollig = true; +const char search_engine[] = "https://searx.org/?q="; +const char dmenu_args[] = "-s 0"; +const unsigned scroll_speed = 100; diff --git a/sb.cpp b/sb.cpp index 661ba4b..617c3b7 100644 --- a/sb.cpp +++ b/sb.cpp @@ -5,6 +5,8 @@ #include #include #include +#include +#include #include #include #include @@ -13,13 +15,15 @@ #include #include +#include "config.hpp" 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 goto_end(bool end); void get_window_id(); +class Main; uint32_t parent_id = 0; bool is_valid_url(const std::string & url) { @@ -39,22 +43,26 @@ class Main : public QMainWindow { if (event->text() == "o") search(); else if (event->text() == "j") - page_scroll(20); + page_scroll(scroll_speed); else if (event->text() == "k") - page_scroll(-20); + page_scroll(-scroll_speed); else if (event->text() == "H") web->back(); else if (event->text() == "L") web->forward(); else if (event->text() == "r") web->reload(); + else if (event->text() == "g") + goto_end(false); + else if (event->text() == "G") + goto_end(true); } }; std::string read_from_dmenu() { std::string output; - std::string cmd = "printf '' | dmenu -p 'open: ' -w "; + std::string cmd = std::string("printf '' | dmenu -p 'open: ' ") + dmenu_args + " -w "; cmd += std::to_string(view->winId()); FILE * f = popen(cmd.c_str(), "r"); @@ -91,11 +99,18 @@ void search() { return; } - open_url("https://duckduckgo.com/?q=" + url); + open_url(search_engine + url); } void page_scroll(int scroll) { - std::string js = "window.scrollBy(0, " + std::to_string(scroll) + ");"; + std::string js = "window.scrollBy({ top: " + std::to_string(scroll) + ", behavior: 'smooth' });"; + web->page()->runJavaScript(QString::fromStdString(js)); +} + +void goto_end(bool end) { + std::string js; + if (end) js = "window.scroll({ top: document.body.scrollHeight, behavior: 'smooth' })"; + else js = "window.scroll({ top: 0, behavior: 'smooth' })"; web->page()->runJavaScript(QString::fromStdString(js)); } @@ -106,25 +121,32 @@ void help(char *arg0) { int main(int argc, char **argv) { - QApplication app(argc, argv); + std::vector arg(argv, argv + argc); + + if (enable_smooth_scrollig) + arg.emplace_back((char*)"--enable-smooth-scrolling"); + + int size = arg.size(); + + QApplication app(size, arg.data()); + QWebEngineProfile profile; + QWebEnginePage page(&profile); + view = new Main(); web = new QWebEngineView(); - std::cout << getenv("WWW_HOME") << '\n'; + web->setPage(&page); web->load(QUrl(getenv("WWW_HOME"))); - if (argc == 3) { - if (argv[1] == std::string("-e")) { - dpy = XOpenDisplay(NULL); - parent_id = std::stoi(argv[2]); - XReparentWindow(dpy, view->winId(), parent_id, 0, 0); - XFlush(dpy); - } else { - help(argv[0]); - } + if (argc == 3 && argv[1] == std::string("-e")) { + dpy = XOpenDisplay(NULL); + parent_id = std::stoi(argv[2]); + XReparentWindow(dpy, view->winId(), parent_id, 0, 0); + XFlush(dpy); } else if (argc != 1) { help(argv[0]); } + view->show(); view->setCentralWidget(web); -- cgit v1.2.3-70-g09d2