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.
Related
I'm making a chrome extension and using page_action instead of browser_action because I need this extension for only one specific url. I'm using declerativeContent for activating the page_action;
// When the extension is installed or upgraded ...
chrome.runtime.onInstalled.addListener(function() {
// Replace all rules ...
chrome.declarativeContent.onPageChanged.removeRules(undefined, function() {
// With a new rule ...
chrome.declarativeContent.onPageChanged.addRules([
{
// That fires when a page's URL contains ...
conditions: [
new chrome.declarativeContent.PageStateMatcher({
pageUrl: { hostEquals: 'www.example.com', schemes: ['https'] },
})
],
// And shows the extension's page action.
actions: [ new chrome.declarativeContent.ShowPageAction() ]
}
]);
});
});
In normal tabs of chrome, there is no problem. Page_action works exactly the way I want. But in pop-up windows, there is a problem. I mean, there is page that contains links to pop-up pages. When I click to them, pop-up windows open but I can't see the page_actions in the address bar.
What could be the problem?
Unfortunately, Google Chrome extensions doesn't provide page_action icon on the adress bar in pop-up windows. But still, extension works on that window. You should think other ways to make your extension functional.
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;
});
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.
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.
I have built a simple chrome application which has a background page that shows desktop notifications when a new article is available.
If a user clicks on the notifications when a browser window is open, then they are taken to the new article page and everything is right with the world.
If however the browser is closed when the notification is shown, then the href does not open the browser. I have also tried to capture the click and set a window.open, but that isn't working either.
As an aside, it would also be good if I could check to see if an instance of my app is already open and use that window/tab when the notification is clicked, but that's secondary to the first problem.
Any help would be great!
Thanks
Check if chrome window is open. if open the create in new tab else oepn new window.
chrome.windows.getCurrent(function(currentWindow) {
if (currentWindow != null) {
return chrome.tabs.create({
'url': url
});
} else {
return chrome.windows.create({
'url': url,
'focused': true
});
}
});
Have you tried, creating a window then tab using the Chrome API? For example:
linkDOM.addEventListener('click', function() {
chrome.windows.create({url: 'http://someurl.com'});
}, false);
I usually do the above when programmatically opening a link from extension pages.