Bug 140210

Summary: Provide public WKWebView API for find-in-page
Product: WebKit Reporter: Eugene But <eugenebut>
Component: WebKit APIAssignee: Nobody <webkit-unassigned>
Status: NEW ---    
Severity: Normal CC: ajuma, craig, fred.wang, keiya.s.0210, stefan, stuartmorgan, thebnich, thegreenfrog, thorton
Priority: P2 Keywords: InRadar
Version: 528+ (Nightly build)   
Hardware: iPhone / iPad   
OS: All   

Description Eugene But 2015-01-07 14:25:31 PST
Summary:
WKWebView appears to have private API for doing a native implementation of find in page (_WKFindDelegate, _findString:, etc.). Chrome for iOS currently has to implement find-in-page via JS, which is significantly slower, and has to alter the DOM for display which has the potential to cause web compatibility issues. The ability to use a native Find API would make the features better for users, since it would be faster and more reliable.

Steps to Reproduce:
Attempt to implement find in page.

Expected Results:
Access to the apparently-existing API.

Actual Results:
No way to do it except re-implementing it from scratch in JS.
Comment 1 Eugene But 2015-01-07 16:08:36 PST
Radar ID: 17581332
Comment 2 Frédéric Wang (:fredw) 2018-11-15 08:20:50 PST
Yes, the API was added for testing purpose. When you say JS, you mean you have your own pure HTML5 implementation? I think you can rely on (proprietary) JS APIs like window.find or execCommand('FindString') to use more native implementation, but they probably have some bugs (e.g. bug 163911). See also https://github.com/whatwg/html/issues/3539
Comment 3 Eugene But 2018-11-15 08:26:14 PST
By JS I mean that Chrome for iOS performs search on the page by calling -[WKWebView evaluateJavaScript:completionHandler:]. The script searches for keywords, highlights them, scrolls the page.


Safari has "Find On Page" feature (Share -> Find On Page) and it seems like that feature actually uses _findString:.
Comment 4 Frédéric Wang (:fredw) 2018-11-15 08:29:26 PST
(In reply to Eugene But from comment #3)
> Safari has "Find On Page" feature (Share -> Find On Page) and it seems like
> that feature actually uses _findString:.

OK, IIRC window.find or document.execCommand('FindString') share some logic with the Find UI.
Comment 5 Eugene But 2018-11-15 08:47:28 PST
Thank you, Frédéric! We will check window.find and document.execCommand('FindString').
Comment 6 thegreenfrog 2018-11-29 14:16:06 PST
Hi Frederic!

I tried out window.find and only was able to get it to highlight the results using document.execCommand('HiliteColor', false, 'yellow'). But I was unable to get it to scroll to results outside of the screen. So my question is two-fold:

1. Does window.find actually scroll & highlight, or is that a safari addition on top?
2. Are you aware of helpful ways to scroll to a given selected content (which is what window.find does)
Comment 7 Frédéric Wang (:fredw) 2018-11-29 21:17:41 PST
(In reply to thegreenfrog from comment #6)
> Hi Frederic!
> 
> I tried out window.find and only was able to get it to highlight the results
> using document.execCommand('HiliteColor', false, 'yellow'). But I was unable
> to get it to scroll to results outside of the screen. So my question is
> two-fold:
> 
> 1. Does window.find actually scroll & highlight, or is that a safari
> addition on top?
> 2. Are you aware of helpful ways to scroll to a given selected content
> (which is what window.find does)

Regarding 1. window.find or document.execCommand('FindString') are nonstandard.  Safari does scroll/highlight but I don't know for other browsers. Discussion is at https://github.com/whatwg/html/issues/3539

Regarding 2., window.find should do the scroll, if it does not it is a bug :-) I already mentioned bug 163911 on mobile safari.
Comment 8 Keiya Sasaki 2022-05-22 06:45:29 PDT
The webkit2gtk provide FindController to highlight search text but the wkwebview doesn’t provide this feature.
Is there a reason why the wkwebview doesn’t provide it?
Thank you.