在 Cocoa WebView 中防止 Flash [英] Prevent Flash in Cocoa WebView

查看:19
本文介绍了在 Cocoa WebView 中防止 Flash的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的 Cocoa 应用程序中,我想阻止 Flash 在 WebView 中加载,并让用户决定是否应为每个页面显示 Flash.(这与通过 ClickToFlash 插件或 Safari 扩展程序已经可用的行为相同.由于许可问题,捆绑任何这些扩展程序可能不是一种选择.)

In my Cocoa app, I want to prevent Flash from loading in a WebView, and let the user decide if the Flash should be shown for each page. (That's the same behavior already available through the ClickToFlash plugin or Safari extension. Bundling any of those extensions is probably not an option because of licensing issues.)

不幸的是,我试图避免的大部分 Flash 都是由嵌入式 JavaScript 生成的,这些 JavaScript 专门设计用于防止简单的 Flash 阻塞,因此我无法过滤原始 HTML 以包含 Flash 对象.

Unfortunately most of the Flash I'm trying to avoid is generated from embedded JavaScript specifically designed to prevent easy flash blocking, so I cannot filter the raw HTML for inclusion of Flash objects.

此外,我无法为我的 WebView 禁用 JavaScript,因为当 JavaScript 关闭时,我想要显示的页面看起来完全不同.

Also, I cannot disable JavaScript for my WebView, as the page I want to display looks completely different when JavaScript is turned off.

在执行 JavaScript 之后但在加载 Flash 插件之前,我可以使用通知/挂钩来修改页面 DOM 吗?

Is there a notification/hook I can use to modify the page DOM after JavaScript has been executed, but before the Flash plugin is loaded?

或者我应该追求不同的方向?

Or should I pursue a different direction?

谢谢,伊利亚

推荐答案

理想情况下,您只需定义自己的 WebKit 插件来处理 application/shockwave-flash MIME 类型并制作您的插件-什么都不做.

Ideally, you would just define your own WebKit plug-in that handles the application/shockwave-flash MIME type and make your plug-in do nothing.

然而,遗憾的是没有办法控制多个注册相同 MIME 类型的 WebKit 插件的优先级.WebKit 插件的加载顺序是完全随机的,因此您不能保证您的插件会处理 Flash 对象而不是 Flash 插件.

However, there is unfortunately no way to control the priority of multiple WebKit plug-ins that all register for the same MIME type. The loading order of WebKit plug-ins is totally random and arbitrary, so you cannot guarantee that your plug-in will handle the Flash object instead of the Flash plug-in.

我发现的唯一解决方法是子类化 WebView 并覆盖私有方法 -_pluginForMIMEType: 像这样:

The only way around this that I've found is to subclass WebView and override the private method -_pluginForMIMEType: like so:

@class WebBasePluginPackage;

@interface WebView ( MyFlashPluginHack )
- (WebBasePluginPackage *)_pluginForMIMEType:(NSString *)MIMEType;
@end

@implementation MyWebView

- (WebBasePluginPackage *)_pluginForMIMEType:(NSString *)MIMEType
{
    if ( [MIMEType isEqualToString:@"application/x-shockwave-flash"] )
    {
        return [super _pluginForMIMEType:@"application/my-plugin-type"];
    }
        else
    {
        return [super _pluginForMIMEType:MIMEType];
    }
}

@end

然后你只需要创建一个自定义的 WebKit 插件来处理application/my-plugin-type"并且让该插件什么都不做.

Then you just need to create a custom WebKit plugin to handle "application/my-plugin-type" and have that plug-in do nothing at all.

这篇关于在 Cocoa WebView 中防止 Flash的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