Skip to content

Commit

Permalink
Add support for Web NFC API
Browse files Browse the repository at this point in the history
  • Loading branch information
FabioPinheiro committed Aug 12, 2023
1 parent 9d7e1b7 commit 0072cb8
Show file tree
Hide file tree
Showing 8 changed files with 226 additions and 0 deletions.
36 changes: 36 additions & 0 deletions api-reports/2_12.txt
Original file line number Diff line number Diff line change
Expand Up @@ -16426,6 +16426,42 @@ MutationRecord[JT] def previousSibling: Node
MutationRecord[JT] def removedNodes: NodeList[Node]
MutationRecord[JT] def target: Node
MutationRecord[JT] def `type`: String
NDEFMessage[JC]
NDEFReader[JC] def addEventListener[T <: Event](`type`: String, listener: js.Function1[T, _], options: EventListenerOptions): Unit
NDEFReader[JC] def addEventListener[T <: Event](`type`: String, listener: js.Function1[T, _], useCapture: Boolean?): Unit
NDEFReader[JC] def dispatchEvent(evt: Event): Boolean
NDEFReader[JC] var onreading: js.Function1[NDEFReadingEvent, Any]
NDEFReader[JC] var onreadingerror: js.Function1[Event, Any]
NDEFReader[JC] def removeEventListener[T <: Event](`type`: String, listener: js.Function1[T, _], options: EventListenerOptions): Unit
NDEFReader[JC] def removeEventListener[T <: Event](`type`: String, listener: js.Function1[T, _], useCapture: Boolean?): Unit
NDEFReader[JC] def scan(options: NDEFScanOptions?): js.Promise[js.Any]
NDEFReader[JC] def write(message: String | js.typedarray.ArrayBuffer | js.typedarray.DataView | js.Array[NDEFRecord], options: NDEFWriteOptions?): js.Promise[js.Any]
NDEFReadingEvent[JT] def bubbles: Boolean
NDEFReadingEvent[JT] def cancelBubble: Boolean
NDEFReadingEvent[JT] def cancelable: Boolean
NDEFReadingEvent[JT] def composed: Boolean
NDEFReadingEvent[JT] def currentTarget: EventTarget
NDEFReadingEvent[JT] def defaultPrevented: Boolean
NDEFReadingEvent[JT] def eventPhase: Int
NDEFReadingEvent[JT] def isTrusted: Boolean
NDEFReadingEvent[JT] var message: NDEFMessage
NDEFReadingEvent[JT] def preventDefault(): Unit
NDEFReadingEvent[JT] var serialNumber: String
NDEFReadingEvent[JT] def stopImmediatePropagation(): Unit
NDEFReadingEvent[JT] def stopPropagation(): Unit
NDEFReadingEvent[JT] def target: EventTarget
NDEFReadingEvent[JT] def timeStamp: Double
NDEFReadingEvent[JT] def `type`: String
NDEFRecord[JC] var data: js.typedarray.DataView
NDEFRecord[JC] var encoding: js.UndefOr[String]
NDEFRecord[JC] var id: js.UndefOr[String]
NDEFRecord[JC] var lang: js.UndefOr[String]
NDEFRecord[JC] var mediaType: js.UndefOr[String]
NDEFRecord[JC] var recordType: String
NDEFRecord[JC] def toRecords(): js.Array[NDEFRecord]
NDEFScanOptions[JT] def signal: js.UndefOr[AbortSignal]
NDEFWriteOptions[JT] def overwrite: Boolean
NDEFWriteOptions[JT] def signal: js.UndefOr[AbortSignal]
NamedNodeMap[JC] @scala.scalajs.js.annotation.JSBracketAccess def apply(index: Int): Attr
NamedNodeMap[JC] def getNamedItem(name: String): Attr
NamedNodeMap[JC] def getNamedItemNS(namespaceURI: String, localName: String): Attr
Expand Down
36 changes: 36 additions & 0 deletions api-reports/2_13.txt
Original file line number Diff line number Diff line change
Expand Up @@ -16426,6 +16426,42 @@ MutationRecord[JT] def previousSibling: Node
MutationRecord[JT] def removedNodes: NodeList[Node]
MutationRecord[JT] def target: Node
MutationRecord[JT] def `type`: String
NDEFMessage[JC]
NDEFReader[JC] def addEventListener[T <: Event](`type`: String, listener: js.Function1[T, _], options: EventListenerOptions): Unit
NDEFReader[JC] def addEventListener[T <: Event](`type`: String, listener: js.Function1[T, _], useCapture: Boolean?): Unit
NDEFReader[JC] def dispatchEvent(evt: Event): Boolean
NDEFReader[JC] var onreading: js.Function1[NDEFReadingEvent, Any]
NDEFReader[JC] var onreadingerror: js.Function1[Event, Any]
NDEFReader[JC] def removeEventListener[T <: Event](`type`: String, listener: js.Function1[T, _], options: EventListenerOptions): Unit
NDEFReader[JC] def removeEventListener[T <: Event](`type`: String, listener: js.Function1[T, _], useCapture: Boolean?): Unit
NDEFReader[JC] def scan(options: NDEFScanOptions?): js.Promise[js.Any]
NDEFReader[JC] def write(message: String | js.typedarray.ArrayBuffer | js.typedarray.DataView | js.Array[NDEFRecord], options: NDEFWriteOptions?): js.Promise[js.Any]
NDEFReadingEvent[JT] def bubbles: Boolean
NDEFReadingEvent[JT] def cancelBubble: Boolean
NDEFReadingEvent[JT] def cancelable: Boolean
NDEFReadingEvent[JT] def composed: Boolean
NDEFReadingEvent[JT] def currentTarget: EventTarget
NDEFReadingEvent[JT] def defaultPrevented: Boolean
NDEFReadingEvent[JT] def eventPhase: Int
NDEFReadingEvent[JT] def isTrusted: Boolean
NDEFReadingEvent[JT] var message: NDEFMessage
NDEFReadingEvent[JT] def preventDefault(): Unit
NDEFReadingEvent[JT] var serialNumber: String
NDEFReadingEvent[JT] def stopImmediatePropagation(): Unit
NDEFReadingEvent[JT] def stopPropagation(): Unit
NDEFReadingEvent[JT] def target: EventTarget
NDEFReadingEvent[JT] def timeStamp: Double
NDEFReadingEvent[JT] def `type`: String
NDEFRecord[JC] var data: js.typedarray.DataView
NDEFRecord[JC] var encoding: js.UndefOr[String]
NDEFRecord[JC] var id: js.UndefOr[String]
NDEFRecord[JC] var lang: js.UndefOr[String]
NDEFRecord[JC] var mediaType: js.UndefOr[String]
NDEFRecord[JC] var recordType: String
NDEFRecord[JC] def toRecords(): js.Array[NDEFRecord]
NDEFScanOptions[JT] def signal: js.UndefOr[AbortSignal]
NDEFWriteOptions[JT] def overwrite: Boolean
NDEFWriteOptions[JT] def signal: js.UndefOr[AbortSignal]
NamedNodeMap[JC] @scala.scalajs.js.annotation.JSBracketAccess def apply(index: Int): Attr
NamedNodeMap[JC] def getNamedItem(name: String): Attr
NamedNodeMap[JC] def getNamedItemNS(namespaceURI: String, localName: String): Attr
Expand Down
15 changes: 15 additions & 0 deletions dom/src/main/scala/org/scalajs/dom/NDEFMessage.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
package org.scalajs.dom

