Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow Web Bluetooth and Web Serial within iframes #2514

Merged
merged 1 commit into from
Mar 19, 2024

Conversation

ErikMejerHansen
Copy link
Contributor

What

Allow livebooks to use the Web Bluetooth and Web Serial APIs.
Previously discussed here: #1331

How

Both Web Bluetooth and Web Serial are disallowed for cross origin iframes unless enabled in the iframe allow attribute.

Verification

Using the attached Livebook before these changes and clicking the "Open Web Bluetooth Dialog" and "Open Web Serial Dialog" will result in Permissions policy violation: bluetooth is not allowed in this document. and Permissions policy violation: serial is not allowed in this document errors.

After these changes the Web Bluetooth and Web Serial dialogs open as expected (in supported browsers).

image

Web BLE and Web Serial Livebook

WebBluetooth & WebSerial

Mix.install([
  {:kino, "~> 0.12.0"}
])

Web Bluetooth

defmodule WebBLE do
  use Kino.JS

  def web_ble() do
    Kino.JS.new(__MODULE__, "")
  end

  asset "main.js" do
    """
       export function init(ctx) {
        ctx.root.innerHTML = `
          <button id="bleButton">Open Web Bluetooth Dialog</button>
       `;

       var bleDialogButtonEl = document.getElementById("bleButton");
       
       bleDialogButtonEl.addEventListener("click", (_event) => {
         navigator.bluetooth.requestDevice({ acceptAllDevices: true});
       }); 
    }
    """
  end
end
WebBLE.web_ble()

Web Serial

defmodule WebSerial do
  use Kino.JS

  def web_serial() do
    Kino.JS.new(__MODULE__, "")
  end

  asset "main.js" do
    """
       export function init(ctx) {
        ctx.root.innerHTML = `
          <button id="serialButton">Open Web Serial Dialog</button>
       `;

       var serialDialogButtonEl = document.getElementById("serialButton");
       
       serialDialogButtonEl.addEventListener("click", (_event) => {
         navigator.serial.requestPort({ acceptAllDevices: true});
       }); 
    }
    """
  end
end
WebSerial.web_serial()

Both Web Bluetooth and Web Serial are disallowed for cross origin
iframes unless enabled in the iframe allow attribute
@CLAassistant
Copy link

CLAassistant commented Mar 18, 2024

CLA assistant check
All committers have signed the CLA.

Copy link
Member

@jonatanklosko jonatanklosko left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks :)

@jonatanklosko jonatanklosko changed the title Enable Web Bluetooth and Web Serial Enable Web Bluetooth and Web Serial within iframes Mar 19, 2024
@jonatanklosko jonatanklosko changed the title Enable Web Bluetooth and Web Serial within iframes Allow Web Bluetooth and Web Serial within iframes Mar 19, 2024
@jonatanklosko jonatanklosko merged commit 269df7b into livebook-dev:main Mar 19, 2024
1 check passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants