-
Notifications
You must be signed in to change notification settings - Fork 7
/
webprint.js
223 lines (198 loc) · 8.04 KB
/
webprint.js
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
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
/**
* This file is part of WebPrint
*
* @author Michael Wallace
*
* Copyright (C) 2015 Michael Wallace, WallaceIT
*
* All rights reserved. This program and the accompanying materials
* are made available under the terms of the GNU Lesser General Public License
* (LGPL) version 2.1 which accompanies this distribution, and is available at
* http://www.gnu.org/licenses/lgpl-2.1.html
*
* This software is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
*/
var WebPrint = function (init, opt) {
var options = {
relayHost: "127.0.0.1",
relayPort: "8080",
listPrinterCallback: null,
listPortsCallback: null,
readyCallback: null
};
$.extend(options, opt);
this.printRaw = function (data, printer) {
var request = {a: "printraw", printer: printer, data: btoa(data)};
sendAppletRequest(request);
};
this.printSerial = function (data, port) {
if (isAndroid){
alert("Serial port printing is not available in Android.");
return;
}
var request = {a: "printraw", port: port, data: btoa(data)};
sendAppletRequest(request);
};
this.printTcp = function (data, socket) {
var request = {a: "printraw", socket: socket, data: btoa(data)};
sendAppletRequest(request);
};
this.printHtml = function (data, printer) {
if (isAndroid){
alert("HTML printing is not available in Android.");
return;
}
var request = {a: "printhtml", printer: printer, data: data};
sendAppletRequest(request);
};
/*
* Opens a port using the specified settings
* @param port String eg. COM1 / TTY0
* @param settings Object eg. {baud:9600, databits:8, stopbits:1, parity:even, flow:"none"}
*/
this.openPort = function (port, settings) {
var request = {a: "openport", port: port, settings: settings};
sendAppletRequest(request);
};
this.requestPrinters = function () {
sendAppletRequest({a: "listprinters"});
};
this.requestPorts = function () {
if (!isAndroid)
sendAppletRequest({a: "listports"});
};
function sendAppletRequest(data) {
data.cookie = cookie;
if (!wpwindow || wpwindow.closed || !wpready) {
if (wpready){
openPrintWindow();
} else {
webprint.checkRelay();
console.log("Print applet connection not established...trying to reconnect");
}
setTimeout(function () {
wpwindow.postMessage(JSON.stringify(data), "*");
}, 220);
return;
}
//freshka 2021-12-29
if (wpwindow && wpfreeze) {
wpwindow.focus();
wpfreeze = false;
}
wpwindow.postMessage(JSON.stringify(data), "*");
}
var wpwindow;
var wpready = false;
function openPrintWindow() {
wpready = false;
wpwindow = window.open("http://"+options.relayHost+":"+options.relayPort+"/printwindow", 'WebPrintService');
wpwindow.blur();
window.focus();
}
var wptimeOut;
this.checkRelay = function () {
if (wpwindow && !wpwindow.closed) {
wpwindow.close();
}
window.addEventListener("message", handleWebPrintMessage, false);
openPrintWindow();
wptimeOut = setTimeout(dispatchWebPrint, 2000);
};
var wpfreeze = false; //freshka 2021-12-29
function handleWebPrintMessage(event) {
if (event.origin != "http://"+options.relayHost+":"+options.relayPort)
return;
switch (event.data.a) {
case "init":
clearTimeout(wptimeOut);
wpready = true;
sendAppletRequest({a:"init"});
break;
case "response":
if (!event.data.json) return; //freshka 2021-12-29
var response = JSON.parse(event.data.json);
if (response.hasOwnProperty('ports')) {
if (options.listPortsCallback instanceof Function)
options.listPortsCallback(response.ports);
} else if (response.hasOwnProperty('printers')) {
if (options.listPrinterCallback instanceof Function)
options.listPrinterCallback(response.printers);
} else if (response.hasOwnProperty('error')) {
alert(response.error);
}
if (response.hasOwnProperty("cookie")){
cookie = response.cookie;
localStorage.setItem("webprint_auth", response.cookie);
}
if (response.hasOwnProperty("ready")){
if (options.readyCallback instanceof Function) options.readyCallback();
}
break;
case "freeze": //freshka 2021-12-29
wpfreeze = true;
break;
case "error": // cannot contact print applet from relay window
webprint.checkRelay();
}
//alert("The Web Printing service has been loaded in a new tab, keep it open for faster printing.");
}
function dispatchWebPrint() {
var answer = confirm("Cannot communicate with the printing app.\nWould you like to open/install the printing app?");
if (answer) {
if (isAndroid){
deployAndroid();
return;
}
var installFile="WebPrint.jar";
if (navigator.appVersion.indexOf("Win")!=-1) installFile="WebPrint_windows_1_1.exe";
if (navigator.appVersion.indexOf("Mac")!=-1) installFile="WebPrint_macos_1_1.dmg";
if (navigator.appVersion.indexOf("X11")!=-1) installFile="WebPrint_unix_1_1.sh";
if (navigator.appVersion.indexOf("Linux")!=-1) installFile="WebPrint_unix_1_1.sh";
window.open("https://content.wallaceit.com.au/webprint/"+installFile, '_blank');
}
}
function deployAndroid(){
if (isAndroidIntentSupported()){
deployAndroidChrome();
} else {
deployAndroidFirefox();
}
//document.location.href = "intent://#Intent;scheme=webprint;package=au.com.wallaceit.webprint;S.browser_fallback_url=https%3A%2F%2Fwallaceit.com.au%2Fplaystore%2Fwebprint;end";
}
function isAndroidIntentSupported() {
var isChrome = navigator.userAgent.toLowerCase().indexOf('chrome') > -1;
if (isChrome) {
var version = parseInt(window.navigator.appVersion.match(/Chrome\/(\d+)\./)[1], 10);
return version >= 25;
} else {
return false;
}
}
function deployAndroidChrome(){
// this link needs to be clicked by the user
var html = '<div id="intent_link" style="position: fixed; top:40%; width: 120px; background-color: white; left:50%; margin-left: -60px; border: solid 2px rgb(75, 75, 75); font-family: Helvetica SansSerif sans-serif; text-align: center; padding: 5px;">' +
'<a onclick="window.location=\'intent://#Intent;scheme=webprint;package=au.com.wallaceit.webprint;S.browser_fallback_url=https%3A%2F%2Fwallaceit.com.au%2Fplaystore%2Fwebprint;end\'; document.getElementById(\'intent_link\').remove();">Click To Open WebPrint</a></div>';
document.body.innerHTML += html;
}
function deployAndroidFirefox() {
var timeout = setTimeout(function() {
window.location = "https://wallaceit.com.au/playstore/webprint";
}, 1000);
window.addEventListener("pagehide", function(evt) {
clearTimeout(timeout);
});
window.location = "webprint://open";
}
var cookie = localStorage.getItem("webprint_auth");
if (cookie==null){
cookie = "";
}
var isAndroid = navigator.appVersion.indexOf("Android")!=-1;
if (init) this.checkRelay();
return this;
};