Focusing browser tab when a value within the page reaches a specific number - javascript

I have a web page that refreshes constantly, where a span have a number that is changing (decreasing).
Is there a way using JS for example to automatically focus the browser tab containing the web page when that number reaches less than 10 so that the supervisor takes action before hitting 0 without knowing?
Thanks.

This is not possible.
There's two possible solutions.
Browser extension. This will give you a lot more leeway and control.
And this has become a lot more popular is to change the favicon on the page to another one that looks like an alert. And changing the title too. Playing a sound is another cue to the user that something has happened.
Hope this helps!

Depending on the browser and the user’s settings, alert might take the user to the tab that opened the alert. In other browsers alert might only change the tab icon to stand out.
Firefox gives the user the option to enable this behavior with a “Allow dialogs from example.com to take you to their tab” checkbox, unchecked by default:
Chrome doesn’t give the user any such option, but does mark the tab with an orange indicator:
You can test your browser’s behavior by running this snippet and then switching to another tab:
setTimeout(function() {
alert('this is a delayed alert');
}, 3*1000);

Related

Javascript bring a tab to the front

I am writing a Javascript which runs in the browser using Tampermonkey.
The logic in the script is as follows.
1. Do some checks.
2. If a certain condition is met, then trigger an alert box in the browser.
Currently the alert box is shown in the browser tab, but amidst multiple tabs, the user does not notice the alert box till they navigate to the tab.
I am trying to figure out one or both of the following.
1. Bring the tab to the front i.e. focus on that tab.
2. Do something more aggressive than the alert box to catch user's attention. I thought of things such as playing sounds etc but it requires on user's speakers being on etc.
Any ideas on how to make this work would be greatly appreciated.
Looks like this cannot be done and the easiest option would be to write some data to the browser's local storage and then open a new window, fetch the data and display it.

element.focus() with open developer console not working

I want to programmatically focus on an input:
document.getElementById('widgetu25071_input').focus()
This does not work when the developer console is open. Is there some way to do it without closing the console first?
You can recreate the issue on google.com:
Open console and execute:
document.getElementById('lst-ib').focus();
result for me: does not focus
now try: open console and execute:
setTimeout(function() {
document.getElementById('lst-ib').focus();
}, 9000);
close console quickly. Focus works when the timeout is finished.
I forget about this every couple years and it drives me crazy until I remember again. So I'm adding an answer for myself and anyone else...
JavaScript basically thinks you're in a different application and can't steal focus to focus wherever you want to focus. ; )
You'll find this behavior if your cursor is in the console or in the URL bar of the browser.
Mouse hover events will still fire but you won't be able to gain focus anywhere on the page unless focus is already on the actual page.
So for example, if you have a little popup that contains a text field and it shows on mouse hover of some other element, you'll get the pop-up on mouse hover but you won't get that text field to focus if your cursor is in the console or in the URL bar.
As a crazy guess I'd say without this, a bad programmer could keep you from navigating away from the page (or doing anything else) by stealing focus back to the page every time you click in the URL bar or the console.
I hope this helps someone even if It's five years later.

How would you stop a page from refreshing automatically?

I have a problem when I am trying to check the source of an interesting page which keeps refreshing automatically every 3-5 seconds (presumably due to some js script) which resets my Inspect Element Inspector window every time the page is refreshed.
Is there any other way other to stop that page from refreshing or perhaps the Inspector window from resetting itself other than turning on NoScript to stop the page from refreshing automatically?
Usually I just open DevTools, switch to the appropriate panel if necessary, and hit pause.
Opening DevTools: Via menus, or by press F12, Ctrl+Shift+I, or Cmd+Shift+I depending on browser and OS.
Switching panels: Pick the panel from the tabs at the top of DevTools. It'll be called "Debugger" (Firefox, IE) or "Sources" (Chrome) or similar.
Pausing: In the Debugger/Sources panel, click the pause button (usually looks like the pause button on a television remote control, ||) or press the keyboard equivalent. Keyboard equivalents are
Firefox & Chrome: F8
IE: Ctrl+Shift+B
(Updated 2020-03-30)
In Firefox 74 this option is in Options -> Privacy & Security -> Permissions
(Original reply)
Firefox has the option to prevent refresh natively, the option is in Advanced->General->Warn me when websites try to redirect or reload the page
The most popular solution for this problem is to trap the beforeunload event. The browser will ask the user for confirmation to leave the page. The code, in its simplest form, looks like this:
window.onbeforeunload = function() { return true }
You can enter this code in console. Alternately, you can simply paste the following URL in the browser address bar (console not required). You can even bookmark it.
javascript:window.onbeforeunload = function() { return true }
Be advised that modern browsers might chop off the javascript: part when you paste it inside the address bar; make sure you type it back.
To determine the cause of redirect in Firefox, try the following:
Open Web Developer Tools (CTRL + SHIFT + I), open "Toolbox Options" and check the "Enable persistent logs" option. This makes the logs persist across page loads (logs are cleared otherwise).
Now switch to "Network Monitor" tab.
Open the URL and let it refresh.
Inside the Network Monitor > Cause column you will find out why the page reloads.
The cause column is pretty ambiguous (Chrome does a much better job). However, if JavaScript was used to trigger page (re)load then it at least shows you the filename and line number of that script.
When the page is still loading, you can press the Esc key. While the page is still white, press it. When you stop the page from loading at this point, this usually stops all the auto loaded javascript. Any scripts that run on actions are usually not effected. Each page is different, try different timings.
When I use a site called NovelUpdates there is javascript that can make certain elements hidden, and when I press Esc on page load all the elements that would be hidden after page load are visible. Then when I click a button that would execute javascript that operates with no problems. NoScript isn't going to solve your issue I believe.
Another example of this are those websites with annoying boxes that pop out after 10 seconds that says you aren't a member and can't view any more of this site without logging in, like some news article websites.
What you could do is use the command exit(), which is the equivalent to die in php and simply stops the script.
If you don't know what's causing it and you don't want to look for the "bad boy", then you might as well stop the entire script at the very bottom of the page.

