Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
469 views
in Technique[技术] by (71.8m points)

python-3.7 - 如何保存已编辑的Webview结果?(How to save edited webview result?)

Situation description: Python 3.7, GTK 3.0, PyGObjects 3.34.0 Webkit2 4.0

(情况说明:Python 3.7,GTK 3.0,PyGObjects 3.34.0 Webkit2 4.0)

I have a dialog window, with GtkNotebook containing 2 tabs.

(我有一个对话框窗口,其中包含2个标签的GtkNotebook。)

1. tab contains editable Webkit webview, the 2. tab contains textview.

(1.选项卡包含可编辑的Webkit Web视图,2.选项卡包含textview。)

One of the arguments provided in class consrtructor is valid HTML snippet as string variable

(类构造函数中提供的参数之一是有效的HTML代码段,它是字符串变量)

What I would like to get as a result, is that any changes made in any window, are automatically reflected in other.

(我想得到的结果是,在任何窗口中进行的任何更改都会自动反映在其他窗口中。)

Current problem: Using solution provided here , any previous changes that were made in webview are discarded upon switching the notepad tabs.

(当前问题:使用此处提供的解决方案,在切换记事本选项卡时,在webview中进行的任何先前更改都将被丢弃。)

Debugging shows that html obtained with aforementioned call, does not contain changes.

(调试显示,通过上述调用获得的html不包含更改。)

Any ideas what might be missing in the logic or handling itself?

(任何想法在逻辑或处理本身中可能缺少什么?)

For reference, the code for the dialog is as follows:

(作为参考,对话框代码如下:)

# -*- coding: utf-8 -*-
import gi
gi.require_version('Gtk', '3.0')
gi.require_version('WebKit2', '4.0')
from gi.repository import Gtk, WebKit2



class DescriptionDialog:
    def __init__(self, *args):
# GTK Builder
        self._builder = args[0]
        self._builder.add_from_file("UI/GUI/description.glade")
        self.dialog = self._builder.get_object("descriptionDialog")
        self._textView = self._builder.get_object("textview1")
        self.webViewContainer = self._builder.get_object("WebViewContainer")
        self.browserHolder = WebKit2.WebView()
        self.browserHolder.set_editable(True)
        self.webViewContainer.add(self.browserHolder)
        self.browserHolder.show()
# valid html snippet, held as string
        self.__buffer_orig__ = args[2]
        self.buffer = args[2]
        self.browserHolder.load_html(self.buffer)
        self._builder.connect_signals(
            {
                "onDialogClose": self.onDialogClose,
                "pageChangeNotebook": self.onPageChange
            })
        self.dialog.set_transient_for(self._builder.get_object("MainWindow"))
        self.textBuffer = self._builder.get_object("textbuffer1")
        self.textBuffer.set_text(self.buffer)
        self.dialog.show()

    def onDialogClose(self, handler):
        self.dialog.hide()

    def onPageChange(self, notebook=None, scrolledWindow=None, pageNumber=0):
        if pageNumber == 0:
            self.buffer = self.textBuffer.get_text(self.textBuffer.get_start_iter(), self.textBuffer.get_end_iter(), True)
            self.browserHolder.load_html(self.buffer)
        if pageNumber == 1:
            self.browserHolder.get_main_resource().get_data(None, self.getDataFromResource, None)

    def getDataFromResource(self, resource, result, userData=None):
# Changed html is not returned here
        self.buffer = str(resource.get_data_finish(result).decode("utf-8"))
        self.textBuffer.set_text(self.buffer)
  ask by Kristaps Livmanis translate from so

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

For other internet users finding this thread.

(对于其他互联网用户,可以找到此线程。)

Currently, at the given versions, this is the working result that I have come up with.

(目前,在给定的版本中,这是我得出的工作结果。)

Main idea with this implementation is following - use the WebView enabled Javascript engine to obtain the contents of a <body> tag.

(此实现的主要思想如下-使用支持WebView的Javascript引擎获取<body>标记的内容。)

Then, parse the Javascript result to use this value later on.

(然后,解析Javascript结果以稍后使用此值。)

def onPageChange(self, notebook=None, scrolledWindow=None, pageNumber=0):
    if pageNumber == 0:
        self.buffer = self.textBuffer.get_text(self.textBuffer.get_start_iter(), self.textBuffer.get_end_iter(), True)
        self.browserHolder.load_html(self.buffer)
    if pageNumber == 1:
        # use JavaScript to get the html contained in rendered body tag
        script = "document.body.innerHTML;"
        # Execute JavasScript via WebKit2.WebView bindings.
        # Result can be obtained asynchronously, via callback method
        self.browserHolder.run_javascript(script, None, self.getJSStatus, None)

def getJSStatus(self, resource, result, userData=None):
    # Sample adapted and simplified from here:
    # https://lazka.github.io/pgi-docs/#WebKit2-4.0/classes/WebView.html#WebKit2.WebView.run_javascript_finish
    # Get the JavaScript result
    data = self.browserHolder.run_javascript_finish(result)
    # Get value from result, and convert it to string
    self.buffer = data.get_js_value().to_string()
    self.textBuffer.set_text(self.buffer)

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

1.4m articles

1.4m replys

5 comments

57.0k users

...