In my quest to develop an array stored in chrome.sync
that holds the URLs of unique pages visited by a user, I have encountered a hurdle. Adding URLs to the array is not problematic, but determining whether a URL already exists within the array is proving to be a challenge. Here is my current approach:
function getURL(array) {
chrome.tabs.query({active: true, lastFocusedWindow: true, currentWindow: true}, tabs => {
let tabUrl = tabs[0].url;
addArr(tabUrl, array);
});
}
function addArr(tabUrl, array) {
array.map((x) => {
let urlCheck = x.url;
console.log(urlCheck);
if(urlCheck != tabUrl) {
array.push(tabUrl);
chrome.storage.sync.set({data: array}, function() {
console.log("added")
});
}
})
}
The code functions properly when I eliminate the map functionality and only retain:
array.push(tabUrl);
chrome.storage.sync.set({data: array}, function() {
console.log("added")
});
Strangely, when inspecting the array using dev tools, no errors are found - just an empty data array. However, upon right-clicking on the extension's popup, I receive the following error message:
Error handling response: TypeError: Cannot read properties of undefined (reading 'url') at chrome-extension://jkhpfndgmiimdbmjbajgdnmgembbkmgf/getURL.js:31:30
That line 31 in getURL.js? It pertains to retrieving the active tab, like so:
let tabUrl = tabs[0].url;
This issue is compounded by the fact that this line is part of a separate function entirely.