Chrome Extension: What is the active tab in 2 opened windows? - google-chrome

I have an extension that needs to know what URL is on the active tab, but the problem is that when I open a second chrome window there are 2 active tabs, in the webmaster tools it doesn't give me any indication of what window I'm actually on.
I was actually on the 2nd window when I took this screenshot.
The code that I am using is:
chrome.tabs.query({'active': true}, function (tabs) {
app.tabInfo = tabs[0];
});
But the good code would have been app.tabInfo = tabs[1]; but I need to know that I need to pick that one. So how can I know?
Thank you.

Make your query to select the last focused Window:
chrome.tabs.query({ active: true, lastFocusedWindow: true }, function (tabs) {
//...
});
Note: Better don't take currentWindow: true because:
The current window is the window that contains the code that is currently executing. It's important to realize that this can be different from the topmost or focused window.
Source: http://developer.chrome.com/extensions/windows.html#current-window

Use chrome.windows.getCurrent() (or .getLastFocused(), right below it) to get the current window, then look for the active tab in the tabs property of the returned window.

Related

How to detect if a tab is unloaded after you restart the Chrome browser?

Let's say I have a bunch of tabs opened in Chrome, I close the browser and then reopen it. Those tabs will remain unloaded, until I click on them, then they will load automatically. This is a native feature of the browser to save memory.
I am looking for a way to check when tabs are in this specific state, before I click on them. I have tried the two properties .status and .discarded mentioned in the chrome.tabs api, which provide information if the tabs are unloaded or discarded:
https://developer.chrome.com/docs/extensions/reference/tabs/
but the values they give those properties are always the same, regardless if those tabs are completely unloaded after a restart or fully loaded after I click on them:
.status = "complete"
.discarded = "false"
And now I am stuck. I dont know how to solve this.
Any help will be appreciated. Thank you.
ps. I am not using any tab suspension addons.
You can check the discarded or status property of a tab, a discarded tab will have its status as “unloaded”:
chrome.runtime.onStartup.addListener(()=>{
chrome.tabs.query({currentWindow: true}, (tabs)=>{
tabs.forEach(tab =>{
if(tab.discarded === true || tab.status === “unloaded” ){
//Do something
}
})
})
});

How can I know if my Chrome Extension is called from a New Tab page?

My Chrome Extension has a popup with a few links, which I would like to be opened in the current tab if it's a New Tab page, or open in a new tab otherwise. So I believe I need to know the active tab's URL. Or is there another way to identify a New Tab?
I'd like to use the "activeTab" permission rather than "tabs" - I want the user to see as few permissions listed as possible.
The only way I've found to identify the tab's URL is by using a background page and
chrome.browserAction.onClicked.addListener(function(tab))
But this is not compatible with having a popup defined in the manifest. I can set the popup page programatically, but I can't see a way to make the popup appear. Is there a way to do that?
When I have default_popup defined in the manifest I use
document.addEventListener('DOMContentLoaded', function ())
to launch the related code, so no reference to the active tab is available. Is there another way to run the code, or to get the active tab?
Thanks.
The activeTab permission allows you to "Get the URL, title, and favicon for that tab via an API that returns a tabs.Tab object". So, to get the current tab URL from the popup you can do:
chrome.tabs.query( {active:true, currentWindow: true}, function(tabs) {
currentUrl = tabs[0].url;
});

How to stop chrome tab focus?

