0

I am trying to develop a chrome extension which saves the url of webpages opened in all tabs and then load them whenever needed. Now I know content scripts, background scripts and popup.js. Content scripts mainly deal with the content of the loaded webpage and they have less chrome api interactions, background scripts are executed in an isolated environment and we can use all chrome api methods, popup.js is simply javascript that runs in context of popup.html.

Now here is my problem, I have a button in popup.html named "save" and on click of that button I want to save all the webpage urls opened in multiple tabs under one window. How can I do that? Should I write a content or a background script?

Sorry for my noobish question. I am new to chrome api. Any help/suggestions?

Rahul Padalkar
  • 125
  • 1
  • 11
  • 1
    Possible duplicate of [Get urls of all open tabs in chrome, and send it to a web service](http://stackoverflow.com/questions/11114395/get-urls-of-all-open-tabs-in-chrome-and-send-it-to-a-web-service) – Ghostship Sep 15 '16 at 13:32
  • @Ghostship No actually. Read the full question. I know the title is same, i will rephrase it. – Rahul Padalkar Sep 15 '16 at 13:43
  • first, read the documentation about messaging and how to communicate between popup and background. other questions already answer that communication part. do not ask friends to upvote, that just draws more attention from moderators like me. – Zig Mandel Sep 15 '16 at 14:11
  • @ZigMandel Thanks for the help. Sorry, but I didn't ask anyone to upvote my post. Even if you think I did, I am sorry. – Rahul Padalkar Sep 15 '16 at 14:38
  • Well, you have more or less already answered your question within the Question. Do you need access to the content of the webpages to do what you want (content script)? Do you need access the the full Chrome API to do what you want (background script)? Do you need either? Does your code within your popup have sufficient access to the APIs and information to do what you desire? – Makyen Sep 15 '16 at 14:49

1 Answers1

2

Neither content script or background page is needed. You could do that just in popup.js, since popup page actually runs in the same context with extension.

In your popup.js, just call chrome.tabs.query to get tab info, including url (you would need to declare tabs permissions in manifest.json). If you want to specify window id, either use WINDOW_ID_CURRENT or retrieve it through other ways (depends on your logic)

chrome.tabs.query({ windowId: YOUR_WINDOW_ID }, (tabs) => {
    tabs.forEach((tab) => console.log(tab.url));
});
Haibara Ai
  • 10,703
  • 2
  • 31
  • 47