Mar
13
2013

A Better Chrome To Safari Bookmarklet

Posted by at

pythonista

In January, I tried to put together a bookmarklet to send the webpage currently open in Google Chrome for iOS to Apple's Safari. That turned out to be a surprisingly complex effort as Google didn't think offering an “Open In Safari” option would be a good idea, and the app's URL scheme produced some interesting results when opening and closing Chrome.

I was reminded of the bookmarklet this morning by reader @CNWLshadow, and I realized that I never posted the solution I settled with. It consists of a browser bookmarklet and a Pythonista script, and it works with just one tap.

With Pythonista 1.3, developer Ole Zorn added the possibility to open links specifically in Safari by prefixing them with safari-, therefore allowing me to create a simple bookmarklet that would:

  • Take the current URL open in Chrome;
  • Give it to Pythonista;
  • Open it in Safari.

The bookmarklet is extremely basic. It assumes a script called “ToSafari” is available in the root of Pythonista, and it sends an argument for the webpage that is currently open. You can copy the code below and add it to Chrome as a bookmark.

javascript:window.location='pythonista://ToSafari?action=run&argv='+encodeURIComponent(document.location.href);

The script is simple as well. It starts by counting the arguments sent to the script: if less than two, it will print that no URL was received; else, it will take the URL and send it to Safari by prefixing it with safari-.

# Receives a URL via JS bookmarklet and sends it to Safari
# Bookmarklet: javascript:window.location='pythonista://ToSafari?action=run&argv='+encodeURIComponent(document.location.href);
# Assumes script is in the root of Pythonista and called 'ToSafari'


import sys
import webbrowser

numArgs = len(sys.argv)

if numArgs < 2:
    print 'No URL was received'
    
else:
    url = sys.argv[1]

    webbrowser.open('safari-' + url)

Once set up, the script takes around 6 seconds to be executed on my iPad mini from a cold start (Pythonista force-quit from the multitasking tray), 1 second if Pythonista is in the background. While it isn't the best solution, I think this is an acceptable compromise to get a working Chrome-to-Safari bookmarklet that doesn't require any further manual operation.

• You should follow the author on Twitter here.