-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathwebview-stub.js
More file actions
156 lines (131 loc) · 5.37 KB
/
Copy pathwebview-stub.js
File metadata and controls
156 lines (131 loc) · 5.37 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
// webview-stub.js — Universal Web Serial API stub for Android WebView
(function() {
if (navigator.serial && navigator.serial.requestPort) {
console.log('[Stub] Native Web Serial API detected, skipping');
return;
}
const MAX_PORTS = 4;
let portCounter = 0;
let initialized = false;
function createPort(portId) {
return {
_portId: portId,
_readable: null,
_writable: null,
_baudRate: 9600,
get readable() {
if (!this._readable) {
this._readable = new ReadableStream({
start: (controller) => {
window['_stubController' + portId] = controller;
console.log('[Stub] ReadableStream created for port ' + portId);
}
});
}
return this._readable;
},
get writable() {
if (!this._writable) {
this._writable = new WritableStream({
write: (chunk) => {
const text = typeof chunk === 'string' ? chunk : new TextDecoder().decode(chunk);
sendToNative('uart://write?' + encodeURIComponent(portId + '|' + text));
}
});
}
return this._writable;
},
open: function(options) {
const baudRate = options?.baudRate || 9600;
console.log('[Stub] Port ' + portId + ' open() baudRate=' + baudRate);
if (baudRate !== this._baudRate) {
this._baudRate = baudRate;
sendToNative('uart://setbaud?' + encodeURIComponent(portId + '|' + baudRate));
}
this._readable = null;
this._writable = null;
return new Promise(resolve => setTimeout(resolve, 300));
},
close: function() {
console.log('[Stub] Port ' + portId + ' close()');
this._baudRate = 9600;
sendToNative('uart://close?' + portId);
return Promise.resolve();
},
getInfo: function() {
return { usbVendorId: 0x0403, usbProductId: 0x6001 };
},
forget: function() { return Promise.resolve(); },
setSignals: function(s) { return Promise.resolve(); },
getSignals: function() { return Promise.resolve({}); }
};
}
function sendToNative(url) {
const iframe = document.createElement('iframe');
iframe.style.display = 'none';
iframe.src = url;
document.body.appendChild(iframe);
setTimeout(() => document.body.removeChild(iframe), 100);
}
const ports = [];
for (let i = 0; i < MAX_PORTS; i++) {
ports.push(createPort(i));
}
window._initStub = function(config) {
if (initialized) return;
initialized = true;
const appName = config?.appName || 'unknown';
const preferredPort = config?.preferredPort || 0;
portCounter = preferredPort;
console.log('[Stub] Initialized for ' + appName + ' (starting port: ' + preferredPort + ', total: ' + MAX_PORTS + ')');
};
navigator.serial = {
requestPort: function(filters) {
const portIndex = portCounter % MAX_PORTS;
portCounter++;
console.log('[Stub] requestPort() -> port ' + portIndex);
return Promise.resolve(ports[portIndex]);
},
getPorts: function() {
return Promise.resolve([...ports]);
},
addEventListener: function() {},
removeEventListener: function() {}
};
// ========== Перехват скачивания файлов для Android ==========
// Перехватываем HTMLAnchorElement.prototype.click
(function() {
var originalClick = HTMLAnchorElement.prototype.click;
var blobUrls = new Map();
var originalCreateObjectURL = URL.createObjectURL;
URL.createObjectURL = function(blob) {
var url = originalCreateObjectURL.call(URL, blob);
blobUrls.set(url, blob);
return url;
};
HTMLAnchorElement.prototype.click = function() {
if (this.download && this.href && this.href.startsWith('blob:')) {
console.log('[Stub] Intercepted click(): ' + this.download);
var filename = this.download;
var blob = blobUrls.get(this.href);
if (blob) {
var reader = new FileReader();
reader.onload = function() {
var base64 = reader.result.split(',')[1];
var iframe = document.createElement('iframe');
iframe.style.display = 'none';
iframe.src = 'app://savefile?' + encodeURIComponent(filename + '|' + base64);
document.body.appendChild(iframe);
setTimeout(function() { document.body.removeChild(iframe); }, 100);
};
reader.readAsDataURL(blob);
}
return;
}
return originalClick.call(this);
};
console.log('[Stub] HTMLAnchorElement.click() interceptor registered');
})();
// =============================================================
console.log('[Stub] Universal Web Serial stub loaded (' + MAX_PORTS + ' ports)');
})();