Skip to content

Commit

Permalink
Added ability to save the position
Browse files Browse the repository at this point in the history
  • Loading branch information
DisableGraphics committed May 28, 2022
1 parent 1f8a1b9 commit 4fed014
Show file tree
Hide file tree
Showing 7 changed files with 141 additions and 12 deletions.
14 changes: 11 additions & 3 deletions Makefile
Original file line number Diff line number Diff line change
@@ -1,13 +1,21 @@
all: korai
all: korai extensions

korai:
#-----------------------------------------------------
# BUILDING KORAI
#-----------------------------------------------------
mkdir -p build & g++ src/main.cpp `pkg-config gtkmm-3.0 --libs --cflags` `pkg-config webkit2gtk-4.0 --libs --cflags` `pkg-config vte-2.91 --libs --cflags` -larchive -pthread -O3 -std=c++17 -o build/korai

korai-nodownload:
korai-nodownload: extensions
#-----------------------------------------------------
# BUILDING KORAI-NODOWNLOAD
#-----------------------------------------------------
mkdir -p build & g++ src/main.cpp `pkg-config gtkmm-3.0 --libs --cflags` `pkg-config webkit2gtk-4.0 --libs --cflags` -DNODOWNLOAD -larchive -pthread -O3 -std=c++17 -o build/korai-nodownload
mkdir -p build & g++ src/main.cpp `pkg-config gtkmm-3.0 --libs --cflags` `pkg-config webkit2gtk-4.0 --libs --cflags` -DNODOWNLOAD -larchive -pthread -O3 -std=c++17 -o build/korai-nodownload

extensions:
#-----------------------------------------------------
# BUILDING EXTENSIONS
#-----------------------------------------------------
mkdir -p build
mkdir -p build/korai-extensions
g++ src/extensions/pos_save.cpp -shared -fPIC -o build/korai-extensions/savepos.so `pkg-config webkit2gtk-web-extension-4.0 --libs --cflags` -std=c++17
86 changes: 86 additions & 0 deletions src/extensions/pos_save.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
#include <fstream>
#include <functional>
#include <webkit2/webkit-web-extension.h>
#include <JavaScriptCore/JavaScript.h>
#include <iostream>
#include <thread>
#include <filesystem>

#define SLEEP_PERIOD 5

using namespace std::filesystem;

bool t{false};

static void save_pos(WebKitWebPage * web_page)
{
std::ofstream tmp_file;
tmp_file.open((std::string) std::filesystem::current_path() + "/poslck");
tmp_file << "e";
tmp_file.close();
sleep(SLEEP_PERIOD + 1);
std::filesystem::remove((std::string) std::filesystem::current_path() + "/poslck");
WebKitDOMDocument * doc = webkit_web_page_get_dom_document(web_page);
WebKitDOMDOMWindow * win = webkit_dom_document_get_default_view(doc);

std::ofstream o;

while(true && !std::filesystem::exists((std::string) std::filesystem::current_path() + "/poslck"))
{
sleep(SLEEP_PERIOD);
o.open(std::filesystem::current_path().string() + "/pos.conf");
o << webkit_dom_dom_window_get_scroll_y(win);
o.close();
std::cout << "Y: " << webkit_dom_dom_window_get_scroll_y(win) << "\nX: " << webkit_dom_dom_window_get_scroll_x(win) << "\n";

}

}

//
// Boilerplate code / signal callback for attaching methods when a
// new javascript context is created.
//
static void
window_object_cleared_callback (WebKitScriptWorld *world,
WebKitWebPage *web_page,
WebKitFrame *frame,
gpointer user_data)
{

if(!t)
{
std::thread dothesaving(std::bind(save_pos, web_page));
dothesaving.detach();
}
}

static void
web_page_created_callback (WebKitWebExtension *extension,
WebKitWebPage *web_page,
gpointer user_data)
{
g_print ("Page %d created for %s\n",
webkit_web_page_get_id (web_page),
webkit_web_page_get_uri (web_page));

}

//
// Extension initialization thing.
//
extern "C" G_MODULE_EXPORT void
webkit_web_extension_initialize (WebKitWebExtension *extension)
{
std::cout << "EXTENSION INITIALIZED\n";

g_signal_connect (webkit_script_world_get_default(),
"window-object-cleared",
G_CALLBACK (window_object_cleared_callback),
NULL);
g_signal_connect (extension, "page-created",
G_CALLBACK (web_page_created_callback),
NULL);


}
26 changes: 23 additions & 3 deletions src/functions.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -129,17 +129,37 @@ inline void load_homepage(WebKitWebView * webview)
//Generates the page for a chapter
inline std::string generateWebPage()
{
std::string scrollto{"0"};
if(std::filesystem::exists((std::string)std::filesystem::current_path() + "/pos.conf") && gotopos)
{
std::ifstream i;
i.open((std::string)std::filesystem::current_path() + "/pos.conf");
i >> scrollto;
i.close();
}

if(comp::isCompressed(file))
{
decompress(file);
}
std::vector<std::string> filesInFolda{getFilesInFolder(tmp_folder)};
std::string generated_html{html::initial_html};
for(auto & fayel : filesInFolda)
std::string generated_html{html::initial_html(scrollto)};
if(gotopos)
{
generated_html += "<img src=\"" + fayel + "\" loading=\"lazy\"><br>\n"; //Lazy loading to improve performance
for(auto & fayel : filesInFolda)
{
generated_html += "<img src=\"" + fayel + "\" " /*loading=\"lazy\"*/"><br>\n"; //Lazy loading prevents Korai from going to the initial position correctly
}
}
else
{
for(auto & fayel : filesInFolda)
{
generated_html += "<img src=\"" + fayel + "\" loading=\"lazy\"><br>\n"; //Lazy loading to improve performance otherwise
}
}
generated_html += "</body> </html>";
gotopos = false;
return generated_html;
}

