@@ -5,7 +5,7 @@ function log(msg) {
55
66log ( "WebNFC available: " + ( "NDEFReader" in window ) ) ;
77
8- const myUrl = "https://rbyers.github.io/webnfc.html " ;
8+ const mimeType = "application/x-rbyerstest " ;
99
1010let abortController = null ;
1111abortController . signal . onabort = event => {
@@ -25,8 +25,8 @@ document.getElementById('init').addEventListener("click", async () => {
2525try {
2626const ndef = new NDEFReader ( ) ;
2727await ndef . write ( { records :[
28- { recordType :"url " , data :myUrl } ,
29- { recordType : "text" , data : "0" } ] } ) ;
28+ { recordType :"mime " , mediaType : mimeType , data :"0" }
29+ ] } ) ;
3030log ( "Card initialized, duration: " + ( new Date ( ) - start ) + "ms" ) ;
3131} catch ( error ) {
3232log ( "Error: " + error ) ;
@@ -50,35 +50,37 @@ document.getElementById('scan').addEventListener("click", async () => {
5050let readTime = new Date ( ) ;
5151log ( `Found card,${ readTime - start } ms.` ) ;
5252log ( ` Serial Number:${ serialNumber } ` ) ;
53- let text = "" ;
54- let matchedUrl = false ;
5553for ( const record of message . records ) {
5654log ( " Record type: " + record . recordType ) ;
5755log ( " MIME type: " + record . mediaType ) ;
5856log ( " Record id: " + record . id ) ;
5957log ( " Encoding: " + record . encoding ) ;
6058switch ( record . recordType ) {
6159case "text" :
62- text = new TextDecoder ( ) . decode ( record . data ) ;
60+ const text = new TextDecoder ( ) . decode ( record . data ) ;
6361log ( ` Text:${ text } (${ record . lang } )` ) ;
6462break ;
6563case "url" :
6664const url = new TextDecoder ( ) . decode ( record . data ) ;
6765log ( " Url: " + url ) ;
68- if ( url == myUrl ) matchedUrl = true ;
66+ break ;
67+ case "mime" :
68+ if ( record . mediaType == mimeType ) {
69+ const text = new TextDecoder ( ) . decode ( record . data ) ;
70+ let count = parseInt ( text ) ;
71+ count ++ ;
72+ log ( " Updating count to: " + count ) ;
73+ await ndef . write ( { signal :abortController . signal , records :[
74+ { recordType :"mime" , mediaType :mimeType , data :"0" }
75+ ] } ) ;
76+ log ( ` Update complete, duration:${ new Date ( ) - readTime } ms` ) ;
77+ }
6978break ;
7079default :
7180log ( " Unknown record type" ) ;
7281}
7382}
7483if ( matchedUrl ) {
75- let count = parseInt ( text ) ;
76- count ++ ;
77- log ( " Updating count to: " + count ) ;
78- await ndef . write ( { signal :abortController . signal , records :[
79- { recordType :"url" , data :myUrl } ,
80- { recordType :"text" , data :count . toString ( ) } ] } ) ;
81- log ( ` Update complete, duration:${ new Date ( ) - readTime } ms` ) ;
8284}
8385} catch ( error ) {
8486log ( "Error: " + error ) ;