A Weird Imagination

Share from phone to local web app

Posted in

The problem#

Previously, I covered how to programmatically navigate Firefox to a URL. But what's the most convenient way for the user to provide that URL? It would be great if when viewing a web page on one device, it would take just a few clicks to open it in the Firefox window on the target device.

You may have noticed this sounds a lot like the "cast" feature in Chrome; we're just trying to build a simplified version of it that we have full control over. At least that UI already exists, which simplifies the problem to figuring out how we can add our own option to the share menu that casting appears in (without having to write and publish our own apps for Android and iOS) or somehow make use of one of the existing options.

The Web Share Target API lets a web app add an entry to the share menu… but it's not supported by Firefox or Safari, so it's only usable on Android devices, not iOS.

The solution#

Home Assistant can be configured to run a script when used as a share target, including making a REST request.

Adding the command cannot be done inside the web interface; instead add the following lines to configuration.yaml:

    url: "http://local_device/ha_share"
    method: POST
    content_type: "text/plain"
    payload: "{{ url }}"

Then add an automation triggered by the mobile_app.share event with the action

service: rest_command.send_url
  url: '{{ trigger.event.data.url }}'

Then whenever a user views a website on their phone which is logged into Home Assistant, they can share to Home Assistant and it will generate an HTTP POST request to http://local_device/ha_share where the body of the request will be the URL of the website.

The details#

What is Home Assistant?#

Home Assistant is an open-source system for home automation, which mainly means controlling "Internet of Things" devices like computer-controlled lights, outlets, and "smart" appliances with support for working entirely locally for many devices (i.e., not relying on anyone's cloud servers). I already had it set up for controlling some lights, so tacking on another automation was pretty lightweight, but if you aren't already using it, then it might not be as convenient.

One downside of using Home Assistant is that it requires a login. That's fine for members of a household where everyone is already set up to use it, but it doesn't really work for guests or use in a more public context. That said, there's always the only slightly less convenient fallback of explicitly going to a website where you copy and paste the URL into a form.

Other options#

Generate QR code#

Instead of trying to transfer the URL over the network, we could use a common way of communicating URLs to nearby devices: QR codes. The share menu on my phone gives an option to display a QR code for the current website on the phone screen. If the target device has a camera, it could read the QR code using zbarcam, qr-scanner, or something similar. The user would have to request the device read the QR code somehow, which would be at least one extra step, but if it's just tapping somewhere on the screen they're holding their phone up to, that seems pretty minor.

Other apps#

I tried to look for other apps or share options that I could get to talk to a local computer in a way that I could script, but I did not manage to come up with anything else. If you have an idea, please let me know!


Have something to add? Post a comment by sending an email to comments@aweirdimagination.net. You may use Markdown for formatting.

There are no comments yet.