How to render HTML from an NPAPI plugin in Safari
I've been writing a Mac NPAPI-based browser plugin to convert a file of custom mimetype (say, "application/x-foo") into an HTML representation, which can then be viewed directly in a browser. These files are usually directly served, so I'm more concerned about supporting full-page viewing, rather than embedded views via <object>
tags.
On Firefox, this has been relatively simple: I make a call to NPN_NewStream
with the text/html
mimetype, write the converted HTML into the stream, and then clean it up with NPN_DestroySteam
. The browser automatically handles the request for a new HTML stream and renders the given HTML into a tab or window. Pretty stand开发者_如何转开发ard.
For Safari, though, NPN_NewStream
does not appear to be implemented (and I did check the WebKit source code). Previously, I was able to use the WebKitPlugin API. With Safari 5.1, this API is gone.
I thought I would be able to create a WebView in a drawing event handler, like this:
NSRect rect = NSMakeRect(0, 0, obj->window.width, obj->window.height);
//...
WebView* webView = [[WebView alloc] initWithFrame:rect frameName:nil groupName:nil];
[[webView mainFrame] loadHTMLString:@"<html><head><title>This is a message from my plug-in!</title></head><body><p><strong>This is a message from my plug-in!</strong></p></body>/html>" baseURL:[NSURL URLWithString:@"http://example.com"]];
[webView drawRect:rect];
and see it in browser. But all that does is render a gray screen with no content, as if not drawn. If I replace the WebView
with an NSTextView
and set its string to the HTML, everything draws just fine, but of course the HTML is not rendered by an NSTextView
.
My question boils down to: is there a good way to render some HTML into a Safari window from an NPAPI plugin? Or if that won't work, into a Google Chrome window? Or some other approach that lets me handle a custom MIME type and display some HTML representation of it?
Honestly, I'm surprised that your newstream approach works on Firefox; that's a new one I've never heard of. If you have to do the drawing yourself, you are limited to either using CoreGraphics with a CGContext or CoreAnimation with a CALayer. In other words, you don't get a NSView, so you can't directly render a WebView into it.
That said, you could put the webview in an offscreen context and render it to your CGContext, then proxy the events; this seems a little complicated for what you're trying to do, but I don't know of another way to do it.
There is an experimental library in FireBreath that does this; scroll wheel isn't implemented, but the other events work, including mouse drag and keyboard. There are still a few strange things with it when using form events, so whether or not this would work for you depends on what content you'll be displaying.
If you're interested in trying to use FireBreath for this you can drop into the FireBreath chat room during business hours (GMT-0600) and I'm usually around and can discuss it with out.
精彩评论