Using background.js in a Chrome Extension can you detect the focus moving from one tab to another?
I'm talking about a very specific situation - not just using onactivated to detect that the user has clicked a different tab.
If I have two Chrome windows open (same user account), each with a different webpage showing, and the user simply clicks to bring one to the front (so you can scroll and read the page), the onactivated event does NOT fire. It only seems to fire if you move between tabs in a single window.
I can capture mousedown etc, but if you just click once to bring a window to the front these events don't fire either (a second click fires mousedown).
Seems a strange oversight...I would have thought onactivated would cover this scenario - the user, they've activated it.
In fact, onactivated doesn't fire when you click that second time. I guess it's specific to switching tabs in a single window.
chrome.tabs.onActivated fires when the active tab in a window changes [docs], meaning it's per window so it won't fire unless the user switches to a different tab within that tab's window.
Solution: use chrome.windows.onFocusChanged that fires when a Chrome window is activated [docs].
Reports chrome.windows.WINDOW_ID_NONE if all Chrome windows have lost focus. Note: On some Linux window managers, WINDOW_ID_NONE is always sent immediately preceding a switch from one Chrome window to another.
chrome.windows.onFocusChanged.addListener(windowId => {
// do something
});
You can specify the type of windows to report:
chrome.windows.onFocusChanged.addListener(windowId => {
// do something
}, {windowTypes: ['normal']});
Related
I have a web page, when I click one button(by both using mouse click or using keyboard with tab and press), it will show some items, normally, it works perfect.
However, if I
1> open windows magnifier with 200%,
AND
2> use keyboard with tab and press
to trigger the button,
the items still shows correctly,
but the whole page moves back and forth in horizon automatically until I move mmouse or press any key.
++++++
I got the root cause:
As I input the follow code
document.addEventListener('focus',
function()
{ console.log('focused: ', document.activeElement)
},
true);
in console to monitor the focused element,
it shows the focus are switch between two elements,
however, I try to set the two element with tabindex = 0 or -1,
and all the 4 combinations,
but it doesn't work.
Are you using "docked", "full screen", or "lens" view for the magnifier?
I'm guessing "full screen" because in that mode, as you tab through a page, the magnifier will try to keep the keyboard focus within view. You might have an issue where your focus is moving between two elements that both can't be displayed in the magnifier at the same time. Usually it will just move the magnifier to the element that has focus.
I'd suggest trying "Docked" and "Lens" view first to see if you still have an issue.
We are automating a web application using UFT/QTP. The issue is that UFT works only on those web objects which are seen in browser window part. If any object/element is at the bottom of the page and require scroll down in window to be seen. UFT is not able to work on those objects. We have written code to page down to work on those objects. It works fine. But sometimes when page is very lengthy and we don't know at what position that object will be like in middle of the page, or far bottom of the page etc. In that case, only page down does not work. Is there any way that we can make the object visible on the basis of object properties? That means we can bring object in front of the window.
If your scrolling pageup or pagedown code is already working, why don't you use a loop...
Set obj = Browser(..).page(..).Link(..) ' use your object
While obj.getROProperty("visible") = False
'Scroll code
Wend
UFT doesn't depend on whether the object is in view or not. If an object is scrolled out of view UFT will automatically scroll it into view before performing any action upon it (e.g. Click).
The behaviour you're describing may be caused by the fact that the web site dynamically adds objects to the DOM when the page is scrolled. If this is the case then UFT has no way to know what will cause the object to be added to the DOM and you have to scroll for it they you've been doing.
Sorry I couldn't bring better news :(
I had once faced a similar issue. For me, changing the Replaytype setting to mouse events worked.
Try changing the ReplayType to Mouse events just before performing any operation(say clicking the object) as shown below:
Settings.WebPackage("ReplayType") = 2 'Changes to mouse/device events mode
'Perform the Click Operation here
Settings.WebPackage("ReplayType") = 1 'Change back to Browser events mode
Additional information on ReplayType:
This setting allows us to change the way mouse operations should
happen on AUT. It can be either browser events or mouse event.
Browser event is similar to DOM events using the Browser methods,
wheras Mouse event simulate the actual user action either from
keyboard or mouse. For example if you click on a button when
ReplayType is Mouse, you will notice that mouse pointer moves to the
location of button and fires the event, but in case of browser event it
doesn’t.
Values of replaytype can be:
1 – Event
2 – Mouse
By default it is set to Event i.e 1
Is there a way to force a certain tab to render, even without currently being the active tab?
The reason i need to do this is because my extension uses chrome.tabs to open multiple tabs and switch between them, and whenever I switch to a tab which has not yet been active, the tab has not rendered yet, which causes a white flash to appear on the tab until the tab renders. The duration of the white flash is also proportional to how heavy the web page is to render.
What I've noticed though, is that if you switch to a tab which was active at least once in the past, the white flash does not appear (since it has been rendered at least once in the past)
I need to force a tab to be rendered before switching to it, so that the white flash doesn't appear.
I am writing on chrome version 38 (due to technical restrictions)
Thanks
No. You can't control it.
The white flashing is indeed an annoying known issue.
The only wild idea that can be implemented right now:
create a new minimized window with a blank url or about:blank,
move the tab you need into that window via chrome.tabs.move
now that the tab is active Chrome should render it even in a minimized window
then after a while move the tab back and activate it
I am building a Chrome extension that moves my pinned tabs to the active window: http://screencast.com/t/bRroTVda
It works great, with one exception:
When switching to another Chrome window by clicking on a tab in that window, I can't use chrome.tabs.move to move my pinned tabs to the newly activated window. runtime.lastError in the callback says
Tabs cannot be edited right now (user may be dragging a tab)
My problem now is: There seems to be no event that I can subscribe to that is raised when the user stops dragging.
What are my options?
You could retry moving tabs until the error goes away. To rate-limit the number of calls to 10 per second, use setTimeout(yourFunctionThatCallsChromeTabsMove, 100);.
I have developed a chrome extension that opens a popup when I click on the icon near the address bar. Everything works fine, however I want to add some functionality to it. So I thought I'd also add a context menu item so that the user can simply search for the highlighted word. I want the popup to showup when the user clicks on the item in the context menu(the default popup in the top right corner and not a new popup window or a new tab).
Can I have this functionality? If yes, how do I implement it?
You can't make the popup page show programmatically as if the user clicked it.
However, you can still have something display based on the background script / content menu click. There are 4 main options for your background script:
Open a new tab to the popup.html page
Programmatic injection of javascript to construct a popup-like dialog on the page
Content script message passing to do the same as above, using a running content-script.
Use the notifications API for a simple minimally stylized message to the user.
Options 2, 3, 4 will allow the user to stay on their tab without any navigation. The notifications API route is the simplest to use if you just want some quick notification to the user, and there are fewer security snags. 2 and 3 require more book-keeping, but you can make the dialog look like your popup.
There should be an API for it now (as in 2023)
https://developer.chrome.com/docs/extensions/reference/action/#method-openPopup
update: tried, but failed, there was a bug.
https://github.com/GoogleChrome/developer.chrome.com/issues/2602
hope they fix it soon.
I was looking for extensions that I have already been used, but forget its name. This extension opened up the result into Context Menu, without open new TAB or popup.
may be useful
https://developer.chrome.com/extensions/contextMenus