0

How can I pass a variable to chrome.tabs.create?

I'm trying to set up event listeners to my anchors, but I'm creating them inside a for loop:

for (var i = 0; i < links.length; i++){
    if(links[i][0] === text){
        var a = document.createElement('a');
        a.setAttribute('href', links[i][2]);
        a.setAttribute('class','links_anchor');
        a.innerHTML = links[i][1];
        
        test = links[i][2];
        a.addEventListener('click', function(){
            chrome.tabs.create({
                url: test,
                active: false
            });
        });

        document.getElementById('links_content').appendChild(a);
    }
}

How can I pass a variable to the url parameter? What's happening now is that all the links have the same action. E.g. When I click any of them it opens the last URL from the array.

1 Answers1

0

If you mean add a query parameter like so: https://example.com/?foo=bar

Seeing as you have the url in test you could turn chrome.tabs.create({ url: test, active: false });

into

chrome.tabs.create({ url: test + "?foo=bar&bar=foo" active: false });

Jonathazn
  • 11
  • 1
  • 2