Getting source HTML from a WebView in Cocoa

穿精又带淫゛_ 提交于 2019-11-30 09:36:49

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.

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

Apple recommends using this snippet here to retrieve the HTML content of a WebKit WebView:

[(DOMHTMLElement *)[[[webView mainFrame] DOMDocument] documentElement] outerHTML];

If you want the HTML interpreted as plain text, you can use:

[(DOMHTMLElement *)[[[webView mainFrame] DOMDocument] documentElement] outerText];

UIWebView lets you run arbitrary JavaScript:

[webview stringByEvaluatingJavaScriptFromString:@"document.getElementsByTagName('html')[0].innerHTML"]
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!