Chrome Tabs API; Javascript code to switch tabs

16,757 views
Skip to first unread message

Matthew Kaufer

unread,
Oct 28, 2013, 7:07:12 AM10/28/13
to chromium-...@chromium.org
Hi,

I was trying to make an app the other day that could switch to the next tab in Chrome via an extension. I was looking in the Chrome Tabs API, but I couldn't figure out how to implement it through there. Does anybody know the code to switch tabs with Javascript in a Chrome extension?   Or could anybody point me in the right direction? I've tried the Tabs API, but I couldn't figure it out from there.

Thanks.

PhistucK

unread,
Oct 28, 2013, 7:18:49 AM10/28/13
to Matthew Kaufer, Chromium-extensions
Something like that (untested)?

// Getting a list of tabs of the current window.
chrome.windows.getLastFocused(
 // Without this, window.tabs is not populated.
 {populate: true},
 function (window)
 {
  var foundSelected = false;
  for (var i = 0; i < window.tabs.length; i++)
  {
   // Finding the selected tab.
   if (window.tabs[i].active)
   {
    foundSelected = true;
   }
   // Finding the next tab.
   else if (foundSelected)
   {
    // Selecting the next tab.
    chrome.tabs.update(window.tabs[i].id, {active: true});
    return;
   }
  }
 });


PhistucK


--
You received this message because you are subscribed to the Google Groups "Chromium-extensions" group.
To unsubscribe from this group and stop receiving emails from it, send an email to chromium-extens...@chromium.org.
To post to this group, send email to chromium-...@chromium.org.
Visit this group at http://groups.google.com/a/chromium.org/group/chromium-extensions/.
To view this discussion on the web visit https://groups.google.com/a/chromium.org/d/msgid/chromium-extensions/fdfa62e4-8910-4fa0-bd7f-65df6287fb67%40chromium.org.
For more options, visit https://groups.google.com/a/chromium.org/groups/opt_out.

Matthew Kaufer

unread,
Oct 28, 2013, 5:11:08 PM10/28/13
to chromium-...@chromium.org, Matthew Kaufer
I'm using your code in this context.

$(document).mousedown(function(){



chrome.windows.getLastFocused(
 // Without this, window.tabs is not populated.
 {populate: true},
 function (window)
 {
  var foundSelected = false;
  for (var i = 0; i < window.tabs.length; i++)
  {
   // Finding the selected tab.
   if (window.tabs[i].active)
   {
    foundSelected = true;
   }
   // Finding the next tab.
   else if (foundSelected)
   {
    // Selecting the next tab.
    chrome.tabs.update(window.tabs[i].id, {active: true});
    return;
   }
  }
 });



});

jQuery works and the manifest.json of my extension has the tabs permission. However, the tabs aren't switching on click.

Manifest.json for reference: 


{
  "name": "Switch",
  "version": "1.0",
"manifest_version": 2,

  "permissions": [
    "tabs", "http://*/*"
  ],
  "content_scripts": [
    {
      "matches": ["http://*/*", "https://*/*"],
      "js": ["jquery-1.10.2.min.js", "script.js"],
      "run_at": "document_end"
    }
  ]
}

PhistucK

unread,
Oct 28, 2013, 5:19:18 PM10/28/13
to Matthew Kaufer, Chromium-extensions
Note that this code must be run in an extension page (a background page, a browser/page action popup page or a tab with a chrome-extension://... URL). It will not work in a regular web page or a content script.
A content script can sendMessage to a background page that will run this code (minus the event handler) onMessage.


PhistucK


--
You received this message because you are subscribed to the Google Groups "Chromium-extensions" group.
To unsubscribe from this group and stop receiving emails from it, send an email to chromium-extens...@chromium.org.
To post to this group, send email to chromium-...@chromium.org.
Visit this group at http://groups.google.com/a/chromium.org/group/chromium-extensions/.

Matthew Kaufer

unread,
Oct 28, 2013, 5:38:45 PM10/28/13
to chromium-...@chromium.org, Matthew Kaufer
I acknowledge this. It's running in a file called script.js which is inside of my extension. Could it possibly be the code, or did I set up something wrong in my manifest? I've tested the jquery click function in this extension, and it can detect and alert when I click, so the javascript is positioned correctly in the extension. 
Any other thoughts?
Also, thanks for helping me so far.

PhistucK

unread,
Oct 29, 2013, 2:37:54 AM10/29/13
to Matthew Kaufer, Chromium-extensions
Your manifest shows that script.js is used as a content script, which as I mentioned, cannot call these APIs and must sendMessage to a background page that will run that code. Am I misunderstanding?


PhistucK


--
You received this message because you are subscribed to the Google Groups "Chromium-extensions" group.
To unsubscribe from this group and stop receiving emails from it, send an email to chromium-extens...@chromium.org.
To post to this group, send email to chromium-...@chromium.org.
Visit this group at http://groups.google.com/a/chromium.org/group/chromium-extensions/.

Matthew Kaufer

unread,
Oct 29, 2013, 7:01:38 AM10/29/13
to chromium-...@chromium.org, Matthew Kaufer
How would I implement it as a background script?

Matthew Kaufer

unread,
Oct 29, 2013, 7:02:50 AM10/29/13
to chromium-...@chromium.org, Matthew Kaufer
I would just add

 "background": {
    "scripts": ["script.js"]
  },

to the manifest, correct?

Thank you very much for your help.

Paulie

unread,
Oct 29, 2013, 7:33:12 AM10/29/13
to chromium-...@chromium.org, Matthew Kaufer
Nearly correct. Your background script would be a different js file to your script.js file. Just call it background.js.

Then read up on message passing:

http://developer.chrome.com/extensions/messaging.html

Just the stuff under the "Simple one-time requests" on that page is what you need.
Reply all
Reply to author
Forward
0 new messages