WebKit2和DomDocument / JavaScriptCore(Python3) [英] WebKit2 and DomDocument/JavaScriptCore (Python3)

查看:101
本文介绍了WebKit2和DomDocument / JavaScriptCore(Python3)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在将Python3应用程序转换为使用WebKit2而不是WebKit(在Debian Buster中不再可用)。

I am converting a Python3 application to use WebKit2 instead of WebKit (which is no longer available in Debian Buster).

在应用程序中,用户可以(de)选中我从Python3应用程序中读取的复选框。在原始代码中,我可以简单地获取Webview的DomDocument并遍历子对象以返回具有给定名称的对象的值(下面的示例代码)。

In the application the user can (de)select check boxes which I read from the Python3 application. In the original code I could simply get the DomDocument of the Webview and iterate through the child objects to return the value of the object with a given name (sample code below).

在WebKit2中, get_dom_document 功能不再可用, WebKit2文档尚不清楚如何进行

In WebKit2 the get_dom_document function is no longer available and the WebKit2 documentation is not clear on how to proceed.

有人知道如何从WebKit2 Webview获取DomDocument或通过名称迭代所有对象并获取其值的另一种方法吗?

Does anybody know how to get the DomDocument from a WebKit2 Webview or another way to iterate through all objects by name and get their value?

WebKit Webview中的DomDocument示例代码:

def get_element_values(self, element_name):
    values = []
    doc = self.get_dom_document()
    elements = doc.get_elements_by_name(element_name)
    for i in range(elements.get_length()):
        child = elements.item(i)
        value = child.get_value().strip()
        if not child.get_checked():
            value = ''
        if value:
            values.append(value)
    return values

注意: self是WebKit.Webview。

Note: "self" is the WebKit.Webview.

我一直在尝试这种方法。首先,我尝试通过首先获取WebPage对象来获取DomDocument。我发现了WebView.get_page_id()和WebKit2WebExtension.get_page(page_id),但不幸的是,这给了我这个错误:AttributeError:'gi.repository.WebKit2WebExtension'对象没有属性'get_page'。

I have been experimenting with this. First I tried to get the DomDocument by getting the WebPage object first. I found WebView.get_page_id() and WebKit2WebExtension.get_page(page_id) but unfortunately that gave me this error: AttributeError: 'gi.repository.WebKit2WebExtension' object has no attribute 'get_page'.

之后,我尝试使用JavaScriptCore并使用WebView.run_javascript()和WebView.run_javascript_finish()插入JavaScript。我得到了JavaScriptResult对象,但是当我尝试使用.get_value()获取值时,出现以下错误:TypeError:找不到'JavaScriptCore.Value'的外部结构转换器。

After that I tried JavaScriptCore and insert JavaScript with WebView.run_javascript() and WebView.run_javascript_finish(). I get a JavaScriptResult object back but when I try to get the value with .get_value() I get this error: TypeError: Couldn't find foreign struct converter for 'JavaScriptCore.Value'.

有人在此处遇到相同的问题,但是建议的解决方案对我来说也太复杂了。

There was a user with the same issue here, but the proposed solution was too complicated for me as well.

这是我用于测试的代码(检查get_element_values()和javascript_finished()函数) ):

Here's the code I used for testing (check the get_element_values() and javascript_finished() functions):

#! /usr/bin/env python3

# WebKit2 reference: https://webkitgtk.org/reference/webkit2gtk/stable
# Code examples: https://github.com/sidus-dev/poseidon/blob/master/poseidon.py

import gi
gi.require_version('WebKit2', '4.0')
gi.require_version('WebKit2WebExtension', '4.0')
from gi.repository import WebKit2, WebKit2WebExtension, Gtk
from os.path import exists
import webbrowser
import re


