【问题标题】:[C/C++]WebKitGtk get scrollbar position[C/C++]WebKitGtk 获取滚动条位置
【发布时间】:2023-01-10 04:18:28
【问题描述】:

我正在尝试获取 WebKitWebView 中的滚动条位置。 我已经阅读了足够多的文档,知道没有直观的方法。

它需要打包成一个可执行文件,因此 WebKitExtension 不会将其删除。

我什至不知道从哪里开始,所以我不会写任何 MRE

注意:如果需要,我可以使用 JavaScript,但我首先不知道如何与本机应用程序通信。

【问题讨论】:

    标签: c++ c webkit webkitgtk webkit2


    【解决方案1】:

    编辑:这个源代码很糟糕。当且仅当您要纠正它时才使用它。

    好吧,你们所有的小男孩、女孩和非欧几里德太空人,我来用一些有趣的知识来迷住你悲伤、疲倦和沮丧的神经元:

    在问这个问题 3 周后,我已经完成了(最后)。为了做到这一点,我不得不使用 WebKitExtension。为你的大脑和小脑欢欣鼓舞,这里有一个糟糕的源代码示例:

    #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
    ";
        
        g_signal_connect (webkit_script_world_get_default(),
                          "window-object-cleared", 
                          G_CALLBACK (window_object_cleared_callback), 
                          NULL);
    
    
        
    }
    

    编译它:g++ extension.cpp -shared -fPIC -o extension.so `pkg-config webkit2gtk-web-extension-4.0 --libs --cflags` -std=c++17

    另外:请查看官方文档以获取更多指南,以了解如何在 WebKitGTK 项目中使用扩展。

    在任何人开始讨厌我之前:我使用了不推荐使用的函数来实现它,并且我使用了临时文件。原因是我不知道如何使用 JavaScriptCore API(我仍在查看文档在哪里),而且我不知道如何在扩展之间进行通信。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2023-01-10
      • 2021-09-06
      • 2014-02-23
      • 1970-01-01
      • 1970-01-01
      • 2012-12-04
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多