@@ -10,22 +10,74 @@ import Combine
1010import WebKit
1111
1212private let script = """
13+ (function() {
14+ function debounce(func, wait, immediate) {
15+ // 'private' variable for instance
16+ // The returned function will be able to reference this due to closure.
17+ // Each call to the returned function will share this common timer.
18+ var timeout;
19+
20+ // Calling debounce returns a new anonymous function
21+ return function() {
22+ // reference the context and args for the setTimeout function
23+ var context = this,
24+ args = arguments;
25+
26+ // Should the function be called now? If immediate is true
27+ // and not already in a timeout then the answer is: Yes
28+ var callNow = immediate && !timeout;
29+
30+ // This is the basic debounce behaviour where you can call this
31+ // function several times, but it will only execute once
32+ // [before or after imposing a delay].
33+ // Each time the returned function is called, the timer starts over.
34+ clearTimeout(timeout);
35+
36+ // Set the new timeout
37+ timeout = setTimeout(function() {
38+
39+ // Inside the timeout function, clear the timeout variable
40+ // which will let the next execution run when in 'immediate' mode
41+ timeout = null;
42+
43+ // Check if the function already ran with the immediate flag
44+ if (!immediate) {
45+ // Call the original function with apply
46+ // apply lets you define the 'this' object as well as the arguments
47+ // (both captured before setTimeout)
48+ func.apply(context, args);
49+ }
50+ }, wait);
51+
52+ // Immediate mode and no wait timer? Execute the function..
53+ if (callNow) func.apply(context, args);
54+ }
55+ }
56+
57+ function _send(method, value) {
58+ webkit.messageHandlers[method].postMessage(value)
59+ }
60+
61+ var sendIn100 = debounce(_send, 100)
62+ var sendIn1000 = debounce(_send, 1000)
63+
1364 document.onselectionchange = function() {
1465 var txt = document.getSelection().toString()
1566
16- webkit.messageHandlers.onSelectionChange.postMessage( txt)
67+ sendIn1000('onSelectionChange', txt)
1768 }
1869 window.oncontextmenu = function() {
1970 var txt = document.getSelection().toString()
2071
21- webkit.messageHandlers.onContextMenu.postMessage( " txt " )
72+ sendIn100('onContextMenu', txt)
2273 }
2374 document.body.onload = function() {
24- webkit.messageHandlers.onBodyLoaded.postMessage( " txt " )
75+ sendIn100('onBodyLoaded', '' )
2576 }
2677 document.body.onkeydown = function(event) {
27- webkit.messageHandlers.onKeyDown.postMessage( event.code )
78+ sendIn100('onKeyDown', event.keyCode )
2879 }
80+ })()
2981"""
3082
3183protocol WKScriptsSetup {
@@ -34,7 +86,7 @@ protocol WKScriptsSetup {
3486}
3587
3688extension WKScriptsSetup {
37- func setupScripts( view: WKPageView , coordinator: WKCoordinator ) {
89+ func setupScripts< T : WKCoordinator > ( view: WKPageView , coordinator: T ) where T : WKScriptMessageHandler {
3890 let userContentController = view. configuration. userContentController
3991
4092 userContentController. add ( coordinator, name: " onSelectionChange " )
0 commit comments