Detect if browser has more then 1 tab/window open

So i am developing a quiz web application. And i wanted to add a setting that the administrator of the quiz could set that would make it so the user could only have 1 window/tab open while the quiz is being taken.
The reason for this is to make it so they cant goto like google and google the answer while the quiz window/tab is open. Of course they could always open a different browser and do it that way, but still thought it would be a nice feature to have for them to enable.
Now i dont know if this would fall under a security sandbox violation (and thus not be available at all) but since i only want to detect if another tab or window is open and not get actual information about the tab/window i am hoping that this is someway possible using javascript.
You can't, but a possible workaround would be to use the new HTML5 fullscreen API. You could use a setInterval function to regularly test that document.fullScreen == true to ensure that the user has not toggled off the full screen.
This only works in modern browsers, and it's trivial to work around if the user knows his way around the JS console, but it does seem to fit your requirements.
Note that all fullscreen API implementations are currently vendor-prefixed.
There seems to be viable alternative to the approach described below the line: using Page Visibility API, currently supported by all the modern browsers. This looks like far more reliable than listening for blur. There's one possible way to do it:
// on quiz start
document.addEventListener('visibilitychange', function() {
if (document.hidden) {
console.log('Y U hide?');
}
});
The problem is that visibilitychange event is fired only when page goes from visible to hidden (and vise versa). It still lets user open two browser instances - one with the quiz page, one with any favorite search engine, for example.
While you cannot detect the number of tabs open, you can try to check when the user goes away from the quiz page with this trick:
$(function(){
$(window).blur(function() {
console.log('I see what you did here!');
});
});
Sadly, it'll also give you plenty of false positives.
Can't, and shouldn't, be done.

disable onclick ads with a content-script in Google Chrome

There are some video streaming sites that pop up an ad anytime you click anywhere on the page. The problem is, you have to click on the page to press play! So I was thinking of making a UserScript that disables the script that does this. The only problem is, I already disable all the scripts on the site and when I do it still pops up. Is there a way that I can disable them ? I'm also using jQuery, so if I can do it through their interface, that would be great.
edit: Two perfect examples of such sites are daclips.in and gorrilavid.in
I have Adblocker Plus, and it seems like it is not recognizing "on Click" events as pop-ups, rather normal clicked links. And the logic is simple, no Adblocker will block you from clicking something intentionally and it (the link) opening in another window/tab.
The problem is the new window contains your clicked Url, while the original window/tab "Refreshes" (i.e. redirects) to another url.
Advertising companies seem to use this trick to bypass adblocking software.
Just ditch Chrome and use Firefox. Firefox already have built-in mouse-click popups. I think all addons like Adguard or Adblock can not disable mouse-click popups. If you use Firefox, these are the steps:
Type about:config in the browser's address bar and hit the enter key.
First time users need to confirm that they be careful on the next page.
Type or paste dom.popup_allowed_events into the search field.
The value of the preference highlights all events that are allowed to spawn popups.
Edit the value to remove some or all of the items here.
Why not just use a browser extension such as AdBlock?
https://chrome.google.com/webstore/detail/adblock/gighmmpiobklfepjocnamgkkbiglidom?hl=en
My go-to is right click and open in new tab. onClick events only happen with a left click. It's cumbersome but it still ends up being less work than closing the pop-up and whatever annoying prompts it may have.
I do not there's a practical solution for this.
Moreover, I think some of the answers here are missing the specific case in OP, where clicking anywhere on the page will cause the pop up to happen, not just clicking on links. According to this, neither right-clicking then choosing "open", nor noticing and blocking the target URL will help. I do not know of an add blocker that helps here either, because it's not trivial to meaningfully filter a click event that is taking place on the whole page object.
Only the solution provided by #Monkey would work, at the drawback of possibly breaking other things.

Categories