Re: Getting started with WebKit plugins
Re: Getting started with WebKit plugins
- Subject: Re: Getting started with WebKit plugins
- From: Christopher Drum <email@hidden>
- Date: Wed, 11 Jan 2006 09:49:43 -0800
Thank you for the suggestions.
(7) Yes, I just discovered today (after breaking my plugin) that I
need + (BOOL)isSelectorExcludedFromWebScript:(SEL)aSelector. Thank you.
(9) I also just discovered the embed name last night (as I started
familiarizing myself with Javascript more). Thank you for that as
well. The syntax is "name=".
(10) This makes sense, and I feel silly for not thinking about the
console for the log file. For some reason it just didn't occur to me
that NSLog was sending it's text anywhere, in the context of a
plugin. Of course, it must and I thank you for reminding me of the fact.
Christopher
On Jan 11, 2006, at 9:12 AM, Rudi Sherry wrote:
Glad that we could be of help. I have a few other suggestions:
(7) You do need + (BOOL)isSelectorExcludedFromWebScript:(SEL)
aSelector, and should use it. Your object is a subclass of NSView,
remember, so any NSView method is also exposed unless you
specifically exclude it (and so are all the methods of all the
objects to which NSView is a subclass, etc). I recently went
through a Security Audit of my code so I'm particularly sensitive
to white-list / black-list issues ;)
(9) You can also give the embed a name (I forget whether it's
"name=" or "id="), and then do document.embeds['objectname']. This
way, if your code is put into a page with more than one embed, you
won't have to remember to adjust the index, as long as you have a
unique name.
(10) NSLog, the Cocoa equivalent of printf, is also a handy tool
for watching things going on, and then it doesn't interfere with
the workings by putting up alerts. These go to the console.
-- Rudi
On Jan 10, 2006, at 10:02 PM, Christopher Drum wrote:
Many thanks to Severin and Rudi who wrote with help on my struggle
to understand WebKit plugins. I got my plugin to work like a
champ. As documentation for future research (because I'm sure I'll
forget something, and I'll probably search the lists for the
answer again), here's what I did.
1. Create a new project in Xcode under New Project > Bundle >
Cocoa Bundle.
2. To the project's Classes folder I added a new file, choosing
the template for Objective-C NSView subclass
3. I changed the Info.plist file as recommended by Severin and Rudi.
a. CFBundlePackageType set to WBPL
b. NSPrincipalClass set to match the name of the new
NSView subclass I created in step 2.
c. WebPluginDescription set to something readable that
makes sense for the project
d. WebPluginMIMETypes - This part confused me as a MIME
newbie. I see now that this is where you register what types of
file extension calls in the webpage will trigger the use of the
custom plugin. I created my own custom MIME type that will only be
used internally with a corresponding custom extension to represent
that "filetype". The key to my understanding this was Rudi's point
that Safari loads my plugin because it "matched an incoming MIME
type to my Info.plist setting".
e. WebPluginName again set to something readable that
makes sense for the project
4. In mimicking how the Apple samples work, I created an instance
variable of type NSDictionary named *arguments to hold the
arguments passed to the class by the web browser, and a
corresponding setArguments: method.
5. Implemented the one method that is 100% necessary for WebKit
plugins to work
+(NSView *)plugInViewWithArguments:(NSDictionary *)arguments;
6. Implemented the methods I want my plugin to call.
7. Implemented an optional method (which, upon reading the docs
more closely, isn't necessary for my simple test plugin which only
has two methods and I want both exposed anyway)
+ (BOOL)isSelectorExcludedFromWebScript:(SEL)aSelector
8. Implemented -(id)objectForWebScript, returning self
Now, I've only ever sudied C++ and Objective-C / Cocoa (with a
teeny bit of Java and HTML thrown in). Not understanding how the
HTML and Javascript trigger WebKit plugins and obtain an instance
of my plugin's class was a big mystery to me.
8. Following Apple's sample code, I put an <embed> whose type is
of the same custom MIME type I created in step 2d and whose source
(src) references a nonexistent file with an extension that matches
the one I created in step 2d. This allowed me to simply trigger
the plugin to verify something was happening.
9. Using JavaScript's document.embeds[0] command, I was able to
obtain the instance of my plugin. With that in hand, calling the
Javascript-accessible functions was simplicity itself, using dot
notation and underscores (in place of Obj-C colons). I understand
now that I could make the methods I exposed more friendly to use
by implementing webScriptNameForSelector: in my NSView subclass.
10. Finally, I found it useful to know if something was happening,
say on plugin initialization or any other method that didn't do
something obvious. I put NSAlert calls in each method so I could
get visible confirmation that the plugin loaded and such. At one
point I found that the plugin was initializing, but nothing
further happened. The NSAlerts helped me track down a bug in my
HTML file that wasn't utilizing the plugin properly.
Thanks again for everyone's help! The explanations helped me make
sense of what I was reading in the Apple docs, and it's all much
clearer to me now.
Christopher Drum
http://homepage.mac.com/christopherdrum
_______________________________________________
Do not post admin requests to the list. They will be ignored.
Cocoa-dev mailing list (email@hidden)
Help/Unsubscribe/Update your Subscription:
This email sent to email@hidden