import scala.scalajs.js
import scala.scalajs.js.annotation.JSGlobal

/** The [[NDEFMessage]] interface of the Web NFC API represents the content of an NDEF message that has been read from
* or could be written to an NFC tag. An instance is acquired by calling the NDEFMessage() constructor or from the
* NDEFReadingEvent.message property, which is passed to the reading event.
*
* @param records\
* The records property of NDEFMessage interface represents a list of NDEFRecords present in the NDEF message.
*/
@js.native
@JSGlobal
class NDEFMessage(var records: js.Array[NDEFRecord] = js.native) extends js.Object
52 changes: 52 additions & 0 deletions dom/src/main/scala/org/scalajs/dom/NDEFReader.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
package org.scalajs.dom

import scala.scalajs.js
import scala.scalajs.js.annotation.JSGlobal
import scala.scalajs.js.|

/** The [[NDEFReader]] interface of the Web NFC API (https://developer.mozilla.org/en-US/docs/Web/API/Web_NFC_API) is
* used to read from and write data to compatible NFC devices, e.g. NFC tags supporting NDEF, when these devices are
* within the reader's magnetic induction field.
*/
@JSGlobal("NDEFReader")
@js.native
class NDEFReader() extends EventTarget {

/** Activates a reading device and returns a Promise that either resolves when an NFC tag read operation is scheduled
* or rejects if a hardware or permission error is encountered. This method triggers a permission prompt if the "nfc"
* permission has not been previously granted.
*
* @return
* a Promise that resolves immediately after scheduling read operations for the NFC adapter.
*/
def scan(options: NDEFScanOptions = js.native): js.Promise[js.Any] = js.native

/** Attempts to write an NDEF message to a tag and returns a Promise that either resolves when a message has been
* written to the tag or rejects if a hardware or permission error is encountered. This method triggers a permission
* prompt if the "nfc" permission has not been previously granted.
*
* @param message
* The message to be written, either a string object or literal, an ArrayBuffer, a TypedArray, a DataView, or an
* array of records. A record has the following members:
* @param options
* An object with the following properties:
*
* @return
* a Promise that either resolves when a message has been written to the tag or rejects if a hardware or permission
* error is encountered.
*/
def write(
message: String | js.typedarray.ArrayBuffer | /* js.typedarray.TypedArray[NDEFRecord, ???] */ js.typedarray.DataView |
js.Array[NDEFRecord], options: NDEFWriteOptions = js.native
): js.Promise[js.Any] = js.native

/** The reading event of the NDEFReader interface is fired whenever a new reading is available from compatible NFC
* devices (e.g. NFC tags supporting NDEF) when these devices are within the reader's magnetic induction field.
*/
var onreading: js.Function1[NDEFReadingEvent, Any] = js.native

/** The readingerror event of the NDEFReader interface is fired whenever an error occurs during reading of NFC tags,
* e.g. when tags leave the reader's magnetic induction field.
*/
var onreadingerror: js.Function1[Event, Any] = js.native
}
22 changes: 22 additions & 0 deletions dom/src/main/scala/org/scalajs/dom/NDEFReadingEvent.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
package org.scalajs.dom

