11// webview-stub.js — заглушка Web Serial API для WebView
2- // Не мешает работе в обычных браузерах с реальным Web Serial API
32( function ( ) {
4- // Проверяем, есть ли уже navigator.serial (в обычном Chrome — есть)
53 if ( navigator . serial ) {
6- console . log ( '[Stub] Real Web Serial API detected, skipping stub ' ) ;
4+ console . log ( '[Stub] Real Web Serial API detected, skipping' ) ;
75 return ;
86 }
97
10- console . log ( '[Stub] Creating virtual serial port for WebView ' ) ;
8+ console . log ( '[Stub] Creating virtual serial port' ) ;
119
12- var _port = null ;
10+ // Сразу создаём порт, чтобы getPorts() всегда его возвращал
11+ var _port = {
12+ readable : new ReadableStream ( {
13+ start : function ( controller ) {
14+ window . _stubController = controller ;
15+ console . log ( '[Stub] ReadableStream started' ) ;
16+ }
17+ } ) ,
18+ writable : new WritableStream ( {
19+ write : function ( chunk ) {
20+ var text = typeof chunk === 'string' ? chunk : new TextDecoder ( ) . decode ( chunk ) ;
21+ console . log ( '[Stub] Write:' , text ) ;
22+ if ( window . _uartWriteCallback ) {
23+ window . _uartWriteCallback ( text ) ;
24+ }
25+ }
26+ } ) ,
27+ open : function ( options ) {
28+ console . log ( '[Stub] Port opened with' , options ) ;
29+ return Promise . resolve ( ) ;
30+ } ,
31+ close : function ( ) {
32+ console . log ( '[Stub] Port closed' ) ;
33+ return Promise . resolve ( ) ;
34+ } ,
35+ getInfo : function ( ) {
36+ return {
37+ usbVendorId : 0x0403 ,
38+ usbProductId : 0x6001
39+ } ;
40+ }
41+ } ;
1342
1443 navigator . serial = {
1544 requestPort : function ( filters ) {
1645 console . log ( '[Stub] requestPort called' , filters ) ;
17- if ( ! _port ) {
18- _port = {
19- readable : new ReadableStream ( {
20- start : function ( controller ) {
21- window . _stubController = controller ;
22- console . log ( '[Stub] ReadableStream started' ) ;
23- }
24- } ) ,
25- writable : new WritableStream ( {
26- write : function ( chunk ) {
27- var text = typeof chunk === 'string' ? chunk : new TextDecoder ( ) . decode ( chunk ) ;
28- console . log ( '[Stub] Write:' , text ) ;
29- if ( window . _uartWriteCallback ) {
30- window . _uartWriteCallback ( text ) ;
31- }
32- }
33- } ) ,
34- open : function ( options ) {
35- console . log ( '[Stub] Port opened with' , options ) ;
36- return Promise . resolve ( ) ;
37- } ,
38- close : function ( ) {
39- console . log ( '[Stub] Port closed' ) ;
40- return Promise . resolve ( ) ;
41- } ,
42- getInfo : function ( ) {
43- return {
44- usbVendorId : 0x0403 ,
45- usbProductId : 0x6001
46- } ;
47- }
48- } ;
49- }
5046 return Promise . resolve ( _port ) ;
5147 } ,
5248
5349 getPorts : function ( ) {
54- console . log ( '[Stub] getPorts called' ) ;
55- var ports = _port ? [ _port ] : [ ] ;
56- return Promise . resolve ( ports ) ;
50+ console . log ( '[Stub] getPorts called, returning 1 port' ) ;
51+ return Promise . resolve ( [ _port ] ) ;
5752 } ,
5853
5954 addEventListener : function ( event , callback ) {
6055 console . log ( '[Stub] addEventListener:' , event ) ;
61- if ( event === 'connect' ) {
62- // Сразу вызываем callback с виртуальным событием
63- setTimeout ( function ( ) {
64- callback ( { target : navigator . serial } ) ;
65- } , 100 ) ;
66- }
6756 } ,
6857
6958 removeEventListener : function ( event , callback ) {
7059 console . log ( '[Stub] removeEventListener:' , event ) ;
7160 }
7261 } ;
7362
74- console . log ( '[Stub] Virtual serial port ready' ) ;
63+ console . log ( '[Stub] Virtual serial port ready (pre-created) ' ) ;
7564} ) ( ) ;
0 commit comments