Extract the code from any Safari Extension

If you have been using any of the new Safari Extensions you might want to peek inside them to see how they work. Fortunately this is really easy because .safariextz files are just xar archives that can be extracted using the Terminal. Here's how you do it.

Start by downloading the Safari extension. It should come in the form of a file with a .safariextz extension. If you wanted to install the extension to use it in Safari you would just double-click it, but don't do that now as the file will disappear from your Downloads folder.

Instead open Terminal, located in the Utilities folder inside the Applications folder. In the window that appears, type the following line and then press return.

cd ~/Downloads/

This should be the path to the folder where the .safariextz file is located. You will need to change this if you have saved the extension somewhere else. For example you might use cd ~/Desktop/Extensions or /Users/matt/Documents/safariextensions/ If you are new to using the Terminal, note that starting with ~/ is a shortcut to inside your user folder, and starting with / is for inside your hard drive. The cd command is for changing directory, i.e. 'moving' to a folder.

Next paste in the following line and press return.

xar -xf 'TheExtensionName.safariextz'

You need to replace TheExtensionName with the filename of the extension. The quotes around the filename are only really important if it contains spaces. If you want to avoid typing in the name, you can just drag the file into the Terminal window from the Finder and it will be entered for you.

After you press return, a new folder will appear with the same name as the extension. This folder contains all the javascript, plist and HTML files that make up the extension. You can then go to the Extension Builder in Safari and click the plus (+) button to add the extension and edit it.

Try this out with some of the extensions I've made. Feel free to use any of my code for your own extensions. If you are feeling nice, leave a link back to http://www.macosxtips.co.uk/extensions.

blog comments powered by Disqus