开发者

Disable a WebKit WebView

开发者 https://www.devze.com 2022-12-13 20:25 出处:网络
Is it possible to disable all user interaction with a WebView, apart from scrolling? I want the user to be able to see the page (and possibly select things),开发者_开发问答 but not click links/right c

Is it possible to disable all user interaction with a WebView, apart from scrolling? I want the user to be able to see the page (and possibly select things),开发者_开发问答 but not click links/right click/refresh/focus form fields/trigger UI DOM events (onclick etc).

I see on this question I can disable right click and selection, but that doesn't help with the form elements and navigation sending DOM events.


You could subclass NSWindow and set your subclass as the window of the WebView. You can then control which events are sent to the WebView by detecting what sort of control is being affected by the mouse event.

This is pretty brute force but will totally disable any mouse events, including rollovers etc:

@interface WebViewEventKillingWindow : NSWindow 
{
    IBOutlet WebView* myWebView;
}
@end

@implementation WebViewEventKillingWindow
- (void)sendEvent:(NSEvent*)event
{
    NSView* hitView;
    switch([event type])
    {
        case NSScrollWheel:
        case NSLeftMouseDown:
        case NSLeftMouseUp:
        case NSLeftMouseDragged:
        case NSMouseMoved:
        case NSRightMouseDown:
        case NSRightMouseUp:
        case NSRightMouseDragged:
            hitView = [myWebView hitTest:[event locationInWindow]];
            if([hitView isDescendantOf:myWebView] && 
                         !([hitView isKindOfClass:[NSScroller class]] || 
                             [hitView isKindOfClass:[NSScrollView class]]))
            {
                return;
            }
            break;
        default:
            break;
    }
    [super sendEvent:event];
}
@end


One option (not really the best...) would be to wrap the WebView inside a ScrollView that you create yourself, and then disable all user interaction with the web view entirely. I imagine you would adjust the web view's frame so that the entire page was visible, and then make it the content view of a scroll view.

You may also be able to subclass WebView and intercept clicks to reject certain actions, but I don't have any experience with this, and I imagine it would be difficult to differentiate some actions, such as selection, from others (especially the contextual menu).

Hope that helps - good luck!


You could probably do this via javascript (iterate through all links, forms, etc) on the page and deactivate them, using -[UIWebView stringByEvaluatingJavaScriptFromString:].

0

精彩评论

暂无评论...
验证码 换一张
取 消