从Cocoa中的WebView获取源HTML [英] Getting source HTML from a WebView in Cocoa

查看:160
本文介绍了从Cocoa中的WebView获取源HTML的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在做一个OS X程序,用户在WebView中做一些光WYSIWYG HTML编辑。作为新的Cocoa和WebKit编程,我绝对不知道如何从WebView获取选定的文本 - 意图是采取用户选择,在文本周围添加HTML代码(如div或span),并替换所选的文本与修改后的代码。这是如何实现的?

I'm working on a OS X program where the user does some light WYSIWYG HTML editing in a WebView. Being new to programming with Cocoa and WebKit, I have absolutely no idea how to get selected text from a WebView - the intention being to take what the user selected, add HTML code (like div's or span's) around the text, and replace the selected text with the modified code. How can this be accomplished?

我目前正在使用MacRuby编程这个项目,但我也很感谢Objective-C程序员的帮助。谢谢!

I'm currently programming this project with MacRuby, but I'd appreciate help from Objective-C programmers as well. Thank you!

推荐答案

您可以要求 WebView code> -selectedDOMRange ,你会得到一个 DOMRange 对象。您可以使用此对象来查找所选内容。 DOMRange 像所有WebKit DOM对象一样,是一个标准W3C DOMRange对象的Objective-C表示,参见 DOMRange.h

You can ask for the WebView's -selectedDOMRange and you'll get a DOMRange object back. You can use this object to find out what is selected. DOMRange, like all WebKit DOM objects, is an Objective-C representation of a standard W3C DOMRange object, see DOMRange.h for what methods/properties it supports.

然后,您可以使用 -replaceSelectionWithMarkupString:替换当前选择, -replaceSelectionWithText: -replaceSelectionWithNode: WebView 的方法。

You can then replace the current selection using the -replaceSelectionWithMarkupString:, -replaceSelectionWithText: or -replaceSelectionWithNode: methods of WebView.

这篇关于从Cocoa中的WebView获取源HTML的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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