import scala.scalajs.js

/** The NDEFReadingEvent interface of the Web NFC API represents events dispatched on new NFC readings obtained by
* NDEFReader.
*
* @see
* https://developer.mozilla.org/en-US/docs/Web/API/NDEFReadingEvent
*/
@js.native
trait NDEFReadingEvent extends Event {

/** Returns an NDEFMessage object containing the received message. */
var message: NDEFMessage = js.native

/** Returns the serial number of the device, which is used for anti-collision and identification, or an empty string
* if no serial number is available.
*/
var serialNumber: String = js.native

}
42 changes: 42 additions & 0 deletions dom/src/main/scala/org/scalajs/dom/NDEFRecord.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
package org.scalajs.dom

import scala.scalajs.js
import scala.scalajs.js.annotation.JSGlobal

/** The [[NDEFRecord]] interface of the Web NFC API provides data that can be read from, or written to, compatible NFC
* devices, e.g. NFC tags supporting NDEF.
*/
@js.native
@JSGlobal
class NDEFRecord extends js.Object {

/** Returns the record type of the record. Records must have either a standardized well-known type name such as
* "empty", "text", "url", "smart-poster", "absolute-url", "mime", or "unknown" or else an external type name, which
* consists of a domain name and custom type name separated by a colon (":").
*/
var recordType: String = js.native

/** Returns the MIME type of the record. This value will be null if recordType is not equal to "mime". */
var mediaType: js.UndefOr[String] = js.native

/** Returns the record identifier, which is an absolute or relative URL used to identify the record.
*
* Note: The uniqueness of the identifier is enforced only by the generator of the record.
*/
var id: js.UndefOr[String] = js.native

/** Returns a DataView containing the raw bytes of the record's payload. */
var data: js.typedarray.DataView = js.native
// var data: DataView = js.native

/** Returns the encoding of a textual payload, or null otherwise. */
var encoding: js.UndefOr[String] = js.native

/** Returns the language of a textual payload, or null if one was not supplied. */
var lang: js.UndefOr[String] = js.native

/** Converts [[NDEFRecord.data]] to a sequence of records. This allows parsing the payloads of record types which may
* contain nested records, such as smart poster and external type records.
*/
def toRecords(): js.Array[NDEFRecord] = js.native
}
10 changes: 10 additions & 0 deletions dom/src/main/scala/org/scalajs/dom/NDEFScanOptions.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
package org.scalajs.dom

import scala.scalajs.js

@js.native
trait NDEFScanOptions extends js.Object {

/** An AbortSignal that allows the current write operation to be canceled. */
def `signal`: js.UndefOr[AbortSignal] = js.native
}
13 changes: 13 additions & 0 deletions dom/src/main/scala/org/scalajs/dom/NDEFWriteOptions.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
package org.scalajs.dom

import scala.scalajs.js

@js.native
trait NDEFWriteOptions extends js.Object {

/** A boolean value specifying whether or not existing records should be overwritten, if such exists. */
def `overwrite`: Boolean = js.native

/** An AbortSignal that allows the current write operation to be canceled. */
def `signal`: js.UndefOr[AbortSignal] = js.native
}

0 comments on commit 0072cb8

Please sign in to comment.