|
-- 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 |