-
Notifications
You must be signed in to change notification settings - Fork 0
/
common.js
executable file
·85 lines (79 loc) · 2.2 KB
/
common.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
document.addEventListener(
'DOMContentLoaded',
function() {
let CARRIER = null;
document.querySelectorAll('input').forEach(ele => {
ele.addEventListener(
'click',
function(event) {
CARRIER = event.target.value;
document
.querySelector('.postal-selector-input')
.setAttribute(
'style',
'height: 100px; overflow: inherit; margin-top: 10px; margin-bottom: 10px'
);
document.querySelector('footer').style.display = 'block';
document
.getElementById('inputTxt')
.setAttribute('placeholder', `*** Enter ${CARRIER.toUpperCase()} Tracking Number ***`);
},
false
);
});
document.getElementById('submit').addEventListener(
'click',
function() {
try {
if (document.getElementById('inputTxt').value) {
const trackingId = document.getElementById('inputTxt').value.split(/[\n, ]+/).join(',');
chrome.tabs.create({ url: getCarrierUrl(CARRIER, trackingId) });
} else {
// error handler
document.getElementById('errorMsg').style.display = 'block';
setTimeout(function() {
document.getElementById('errorMsg').style.display = 'none';
}, 6500);
}
} catch (err) {}
},
false
);
// loads google analytics scripts
loadScript();
},
false
);
const getCarrierUrl = (carrier, trackingId) => {
let url = null;
switch (carrier) {
case 'ups':
url = `https://www.ups.com/track?tracknum=${trackingId}`;
break;
case 'fedex':
url = `https://www.fedex.com/apps/fedextrack/?tracknumbers=${trackingId}`;
break;
case 'usps':
url = `https://tools.usps.com/go/TrackConfirmAction?tLabels=${trackingId}`;
break;
case 'dhl':
url = `https://www.dhl.com/en/express/tracking.html?AWB=${trackingId}`;
break;
default:
url = 'https://www.google.com';
break;
}
return url;
};
function gtag() {
dataLayer.push(arguments);
}
function loadScript() {
var scriptTag = document.createElement("script");
scriptTag.src = "https://www.googletagmanager.com/gtag/js?id=UA-102573905-2";
scriptTag.async = true;
document.querySelector("head").append(scriptTag);
window.dataLayer = window.dataLayer || [];
gtag("js", new Date());
gtag("config", "UA-102573905-2");
}