2

I am new to Javascript & I want to get the ID of the currently opened tab of google chrome browser, using the console of chrome developer tools. I tried the code below, but got an exception. How do I make this work ? Do I have to learn something more just for this little thing ?

chrome.tabs.getCurrent(function(tab){ console.log(
JSON.stringify(tab,null, 2)); });

Exception:

Uncaught TypeError: Cannot read property 'getCurrent' of undefined(…)
VM1201:1 Uncaught TypeError: Cannot read property 'getCurrent' of undefined(…)

Source: Chrome how to get the current tab's id?

EDIT: These dont not work either and fail due to the same "undefined" error.

how to get current tabId from background page

How can I get the current tab URL for chrome extension?

Community
  • 1
  • 1
MasterJoe
  • 2,103
  • 5
  • 32
  • 58
  • 1
    Hello, i think you can find the answer here : http://stackoverflow.com/questions/15034859/chrome-tabs-returns-undefined-in-content-script – Redrif Jul 28 '16 at 07:40
  • 1
    @Redrif - there is no answer in that page, no clues, no tutorial and no working code. – MasterJoe Jul 28 '16 at 07:43
  • 2
    _chrome.tabs is only available in background scripts and popup scripts. If you wanna to use chrome.tabs then pass message from content_script to background script and play with chrome.tabs_ And also there is this link : https://developer.chrome.com/extensions/content_scripts#pi Your problem is, that there is no property tabs in your chrome object. – Redrif Jul 28 '16 at 07:46
  • 1
    @Redrif - "pass message from content_script to background script". How do I do that ? What is content_script ? Content scripts - https://developer.chrome.com/extensions/content_scripts ??? – MasterJoe Jul 28 '16 at 07:48
  • I just gave you link where you can read all about content scripts and what they do. Do you want a working code example? – Redrif Jul 28 '16 at 07:52
  • @Redrif - Thanks for the link. Looks like its only for developing extensions. Seems that I cannot do what I want using the console. Can I do it by injecting javascript into a tab ? – MasterJoe Jul 28 '16 at 07:58
  • I think this will only work with scripts injected **by** an extension – AnimiVulpis Jul 28 '16 at 11:42
  • @AnimiVulpis - Is it a good idea to make a simple extension only to get the current tab id ? I want to get the current tab id so that I can automate testing of a chrome extension as mentioned here. http://stackoverflow.com/questions/38647629/how-do-i-automate-browser-extension-testing-with-selenium – MasterJoe Jul 29 '16 at 00:56
  • I think you could write an extension that can give you the tab id of the open tabs. **But** I am not sure if this will work with tabs created by another extension (guessing this is important to you because of your other question) – AnimiVulpis Jul 29 '16 at 05:48

0 Answers0