class SimpleBrowser(WebKit2.WebView):
    def __init__(self):
        WebKit2.WebView.__init__(self)

        # Get version
        self.webkit_ver = WebKit2.get_major_version(), WebKit2.get_minor_version(), WebKit2.get_micro_version()
        print(("WebKit2 Version: {0}".format('.'.join(map(str, self.webkit_ver)))))

        # Signals
        self.connect('decide-policy', self.on_decide_policy)
        #self.connect("load_changed", self.on_load_changed)
        self.connect("load-failed", self.on_load_failed)
        self.connect('button-press-event', lambda w, e: e.button == 3)

        # Settings
        s = self.get_settings()
        s.set_property('allow_file_access_from_file_urls', True)
        s.set_property('enable-spatial-navigation', False)
        s.set_property('enable_javascript', True)


    def show_html(self, html_or_url):
        if exists(html_or_url):
            matchObj = re.search('^file:\/\/', html_or_url)
            if not matchObj:
                html_or_url = "file://{0}".format(html_or_url)
        matchObj = re.search('^[a-z]+:\/\/', html_or_url)
        if matchObj:
            self.load_uri(html_or_url)
        else:
            self.load_html(html_or_url)
        self.show()

    def get_element_values(self, object, element_name):
        # JavaScript > dead end :(
        # https://webkitgtk.org/reference/webkit2gtk/stable/WebKitWebView.html#webkit-web-view-run-javascript
        js = 'var e = document.getElementsByName("' + element_name + '"); var r = []; var c = 0; ' \
             'for (var i = 0; i < e.length; i++) { if (e[i].checked) { r[c] = e[i].value; c++;} }'
        self.run_javascript(js, None, self.javascript_finished, None);
        return

        # DOM > dead end :(
        # https://webkitgtk.org/reference/webkit2gtk/stable/WebKitWebPage.html
        # https://webkitgtk.org/reference/webkit2gtk/stable/WebKitWebView.html#webkit-web-view-get-page-id
        page_id = self.get_page_id()
        print((page_id))
        # https://webkitgtk.org/reference/webkit2gtk/stable/WebKitWebExtension.html#webkit-web-extension-get-page
        # AttributeError: 'gi.repository.WebKit2WebExtension' object has no attribute 'get_page'
        web_page = WebKit2WebExtension.get_page(page_id)
        print((web_page))

    def javascript_finished(self, webview, result, user_data):
        # https://webkitgtk.org/reference/webkit2gtk/stable/WebKitWebView.html#webkit-web-view-run-javascript-finish
        js_result = self.run_javascript_finish(result)
        print((">>> js_result = %s" % str(js_result)))
        # TypeError: Couldn't find foreign struct converter for 'JavaScriptCore.Value'
        value = js_result.get_value()
        print((">>> value = %s" % str(value)))

    def on_decide_policy(self, webview, decision, decision_type):
        # User clicked on a <a href link: open uri in new tab or new default webview
        if (decision_type == WebKit2.PolicyDecisionType.NAVIGATION_ACTION):
            action = decision.get_navigation_action()
            action_type = action.get_navigation_type()
            if action_type == WebKit2.NavigationType.LINK_CLICKED:
                decision.ignore()
                uri = action.get_request().get_uri()
                # Open link in default browser
                webbrowser.open_new_tab(uri)
        else:
            if decision is not None:
                decision.use()

    def on_load_changed(webview, event):
        # TODO: get html of loaded page
        if event == WebKit2.LoadEvent.FINISHED:
            resource = webview.get_main_resource()
            resource.get_data()
            html = resource.get_data_finish(None)
            print(html)

    def on_load_failed(webview, event, url, error):
        print("Error loading", url, "-", error)

html = '<html><body style="background-color:#E6E6E6;"><h1>WebKit2 Test</h1>' \
       '<p><a href="https://solydxk.com">link</a></p><form onsubmit="return false;">' \
       '<input type="checkbox" name="chktst" value="checkbox1" checked /> CheckBox 1<br />' \
       '<input type="checkbox" name="chktst" value="checkbox2" /> CheckBox 2<br />' \
       '<button onclick="alert(show_values());">JS Show Values</button>' \
       '</form>' \
       '<script>' \
       'function show_values() {' \
       'var e = document.getElementsByName("chktst"); var r = []; var c = 0;' \
       'for (var i = 0; i < e.length; i++) {' \
       '    if (e[i].checked) { r[c] = e[i].value; c++;}' \
       '}return r;}' \
       '</script>' \
       '</body></html>'


win = Gtk.Window()
win.connect("delete-event", Gtk.main_quit)
win.set_default_size(600,400)

webview = SimpleBrowser()
webview.show_html(html)
box = Gtk.Box.new(Gtk.Orientation.VERTICAL, 0)
button = Gtk.Button(label="Python Show Values")
button.connect("clicked", webview.get_element_values, 'chktst')

win.add(box)
box.pack_start(webview, True, True, 0)
box.pack_start(button, False, False, 1)

win.show_all()

Gtk.main() 

在Debian Stretch中,您需要从backport安装软件包:

In Debian Stretch you need to install the packages from backports:

sudo apt-get install -t stretch-backports gir1.2-javascriptcoregtk-4.0 gir1.2-webkit2-4.0 libjavascriptcoregtk-4.0-18 libwebkit2gtk-4.0-37  libwebkit2gtk-4.0-37-gtk2

现在,WebKit2.JavascriptResult.get_js_value()将返回一个JavaScriptCore.Value对象。我将对此进行进一步测试,并在找到答案后将其发回。

Now, WebKit2.JavascriptResult.get_js_value() will return a JavaScriptCore.Value object. I will test this further and post back an answer if I find one.

检查此错误报告: https://bugs.webkit.org/show_bug.cgi?id=136989

推荐答案

  self.webView.run_javascript("document.documentElement.outerHTML", None, self.on_file_save_finish, None)

def on_file_save_finish(self, webview, result, user_data=None):
  document_html = self.webView.run_javascript_finish(result).get_js_value().to_string()

这在KeiEditor.py中使用

This is used in KeiEditor.py here

https://github.com/orchardguides/Kei-HTML-Editor

这篇关于WebKit2和DomDocument / JavaScriptCore(Python3)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