Skip to content

Commit 080f1d0

Browse files
authored
Code detection improvements (#5)
* feat: prefer EAN13 > 8 when multiple * fix: false positive EAN for longer strings
1 parent 5dc3f6b commit 080f1d0

5 files changed

Lines changed: 25 additions & 5 deletions

File tree

content_scripts/pola.js

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,9 @@
4343
text = text.replace(/<script\b[^<]*(?:(?!<\/script>)<[^<]*)*<\/script>/gi, ' ');
4444
text = text.replace(/<style\b[^<]*(?:(?!<\/style>)<[^<]*)*<\/style>/gi, ' ');
4545
text = text.replace(/<[^>]+>/gi, ' ');
46-
result = text.match(/\d{13}|\d{8}/g);
46+
// Granice (?<!\d)/(?!\d) wykluczają fragmenty dłuższych ciągów cyfr,
47+
// np. 13-cyfrowy prefiks 14-cyfrowego kodu wewnętrznego sklepu.
48+
result = text.match(/(?<!\d)(?:\d{13}|\d{8})(?!\d)/g);
4749
let gtin13 = document.querySelectorAll('meta[itemprop="gtin13"][content]');
4850
let gtin8 = document.querySelectorAll('meta[itemprop="gtin8"][content]');
4951
let flix = document.querySelectorAll('script[data-flix-ean]');
@@ -63,6 +65,14 @@
6365
if (result !== null) {
6466
result = result.filter((v, i, a) => a.indexOf(v) === i);
6567
result = result.filter(validateEAN);
68+
// Krótkie kody wewnętrzne sklepów bywają błędnie łapane jako EAN-8;
69+
// jeśli obok nich jest dokładnie jeden EAN-13, uznaj go za właściwy kod.
70+
if (result.length > 1) {
71+
let ean13 = result.filter((v) => v.length === 13);
72+
if (ean13.length === 1) {
73+
result = ean13;
74+
}
75+
}
6676
}
6777
if (result !== null && result.length === 1) {
6878
chrome.runtime.sendMessage({

lib/validateEAN.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
* Validates an EAN-8 or EAN-13 barcode using the check digit algorithm.
33
*/
44
function validateEAN(elem) {
5-
if (isNaN(elem)) {
5+
if (isNaN(elem) || (elem.length !== 8 && elem.length !== 13)) {
66
return false;
77
}
88
let sum = 0;

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
"private": true,
66
"scripts": {
77
"test": "jest",
8-
"test:integration": "xvfb-run npx playwright test"
8+
"test:integration": "npx playwright test"
99
},
1010
"devDependencies": {
1111
"@playwright/test": "^1.61.1",

test-e2e/ean-detection.spec.js

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -177,8 +177,6 @@ test.describe('EAN detection on real product pages', () => {
177177

178178
if (eanMessage) {
179179
expect(eanMessage.result).toBe(ean);
180-
} else if (multipleMessage) {
181-
expect(multipleMessage.result).toContain(ean);
182180
} else {
183181
throw new Error(
184182
`Content script nie znalazł EAN ${ean} na stronie ${store}. ` +

test/validateEAN.test.js

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,18 @@ describe('validateEAN', () => {
4646
expect(validateEAN('12345')).toBe(false);
4747
});
4848

49+
test('14-digit internal code (00461455550000)', () => {
50+
expect(validateEAN('00461455550000')).toBe(false);
51+
});
52+
53+
test('valid EAN-13 with extra digit appended (59003977457800)', () => {
54+
expect(validateEAN('59003977457800')).toBe(false);
55+
});
56+
57+
test('12 digits (590039774578)', () => {
58+
expect(validateEAN('590039774578')).toBe(false);
59+
});
60+
4961
test('non-numeric string', () => {
5062
expect(validateEAN('abcdefghijklm')).toBe(false);
5163
});

0 commit comments

Comments
 (0)