-
Notifications
You must be signed in to change notification settings - Fork 0
/
content.js
68 lines (56 loc) · 2.29 KB
/
content.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
// Extract product ID from URL
function extractProductIdFromUrl(url) {
const urlParts = url.split('/');
const productIdIndex = urlParts.findIndex(part => part.startsWith('PLID'));
return productIdIndex !== -1 ? urlParts[productIdIndex] : null;
}
// Create and style the button
function createButton(productId) {
const iconUrl = 'https://www.servaltracker.com/static/images/favicons/serval_favicon_48x48.png';
const icon = document.createElement('img');
icon.src = iconUrl;
icon.alt = 'Icon';
icon.width = 24;
icon.height = 24;
const buttonText = document.createElement('span');
buttonText.innerText = 'Track on Serval Tracker';
const flexContainer = document.createElement('div');
flexContainer.style.display = 'flex';
flexContainer.style.alignItems = 'center';
flexContainer.style.justifyContent = 'center';
flexContainer.appendChild(icon);
flexContainer.appendChild(buttonText);
const button = document.createElement('a');
button.href = `https://www.servaltracker.com/products/${productId}`;
button.target = '_blank';
button.style.display = 'block';
button.style.marginTop = '10px';
button.style.padding = '10px';
button.style.backgroundColor = 'gold';
button.style.color = 'black';
button.style.textAlign = 'center';
button.style.textDecoration = 'none';
button.style.borderRadius = '5px';
button.appendChild(flexContainer);
return button;
}
// Add the button after the product title
function addButton() {
const productId = extractProductIdFromUrl(window.location.href);
if (productId !== null) {
const titleElement = document.querySelector('.product-title');
if (titleElement && titleElement.nextElementSibling.tagName !== 'A') {
const button = createButton(productId);
titleElement.parentNode.insertBefore(button, titleElement.nextSibling);
}
}
}
// Callback function for MutationObserver
function callback(mutationsList, observer) {
addButton();
}
// Create a MutationObserver object and start observing the DOM changes
const observer = new MutationObserver(callback);
observer.observe(document.body, { childList: true, subtree: true });
// Add button on page load
addButton();