In the Firefox example of browser extensions, the popup script sends a message to the content script using the following:
browser.tabs.sendMessage(tabs[0].id, {
command: "beastify", beastURL: url }
Looking through the MDN documentation doesn't give a clear cut answer as to what tabs really is. It seems like its an array containing all the tab objects in the browser. But how do I know what tabs[0] is? and what about the rest of the array?
Is using tabs[0] equivalent to finding the current active tab?
This is from Firefox documentation
browser.tabs
.query({ active: true, currentWindow: true })
.then(beastify)
....
function beastify(tabs) {
browser.tabs.insertCSS({ code: hidePage }).then(() => {
let url = beastNameToURL(e.target.textContent);
browser.tabs.sendMessage(tabs[0].id, {
command: "beastify",
beastURL: url
});
});
}
As you can see the beastify function is called with list (array) of tabs from browser.tabs.query()
Because only one tab in the same window can be active, it's save to say that the list will contain only single item, hens we can safely use tabs[0] to access first and only item in the array.
Related
I have a problem with the chrome extension project I'm working on.
Generally, it has only one purpose - to remove the addresses from browsing history that is included in the array. (later on, I'm going to add some UI for it ). The chrome background console doesn't show any errors - additionally, I did it just like the documentation said.
How to make that chrome extension background script removing array elements from history.
console.log("hi");
// delete URL
const arr = [
"https://www.facebook.com/",
"https://www.messenger.com/",
"https://www.netflix.com/",
"https://www.youtube.com/"
]
arr.forEach( site => {
console.log(site)
chrome.history.deleteUrl({ url: site });
});
I am working on a chrome extension, and I need information about the active tab (when I say "active", I mean the tab that I am looking at in the current window that is focused).
Using the chrome.tabs api, I should be able to do something like the following to get what I want:
function getActiveTab() {
var activeTabInfo = {"currentWindow": true, "active" : true};
return chrome.tabs.query(activeTabInfo,function (tabs) {
return tabs[0];
});
}
However, when I log the length of tabs within the callback, I'm getting a length of 0. I modeled this snippet after How to fetch URL of current Tab in my chrome extension using javascript, but can't seem to get it to work.
Any thoughts?
I was messing around with communicating inside a Google Chrome extension and was using the following guide: https://developer.chrome.com/extensions/messaging
It used to work but I have encountered an error :
Error in response to tabs.query: TypeError: Cannot read property 'id'
of undefined
I compared my code and the Google Chrome code and I can't seem to find why my code produces that error:
chrome.tabs.query({active: true, currentWindow: true}, function(tabs) {
chrome.tabs.sendMessage(tabs[1].id, {fen: request.needMove}, function(response) {
//console.log(response.farewell);
});
});
Here is where I send it to:
chrome.runtime.onMessage.addListener(
function(request, sender, sendResponse) {
console.log("recv FEN : " + FEN);
FEN = request.fen;
setCookie("FEN_SET", "true" , 1);
setFEN(FEN);
});
I can't fix that error, whatever I try it stays the same.
"Cannot read property of undefined" is implying that 'tabs' is undefined as far as I understood but I don't understand why it works in the Google example and here it doesn't.
Another Q :
If I'm trying to send it to tabs[1] does that mean it's the tab in the second position, or am I interpreting it wrong?
tabs is the list of all tabs (regardless of position) that pass the filter.
Your query is {active: true, currentWindow: true}, so normally it should be just 1 tab (as there is at most 1 current window with exactly 1 active tab).
So you need the first element, which is tabs[0].
tabs[1] will always be undefined with this query.
Cases when tabs will be empty empty used to be exceedingly rare (Chrome running in background with no windows open).
However, with a recent change the API will not return the Dev Tools tab. So if you're debugging your extension and the Dev Tools window is open and focused, the array will be empty. You should check for that.
I am using the CrossRider api to get the opened tabs, using there API I can get the title of the links in my bookmarks, however with there api I cannot use how to get the title of the urls in my opened tabs, I can just get the URL.
Does anyone know if this is achievable, if not then if there is some other way. Current I have to call a php script to make calls to the URLs and extract the title, this is getting too slow with a lot of tabs open
It would be great if you included a code snippet to show how your code works. However, in the absence of a snippet, I'm guessing that you are working in the background scope using an API method such as appAPI.tabs.getAllTabs. As you correctly surmised, the method does no not provide the title of the URL.
There are several way to work around this, on of which you mentioned, however, I prefer getting the information from the tab itself through messaging. For example, assuming you still need the information in the background scope:
background.js:
appAPI.ready(function($) {
appAPI.message.listener(function(msg) {
if (msg.action==='tab-info') {
doSomething(msg.data);
}
});
appAPI.message.toAllTabs({action:'get-tab-info'});
});
extension.js:
appAPI.ready(function($) {
appAPI.message.listener(function(msg) {
if (msg.action==='get-tab-info') {
appAPI.message.toBackground({
action:'tab-info',
data: {
tabId: appAPI.getTabId(),
url: appAPI.dom.location.href,
title: document.title
}
});
}
});
});
[Disclosure: I am a Crossrider employee]
Is there a way of getting the value of the title of the page from a Google Extension?
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;
// ...
});
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