-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathlightspeedcatchbarcode.user.js
More file actions
177 lines (151 loc) · 5.14 KB
/
lightspeedcatchbarcode.user.js
File metadata and controls
177 lines (151 loc) · 5.14 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
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
// ==UserScript==
// @name Lightspeed Register Barcode Catcher
// @namespace https://github.com/gmkarl/lightspeedcatchbarcode/
// @version 0.2.1
// @description Handles barcodes entered into the wrong place in the Lightspeed Register.
// @author Karl Semich
// @match https://*.merchantos.com/register.php*
// @grant unsafeWindow
// @grant GM_xmlhttpRequest
// ==/UserScript==
(function(){
'use strict';
var lastKeys = [];
var initialTarget;
var initialValue;
function isNumeric(evt) {
return evt.keyCode >= 48 && evt.keyCode <= 57;
}
function isEnter(evt) {
return evt.keyCode == 13;
}
function toNumeric(evt) {
return evt.keyCode - 48;
}
function isWrongTarget(evt) {
var element = unsafeWindow.singleElementById('add_search_item_text');
return element && evt.target && evt.target != element;
}
function isEAN(digitArray) {
var sum = 0;
for (var i = 0; i < digitArray.length; ++i) {
if ((digitArray.length-i)%2)
sum += digitArray[i];
else
sum += digitArray[i] * 3;
}
return sum % 10 == 0;
}
function isUPCE(d) {
if (d.length != 8)
return false;
if (d[0] != 0 && d[0] != 1)
return false;
var n;
if (d[6] < 3)
n = [d[0],d[1],d[2],d[6],0, 0, 0,0,d[3],d[4],d[5],d[7]];
else if (d[6] == 3)
n = [d[0],d[1],d[2],d[3],0, 0, 0,0,0, d[4],d[5],d[7]];
else if (d[6] == 4)
n = [d[0],d[1],d[2],d[3],d[4],0, 0,0,0, 0, d[5],d[7]];
else if (d[6] > 4)
n = [d[0],d[1],d[2],d[3],d[4],d[5],0,0,0, 0, d[6],d[7]];
if (isEAN(n)) {
//lastKeys = n;
return true;
} else {
return false;
}
}
function isBarcode(digitArray) {
return isUPCE(digitArray) || ((digitArray.length == 8 || digitArray.length == 12 || digitArray.length == 13) && isEAN(digitArray));
}
function doItemSearch(string) {
window.eval(
"var element = singleElementById('add_search_item_text');" +
"element.value = '" + string + "';" +
"merchantos.register.addItemSearch(element);"
);
}
unsafeWindow.onkeydown = cloneInto(function(evt) {
try {
var register = document.getElementById("register");
if (!register || register.style.display == "none") {
lastKeys = [];
return true;
}
if (isNumeric(evt)) {
if (lastKeys.length == 0) {
initialTarget = evt.target;
initialValue = initialTarget.value;
}
lastKeys.push(toNumeric(evt));
return true;
}
if (isEnter(evt) && isWrongTarget(evt)) {
if (isBarcode(lastKeys)) {
console.log("Barcode entered into wrong field.");
if (evt.target == initialTarget && initialValue !== undefined ) {
console.log("Returning field to original value of '" + initialValue + "'");
initialTarget.value = initialValue;
}
var barcode = lastKeys.join("");
console.log("Performing itemsearch for " + barcode);
doItemSearch(barcode);
lastKeys = [];
return false;
}
}
lastKeys = [];
} catch(e) {
reportExceptionAsIssue(e, "onkeydown");
}
return true;
}, unsafeWindow, {cloneFunctions:true});
unsafeWindow.onclick = cloneInto(function(evt) {
try {
lastKeys = [];
} catch(e) {
reportExceptionAsIssue(e, "onclick");
}
return true;
}, unsafeWindow, {cloneFunctions:true});
// Submit a github issue about a thrown exception
var reportExceptionAsIssueRequest;
//var eventLog = [];
function reportExceptionAsIssue(error, label) {
try {
var issueTitle = label + ": " + error.toString();
var issueStackTrace = error.stack;
//var issueEventLog = eventLog.join("\n")
// .replace(/<select name=\\?"employee_id\\?"[^]*?<\/select>/g, "<!-- censored employee id -->");
console.log(issueTitle);
console.log("Stack trace:");
console.log(issueStackTrace);
//console.log("Event log:");
//console.log(issueEventLog);
try {
if (document.getElementById("session_shop").innerHTML == "Test Store")
return;
} catch(e) {}
reportExceptionAsIssueRequest = GM_xmlhttpRequest({
url: "https://api.github.com/repos/gmkarl/lightspeedcatchbarcode/issues",
method: "POST",
headers: {
"User-Agent": "lightspeedcatchbarcode",
Accept: "application/vnd.github.v3+json",
Authorization: "token be8980229117ea4298" + "497dc0f7f4af73ac24f040",
"Content-Type": "application/json"
},
data: JSON.stringify({
title: issueTitle,
body: "Stack trace:\n```\n" + issueStackTrace + "\n```" //+ "\nEvent log:\n```\n" + issueEventLog + "\n```"
}),
});
} catch(e) {
console.log("exception in exception handler");
console.log(e.toString());
console.log(e.stack);
}
}
})();