I am writing a chrome extension which refreshes pages after a certain time interval. I got it working fine, but a new problem occurred..
Whenever the page refreshes the tab gets highlighted (starts blinking) or else comes in focus. Ie it is getting focused on.
Now this works differently depending on the chrome browser state, but the core issue is the same:
1. If user switches tab to another one (suppose writing an email in gmail.com) while waiting for page to refresh itself, on refresh the current mail tab will get outfocused (so he wont be able to continue writing his email without clicking on the window first)
If the user swithces to any other application on his workspace (like my computer, outlook etc), on refresh the chrome icon will start blinking on the taskbar (highly annoying)
If the user has multiple chrome windows open with a tab each running the refresh code, then on refresh that window will get focus (come at top). This will repeat for all chrome windows whenever there specific refreshes occur.
Steps to reproduce the problem:
Write a simple extension
In the background page, create a new tab with:
chrome.tabs.create({ url: "https://drive.google.com/", active: false})
Now in a loop, after every minute, refresh this tab with:
chrome.tabs.update(tab_id, {url: "https://drive.google.com/",
active: false,
highlighted: false});
Once you start this code, either minimize the chrome window or shift your workspace and focus on something else.
Every time tabs.update() gets called, the Chrome window either unminimizes from task bar, or gets the focus on it.
What i want is to remove this focus whenever the page refreshes.
If anyone can please help me with this i would be very greatful.
Thanks,
Umair
EDIT:
code for my background.js file
chrome.webNavigation.onErrorOccurred.addListener(function (_errorDetails) {
var myUrl = 'myPage.htm';
chrome.tabs.update(_errorDetails.tabId, { url: myUrl, active: false,
highlighted: false, selected: false },
function (_tabDetails) {
chrome.windows.update(_tabDetails.windowId, { drawAttention: false });
});
});
Iv tried to make all related parameters false to stop focus.
Also on myPage.htm the function reload() is called on pageload.
function reloadPage() {
//code for 10 second delay
location.reload();
}
myPage.htm is itself a simple page showing few lines of text like 'Unable to load page' etc.
I changed the code and got it working for me as per my requirements.
What i did was instead of using the reload function to refresh page, i used the message passing technique of the chrome extension to send a message (containing url and tabID) from my js to background.js and update the relevant tab there with the url.
I know this is not a perfect solution, but it worked for me, therefore im sharing it.

how to get reference to current tab of current window?

I am a newbie for Google Chrome Extensions development ,I wonder how to get a reference to the current tab of current window .
I used chrome.tabs.query({'active': true} but it doesn't work when multiple windows opened .
Every window that has a tab has one active tab, so if there are multiple windows open, you need to specify which window you want.
To get the window that the current script is calling from, use:
chrome.tabs.query({ active: true, windowId: chrome.windows.WINDOW_ID_CURRENT }, function (tabs) {
// Do something with tabs
});
If, however, by "current window", you mean the front-most focused window shown to the user, use:
chrome.tabs.query({ active: true, lastFocusedWindow: true }, function (tabs) {
// Do something with tabs
});
For further info, see chrome.tabs.query and Chrome's definition of current window.

Chrome extension, focus on the page instead of omnibox after selected tab is updated?

I created an extension called quickmarks which will open bookmark by keyword at currently selected tab. I am
using omnibox to select the bookmark (chrome.omnibox.onInputEntered),
and chrome.tabs.update API to open bookmark's url in current tab, by
providing the url in updateProperties. However after the tab is
updated, focus still remains in omnibox, which make the user-
experience not as good as I desired. So is there a way to set the
focus to the page, instead of the omnibox.
Btw, I have tried to open a new tab by using chrome.tabs.create. The
page will be focused instead of omnibox, which is my desired
behaviour.
Thanks.
Using chrome.tabs.update(tab.id, {url: url, selected: true}); does the trick for me.
The accepted answer no longer works in Chrome 31. I had hoped it was a simple matter of the selected property being deprecated but the replacement highlighted property did not assign focus to the tab's content either.
I was only able to steal focus from the Omnibox by closing the current tab and then creating a new tab in its place. Here's the code that works in Chrome 31:
chrome.tabs.getSelected(null, function(tab) {
chrome.tabs.remove(tab.id, function() {
chrome.tabs.create({url:url, active:true});
});
});
While this is certainly not ideal, the current tab is closed and a new one opened so fast you barely notice any difference.
Dave Teare is right that this no longer works in current version of Chrome, however his method did not work for me. It seemed like the chrome.tabs.create did not get called after the tab was removed.
I use the Chrome extension iChrome and I wanted it to be selected when I created a new tab, so I installed another extension that redirects new tabs to iChrome. It unfortunately used the same deprecated "selected:true" method.
From what I can tell there is currently no way to do this cleanly, you cannot have Chrome make the updated tab's input focused nor can you have it select the text in onmibar so you cant just start searching after creating a new tab. So this is what I came up with:
chrome.tabs.create({url:url, active:true}, function(){
chrome.tabs.remove(tab.id);
});
Definitely still not ideal, and you can see a flash when it closes old tab, but it works. The input is focused.