Reputation: 135
Is there a way of getting the value of the title of the page from a Google Extension?
Upvotes: 0
Views: 15567
Reputation: 602
Notice that the above method mentioned by CMS is deprecated since Chrome 33.
You don't really need to specify the tabs
permission in your manifest file since what you're doing here isn't some advanced action. You can perform most of the tabs
actions without specifying the permission; only for some certain methods will you need to.
The new way of querying the currently selected tab is by the following code:
chrome.tabs.query({ active: true }, function (tab) {
// do some stuff here
});
This will give you the selected tabs in all windows, if you have multiple windows open. If you want to get only the selected tab in the current window, use the following:
chrome.tabs.query({ active: true, currentWindow: true }, function (tab) {
// do some other fanciful stuff here
});
For more details, refer to https://developer.chrome.com/extensions/tabs#method-query
Upvotes: 2
Reputation: 827316
At first you should declare the tabs
API permission in your manifest.json
:
{
"name": "My extension",
...
"permissions": ["tabs"],
...
}
Then you will be able to use the tabs API, you are looking for the chrome.tabs.getSelected(windowId, callback)
method.
To get the selected tab of the current window, you can simply pass null
as the windowId
.
This method will execute the callback function passing a Tab object as its first argument, where you can simply get the title
property:
chrome.tabs.getSelected(null,function(tab) { // null defaults to current window
var title = tab.title;
// ...
});
Upvotes: 8