向WebBrowserComponent的C ++回调功能添加JS

时间:2018-07-19 06:26:06

标签: c++ macos webkit juce

JUCE在WebBrowserComponent中使用本机macOS WebView,因此从理论上讲这是完全可能的。我在JUCEWebClickDetector类中添加了(我相信是)适当的方法,该类现在如下所示:

        DownloadClickDetectorClass()  : ObjCClass<NSObject> ("JUCEWebClickDetector_")
    {
        addIvar<WebBrowserComponent*> ("owner");

        addMethod (@selector (webView:decidePolicyForNavigationAction:request:frame:decisionListener:),
                   decidePolicyForNavigationAction, "v@:@@@@@");
        addMethod (@selector (webView:decidePolicyForNewWindowAction:request:newFrameName:decisionListener:),
                   decidePolicyForNewWindowAction, "v@:@@@@@");
        addMethod (@selector (webView:didFinishLoadForFrame:), didFinishLoadForFrame, "v@:@@");
        addMethod (@selector (webView:didFailLoadWithError:forFrame:),  didFailLoadWithError,  "v@:@@@");
        addMethod (@selector (webView:didFailProvisionalLoadWithError:forFrame:),  didFailLoadWithError,  "v@:@@@");
        addMethod (@selector (webView:willCloseFrame:), willCloseFrame, "v@:@@");
        addMethod (@selector (webView:runOpenPanelForFileButtonWithResultListener:allowMultipleFiles:), runOpenPanel, "v@:@@", @encode (BOOL));

        addMethod (@selector (isSelectorExcludedFromWebScript:), webview_is_selector_excluded_from_web_script, "c@::", @encode (BOOL));
        addMethod (@selector (webScriptNameForSelector:), webview_webscript_name_for_selector, "c@::" );
        addMethod (@selector (webView:didClearWindowObject:forFrame:), webview_did_clear_window_object, "v@:@@@" );
        addMethod (@selector (invoke:), webview_external_invoke, "v@:@" );

        registerClass();
    }

    static void setOwner (id self, WebBrowserComponent* owner)   { object_setInstanceVariable (self, "owner", owner); }
    static WebBrowserComponent* getOwner (id self)               { return getIvar<WebBrowserComponent*> (self, "owner"); }

    static BOOL webview_is_selector_excluded_from_web_script(id self, SEL cmd,
                                                             SEL selector) {
        return selector != @selector(invoke:);
    }

    static NSString *webview_webscript_name_for_selector(id self, SEL cmd,
                                                         SEL selector) {
        return selector == @selector(invoke:) ? @"invoke" : nil;
    }

    static void webview_did_clear_window_object(id self, SEL cmd, id webview,
                                                id script, id frame) {
        [script setValue:self forKey:@"external"];
    }

    static void webview_external_invoke(id self, SEL cmd, id arg) {
        std::cout << "JS Click" << std::endl;
    }
   ...

当我运行带有某些断点的应用程序时,它将在webview_did_clear_window_object函数上中断,但是当我单击页面上的HTML按钮时没有任何反应。据我所知,没有其他函数被调用(不会在断点处中断)。

这是我第一次直接修改任何juce模块,因此我很可能在这里遗漏了一些简单的东西,但是我似乎无法弄清这里是什么问题。

作为参考,这是我正在加载的HTML文件:

<!DOCTYPE html>
<html>
<body>

<button type="button" onclick="myFunction()">Click Me!</button>

<script>
function myFunction() {
    window.external.invoke();
}
</script>

</body>
</html>

0 个答案:

没有答案