Repository navigation
Expand file tree
/
Copy pathbackground.js
More file actions
64 lines (58 loc) · 2.33 KB
/
Copy pathbackground.js
File metadata and controls
64 lines (58 loc) · 2.33 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
chrome.tabs.onUpdated.addListener((tabId, changeInfo, tab) => {
if ( (tab.url.indexOf("takealot.com") > -1 ) && (tab.url.indexOf("PLID") > -1) ) {
chrome.tabs.sendMessage(tabId, {message: "OnTakealot"});
}
}
);
chrome.runtime.onInstalled.addListener((details) => {
// Check if values already exist, and set defaults only if they don't
chrome.storage.local.get(['enableLittleButton', 'theme'], (result) => {
if (result.enableLittleButton === undefined) {
chrome.storage.local.set({ enableLittleButton: false });
}
if (result.theme === undefined) {
chrome.storage.local.set({ theme: 'modern' });
}
});
if (details.reason === "install" || details.reason === "update") {
const optionsUrl = chrome.runtime.getURL('options.html');
const urlWithParam = `${optionsUrl}?reason=${details.reason}`;
chrome.tabs.create({ url: urlWithParam });
}
});
async function fetchData(url) {
//we want to extract the 8 digit code after PLID from the url and return https://www.servaltracker.com/products/PLIDXXXXXXXX/
//Loop through url and find the index of PLID
for (let i = 0; i < url.length-5; i++) {
if (url[i] === "P" && url[i + 1] === "L" && url[i + 2] === "I" && url[i + 3] === "D") {
//we found the index of PLID
//now we want to extract the 8 digit code after PLID
let code = "";
for (let j = i + 4; j < i + 12; j++) {
code += url[j];
}
return `https://www.servaltracker.com/products/PLID${code}/`;
}
}
//If we finish the for loop that means we did not find the index of PLID
//so we return null
return null;
}
const OnClickEventHandler = () => {
chrome.tabs.query({active: true, lastFocusedWindow: true}, tabs => {
let url = tabs[0].url;
fetchData(url).then(data => {
if (data != null) {
chrome.tabs.create({url: data});
}else{
alert("Doesn't seem to be a valid takealot url, ensure you have the product page open");
}
});
});
}
//when I receive a message from the content script
chrome.runtime.onMessage.addListener((request, sender, sendResponse) => {
if (request.message === "ButtonClicked") {
OnClickEventHandler();
}
});