Expand Down
3 changes: 2 additions & 1 deletion src/global_variables.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,5 @@
inline int position{-1};
//Variables to know different details
inline std::string file{""}, folder{""}, saveFile;
inline std::string tmp_folder{""};
inline std::string tmp_folder{""};
inline bool gotopos{true};
15 changes: 14 additions & 1 deletion src/main_window.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
#include "gtkmm/window.h"
#include <cstddef>
#include <filesystem>
#include <fstream>
#include <gtkmm.h>
#include <webkit2/webkit2.h>
#include "icon.xpm"
Expand All @@ -15,6 +16,7 @@
#include "comp.hpp"
#include "signal_functions.hpp"
#include "downloader.hpp"
#include "web_contents.h"
#include "webkitdom/webkitdomdefines.h"
#include <webkitdom/webkitdom.h>

Expand Down Expand Up @@ -69,6 +71,8 @@ class MainWindow : public Gtk::Window
buttonsBox.pack_start(openButton);
buttonsBox.pack_start(nextButton);

webkit_web_context_set_web_extensions_directory(webkit_web_context_get_default(), (std::filesystem::current_path().string() + "/korai-extensions").c_str());

WebKitSettings * settings = WEBKIT_SETTINGS(webkit_settings_new());
webkit_settings_set_enable_smooth_scrolling(settings, TRUE);
webkit_settings_set_allow_file_access_from_file_urls(settings, TRUE);
Expand Down Expand Up @@ -115,6 +119,8 @@ class MainWindow : public Gtk::Window
set_titlebar(titleBar);
titleBar.set_show_close_button();



if(tutorial)
{
//Yes, I need these absolute f*ckton of arguments. And there's no argument copied by value. (All of them are pointers and references)
Expand All @@ -134,12 +140,13 @@ class MainWindow : public Gtk::Window
}

}

show_all();

webkit_web_view_reload(webview);

g_signal_connect_object(webview,"load-changed",G_CALLBACK(on_load_changed), spin.gobj(), G_CONNECT_AFTER);
g_signal_connect(webkit_web_context_get_default(), "initialize-web-extensions",G_CALLBACK(on_get_extensions), NULL);
//These all the goddamn buttons. Man, not using Glade didn't pay off...
openButton.signal_clicked().connect(sigc::bind(sigc::ptr_fun(open), webview, &titleBar));
nextButton.signal_clicked().connect(sigc::bind(sigc::ptr_fun(next_chapter), webview, &titleBar));
Expand Down Expand Up @@ -178,4 +185,10 @@ class MainWindow : public Gtk::Window

Gtk::VBox menuBox;
Gtk::Separator sep1, sepabout, sep2;

static void on_get_extensions(WebKitWebContext *context, gpointer user_data)
{
std::string f{std::filesystem::current_path()};
webkit_web_context_set_web_extensions_directory(context, f.c_str());
}
};
1 change: 1 addition & 0 deletions src/signal_functions.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -224,6 +224,7 @@ inline void open_mangadex(WebKitWebView * webView, Gtk::HeaderBar * titleBar, st
//When korai is opened, this is executed
inline void on_load(WebKitWebView * webView, Gtk::HeaderBar& titlebar)
{

//Creates the 'tmp' directory
std::filesystem::create_directory(((std::string)std::filesystem::current_path() + "/tmp/"));
//Loads the chapter file
Expand Down
8 changes: 4 additions & 4 deletions src/web_contents.h
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ namespace css
"display: block;\n"
"margin-left: auto;\n"
"margin-right: auto;\n"
"max-width: 95%;\n"
"width: 95%;\n"
"max-height: calc((29.7 / 21) * 95%);\n"
"}\n"
};
Expand All @@ -34,17 +34,17 @@ namespace html
});
*/
//Used to create a new chapter page
inline std::string initial_html{"<html> <style>"
inline std::string initial_html(std::string scrollto){return "<html> <style>"
+ css::css +
"</style>"
"<body>"
"<script>"
"function jumpToTop() "
"{"
"window.scrollTo({ top: 0, behavior: 'smooth' });"
"window.scrollTo({ top: " + scrollto +", behavior: 'smooth' });"
"}"
"window.onload = jumpToTop;"
"</script>"
"</html>"};
"</html>";};

}

0 comments on commit 4fed014

Please sign in to comment.