atesh/XKit — pull request #94 by beiju (2013-07-06 22:42:40+00:00)
Title: Safari 7 Bug Fixes

I took a look at XKit in Safari 7 and it looks like the reason for the problems was that Safari 7 is now much more strict about enforcing what kind of data can be passed as a message body. There were two changes in particular:

* Functions passed as part of an object either cause the message to not be sent or are set to `undefined` (debugging was pretty confusing and to be honest I forgot which happens). To preserve behavior I overwrite the 'onload' and 'onerror' keys in lines 90–92 of bridge.js. Note that if another callback is added, it will need to be manually overwritten there. It would be possible to check for functions dynamically, feel free to implement that if you think it would work better. The solution to the second problem would not work for this because it would set the 'onload' and 'onerror' keys to `undefined`, which would cause the callback in global.html to not execute them.
* An attempt to send an XMLHttpResponse object (and probably other types of objects as well) in the body of a message, even if it's a property within another object, throws a cryptic `DataCloneError: DOM Exception 25: An object could not be cloned` error. The solution to this is to convert it into a plain object by encoding and decoding it with JSON. Since JSON doesn't support anything Safari wouldn't allow in a message, and it deals with that by throwing away the property and not throwing an error, this should be future-proof. Note however that this may reduce compatibility. Any property that isn't a JSON-supported type, including functions and dates, will be discarded. If any extensions try to read one of those properties they may throw errors or show strange behavior when they don't find it. I have *not* tested *any* extensions for compatibility. The installation process and the preferences seem to work fine, but I did not test extensively nor did I test in Safari 6. However, if this fix creates any problems, they will be visible in Safari 6 as well as 7.

I've also seen a fair amount of general instability. Sometimes on page refresh XKit won't initialize, or it won't be able to load the extension gallery, or an extension download will never end. I can't seem to find a cause for this, so I'm going to attribute it to Safari 7 being an early beta. If you have more information, let me know and I'll take a look.

I also updated the build script to work with Safari's Extension Builder. To use it now, specify the build target (currently limited to 'safari', 'chrome', or 'all') as a parameter. (e.g. `./build.sh safari`). To use it with Safari's Extension Builder, open the Extension Builder from the Develop menu, click the + in the bottom corner, and choose the .safariextension folder. Click 'Install' to install it from the first time. Every time you make a change, re-run the build script and click 'Reload' in the Extension Builder to reload the extension and any tabs you have open to tumblr.com.