Search code examples
javascriptgoogle-chromegoogle-chrome-extension

I'm trying to parse all the urls of the tabs I have open


I'm trying to copy the url's of all the open tabs with javascript and then put the urls into a html textarea, however it's not working. This is my code.

            chrome.windows.getCurrent({"populate":true}, function(currentWindow) {
               var tabURLs = [];
               var tabs = currentWindow.tabs;
               for (var i=0; i<tabs.length; i++) {
                   tabURLs.push(tabs[i].url);
               }
               var textArea = document.getElementById("thing");
               var text = textArea.value;
               tabURLs.forEach(item => text += item);
            });
        };

Keep in mind this is a google chrome extension, let me know how I can fix this!


Solution

  • Your code is modifying a copy of the element's value, not the element itself.
    You need to replace text += with textArea.value +=

    P.S. The code can be shortened:

    chrome.tabs.query({}, tabs => {
      document.getElementById("thing").value = tabs.map(t => t.url).join('\n');
    });