c++cwebkitwebkitgtkwebkit2

[C/C++]WebKitGtk get scrollbar position


I'm trying to get the scrollbar position in a WebKitWebView. I've read enough documentation to know that there's no intuitive way.

It needs to be packaged into one executable, so a WebKitExtension won't cut it out.

I don't even know where to begin so I won't be writing any MRE

Note: I could use JavaScript if the need arises, but I don't know how to communicate with the native application in the first place.


Solution

  • Edit: this source code is pretty bad. Use it if and only if you're gonna correct it.

    Ok y'all little boys, girls and non-euclidean space beings, I've come to infatuate your sad, tired and frustrated neurons with some delicious knowledge:

    I've done it (AT LONG LAST), 3 weeks after asking this question. I've had to use a WebKitExtension in order to do this. Rejoice your brain and cerebellum, for an example shitty source code is here:

    #include <fstream>
    #include <functional>
    #include <webkit2/webkit-web-extension.h>
    #include <JavaScriptCore/JavaScript.h>
    #include <iostream>
    #include <thread>
    #include <filesystem>
    
    #define SLEEP_PERIOD 5
    
    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();
    
      }
      
    }
    
    static void 
    window_object_cleared_callback (WebKitScriptWorld *world, 
                                    WebKitWebPage *web_page, 
                                    WebKitFrame *frame, 
                                    gpointer user_data)
    { 
    
    
      
      std::thread dothesaving(std::bind(save_pos, web_page));
      dothesaving.detach();
      
    }
    
    extern "C" G_MODULE_EXPORT void
    webkit_web_extension_initialize (WebKitWebExtension *extension)
    {
        std::cout << "[INFO] Extension initialized\n";
        
        g_signal_connect (webkit_script_world_get_default(),
                          "window-object-cleared", 
                          G_CALLBACK (window_object_cleared_callback), 
                          NULL);
    
    
        
    }
    

    Compile it with: g++ extension.cpp -shared -fPIC -o extension.so `pkg-config webkit2gtk-web-extension-4.0 --libs --cflags` -std=c++17

    Also: please take a look at the official documentation for more guidelines to know how the hecc you could use extensions in your WebKitGTK project.

    Before anybody begins hating me: I've used deprecated functions for this to work and I've used temporary files. The reason is that I don't know how to use the JavaScriptCore API (I'm still to see where is the documentation) and also that I don't know how to communicate between extensions.