Skip to content

Instantly share code, notes, and snippets.

@boozook
Last active April 10, 2016 22:04
Show Gist options
  • Save boozook/b9cc28ee20cac95706d0bca1cedcbed9 to your computer and use it in GitHub Desktop.
Save boozook/b9cc28ee20cac95706d0bca1cedcbed9 to your computer and use it in GitHub Desktop.
IPFS / IPNS URL-Scheme Handler App (for Mac OS X Safari only)
  1. Open Ipfs.url.scheme.handler.scpt in Apple Script Editor.app;
  2. Compile Ipfs.url.scheme.handler.scpt -> to application;
  3. Open Ipfs.url.scheme.handler.app/Contents/Info.plist in text editor;
  4. Add next nodes:
<!-- URL-Schemes support: -->
<key>CFBundleURLTypes</key>
<array>
	<dict>
		<key>CFBundleURLName</key>
		<string>IPFS Handler</string>
		<key>CFBundleURLSchemes</key>
		<array>
			<string>ipfs</string>
			<string>ipns</string>
			<string>fs</string>
		</array>
	</dict>
</array>
<!-- Our app will not catch the focus of the browser. -->
<key>LSBackgroundOnly</key>
<true/>
<!-- see: https://developer.apple.com/library/ios/documentation/General/Reference/InfoPlistKeyReference/Articles/LaunchServicesKeys.html -->
<key>LSUIElement</key>
<true/>
  1. Open next links in Safari:
  • ipfs://QmYw.../readme/fs://QmYw.../readme
  • ipns://ipfs.io/.../fs://ipfs.io/...

Super Great Features:

  • Support ipfs:// & ipfs:/ - resolve Qm.... Base58 wrapped in Multihash;
  • Support ipns:// & ipns:/ - resolve DNS;
  • Support fs:// & fs:/ - resolve both (DNS & Hash)

Links:

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
<key>CFBundleAllowMixedLocalizations</key>
<true/>
<key>CFBundleDevelopmentRegion</key>
<string>English</string>
<key>CFBundleExecutable</key>
<string>applet</string>
<key>CFBundleIconFile</key>
<string>applet</string>
<key>CFBundleIdentifier</key>
<string>com.apple.ScriptEditor.id.Ipfs-url-scheme-handler</string>
<key>CFBundleInfoDictionaryVersion</key>
<string>6.0</string>
<key>CFBundleName</key>
<string>Ipfs.url.scheme.handler</string>
<key>CFBundlePackageType</key>
<string>APPL</string>
<key>CFBundleShortVersionString</key>
<string>1.0</string>
<key>CFBundleSignature</key>
<string>aplt</string>
<key>LSMinimumSystemVersionByArchitecture</key>
<dict>
<key>x86_64</key>
<string>10.6</string>
</dict>
<key>LSRequiresCarbon</key>
<true/>
<key>WindowState</key>
<dict>
<key>bundleDividerCollapsed</key>
<true/>
<key>bundlePositionOfDivider</key>
<real>0.0</real>
<key>dividerCollapsed</key>
<false/>
<key>eventLogLevel</key>
<integer>2</integer>
<key>name</key>
<string>ScriptWindowState</string>
<key>positionOfDivider</key>
<real>421</real>
<key>savedFrame</key>
<string>350 18 700 672 0 0 1440 877 </string>
<key>selectedTab</key>
<string>result</string>
</dict>
<!-- URL-Schemes support: -->
<key>CFBundleURLTypes</key>
<array>
<dict>
<key>CFBundleURLName</key>
<string>IPFS Handler</string>
<key>CFBundleURLSchemes</key>
<array>
<string>ipfs</string>
<string>ipns</string>
<string>fs</string>
</array>
</dict>
</array>
<!-- Our app will not catch the focus of the browser. -->
<key>LSBackgroundOnly</key>
<true/>
<!-- It for the same reason. -->
<!-- see: https://developer.apple.com/library/ios/documentation/General/Reference/InfoPlistKeyReference/Articles/LaunchServicesKeys.html -->
<key>LSUIElement</key>
<true/>
</dict>
</plist>
-- https://gist.github.com/fzzr-/b9cc28ee20cac95706d0bca1cedcbed9
on open location theURL
set theNewURL to ""
set theGateway to "https://gateway.ipfs.io/"
set oldDelims to AppleScript's text item delimiters
-- xxx: better use `env bash -l`
set isIpfsRunning to length of (do shell script "bash -lc " & quoted form of "ipfs id -f='<addrs>'") > 0
-- todo: display dialog "Wanna activate IPFS daemon?"
if (isIpfsRunning) then
set theGateway to "http://127.0.0.1:8080/"
end if
-- Resolve Hash part --
if (theURL starts with "ipfs:") then
-- ipfs:/QmQZB43ePCwDsow74gehT8v74M2tKtTs9FCbH4wpoUksfF
set AppleScript's text item delimiters to "ipfs:"
set thePath to item 2 of the text items of theURL
set theNewURL to theGateway & "ipfs" & thePath
else if (theURL starts with "fs:/Qm") or (theURL starts with "fs://Qm") then
-- fs:/QmQZB43ePCwDsow74gehT8v74M2tKtTs9FCbH4wpoUksfF
set AppleScript's text item delimiters to "fs:"
set thePath to item 2 of the text items of theURL
set theNewURL to theGateway & "ipfs" & thePath
-- Resolve DNS part --
else if (theURL starts with "fs:") then
-- fs:/ipfs.io
set AppleScript's text item delimiters to "fs:"
set thePath to item 2 of the text items of theURL
set theNewURL to theGateway & "ipns" & thePath
else if (theURL starts with "ipns:") then
-- ipns:/ipfs.io
set AppleScript's text item delimiters to "ipns:"
set thePath to item 2 of the text items of theURL
set theNewURL to theGateway & "ipns" & thePath
end if
set AppleScript's text item delimiters to oldDelims
tell application "Safari"
tell window 1
--set current tab to (make new tab with properties {URL:theNewURL})
set properties of current tab to {URL:theNewURL}
activate
end tell
end tell
return theNewURL
end open location
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment