Selfclosing confirm box - javascript

I am building a web app and I am trying to do a function where there is a popup confirm box that shows after the user has been inactive for 10 minutes. When the popup opens the user can choose to continue being logged in or not. I need this box to close down after 1 minute if they have not answered the popup. How can I do this. Everything else works fine.
This is my code:
function checkTime () {
setTimeout (function () {
var dialog = confirm ("Do you want to continue being logged in?");
if (dialog == true) {
checkTime ();
} else {
window.location = 'LOGOUT URL';
}
}, 10000);
};

You cannot do that with the native confirm() as it stops JavaScript run on that page until the user has clicked on it.
So, You have to create a plugin for confirm-box or try someone else.
And they often look better, too. :)

you can't make the confirm close itself - as a thought path my bank has the same sort of thing on their on-line banking system - quite simply if you walk away and come back, it you've been longer than 10 minutes it doesn't matter what you chose it apologizes and logs you out anyway
the dialog requires human interaction it can not be overridden for security reasons. in order to do what you are trying to you would need to use a modal solution (this can be achieved with a small amount of js and some css if you don't want to use a third party solution)
I know it's not the answer you were looking for I'm sorry but hopefully it gives you some ideas for options

Related

How can I automatically login to this website?

I'm hoping to use javascript within tampermonkey to scrape https://www.intellizoom.com/ so I can be alerted immediately when any new jobs come in for me to accept (jobs can disappear within seconds if not acted on immediately). Unfortunately my login eventually expires, and the website redirects to https://www.intellizoom.com/login.
I therefore want to use a tampermonkey script matching the login URL to automatically log back in when needed.
I'm struggling to get this to work. First of, it seems you have to focus any input fields before they correctly accept any input from javascript. That works for adding the username and password, but just using focus() on the login button fails.
My test script successfully logs in if, before the message "click!" appears in the console, I physically click anywhere in the website page. (Hence the 5 second setTimeout to give me time to physically click - for test purposes). The login button then turns blue, and the .click(); javascript function then successfully submits the login details.
But, I can't find any way to simulate the physical click using javascript, all attempts to use click() on elements or on co-ordinates just does nothing (with no error messages in the console). Adding focus() before click() doesn't help either.
Can anyone figure out how to submit the login details for this website via javascript?
setTimeout(function(){
document.getElementById("email").focus();
document.getElementById("email").value = "username#Domain.co.uk";
document.getElementById("password").focus();
document.getElementById("password").value = "password";
},500);
setTimeout(function(){
console.log("click!");
document.getElementsByClassName("button large primary is-rounded")[0].click();
},3000);
UPDATE: The solution was kindly provided by Markfila - this code works:
setTimeout(function(){
document.getElementById("email").value = "username#Domain.co.uk";
document.getElementById("password").value = "password";
let event = new Event("blur");
document.getElementById("email").dispatchEvent(event);
document.getElementById("password").dispatchEvent(event);
document.getElementsByClassName("button large primary is-rounded")[0].click();
},100);
I've raised a follow up here question (since I'm not supposed to ask follow questions directly in here).
I dont think you need the focus, there using some kind of third party library for the ui and its not triggering the event that sets the value attribute. After a bit of messing around it looks like the ui library is setting the value on the blur event.
If you add this after setting the value then the login button should enable and the click code you have should work
let event = new Event("blur");
document.getElementById("email").dispatchEvent(event);
document.getElementById("password").dispatchEvent(event);
It seems like the window isn't focused because if you click the window then it will login and clicking the window focuses it.
You could try using var win = window.open("https://www.intellizoom.com/login") then win.focus() to focus the window.
So the whole code would look like this:
var win = window.open("https://www.intellizoom.com/login");
win.focus();
setTimeout(function(){
document.getElementById("email").focus();
document.getElementById("email").value = "username#Domain.co.uk";
document.getElementById("password").focus();
document.getElementById("password").value = "password";
},500);
setTimeout(function(){
console.log("click!");
document.getElementsByClassName("button large primary is-rounded")[0].click();
},3000);
I am not 100% sure about this but by reading your post this is what it made me think.
I hope this helps!

How to switch between different window.locations depending on answer to window confirm() method

I have a page that presents a test, where javascript sets a timeOut to abandon the page if the user waits too much (180") before giving an answer and pressing submit.
It has to be done for various reasons, and I cannot modify the committment.
After 180" on the page, so, currently the code goes back to a beginningpage.php where the user will have to re-enter its unique ID to continue the test.
This is the simple javascript code:
setTimeout(function () {
window.location.href= "beginningpage.php";
}, 180000);
Everything runs smooth. Now I need to modify the code to better manage the case. We assume that if the user has gone away, it will not be able to answer to an alert box. So let's say we push a confirm() after those 180".
How to write code so that - when the confirm() windows is displayed - if nobody clicks a confirm button (for let's say 30") the code automatically close the confirm window and leaves the page going back to beginningpage.php?
Otherwise, if the user is still at his desk and is simply wondering about the answer to give, he must be given the possibility to click "ok, I hurry up" to remain on the page for say another 60". That means that after the first displaying of the confirm window, the timeout must be set to a different deadline, not 180000 as before but 60000.
Maybe the idea of using different window.locations.href is not the right one.
How could be the code to accomplish everything I need as above explained?
Just don't use a confirm box... Show a div with two buttons after 180 seconds and then start counting your 30 seconds.
So let's say we push a confirm() after those 180".
How to write code so that - when the confirm() windows is displayed - if nobody clicks a confirm button (for let's say 30") the code automatically close the confirm window and leaves the page going back to beginningpage.php?
You can't. confirm, alert, and promopt are 1990s holdovers with stop-the-world behavior. (Mostly. Browsers are eroding that slowly over time.) That means none of your JavaScript code can run while the archaic window is showing.
Instead, switch to using modern alerting techniques (showing a "modal" div, etc.) so that your JavaScript code can use setTimeout to give up after the relevant amount of time.
You can never achieve this with alert or confirm because they block further js execution till the popup is active.
You need a custom modal with html element and a js function
All you have to do is call clearTimer for auto-page reload based on the response from custom confirm modal.
var x = document.getElementById("stopReloadBtn")
var reloadTimer
setTimeout(function() {
x.style.display = "block"
reloadTimer = setTimeout(function() {
window.location.href = "beginningpage.php"
}, 5000)
}, 5000);
function stopReload(){
clearTimeout(reloadTimer)
x.style.display = "none"
}
<button id="stopReloadBtn" style="display: none" onclick="stopReload()">Still Here?</button>
I have reduced the timers to 5 sec in this code. A button will appear after 5 sec. After which you will have 5 sec to click this button else the page will reload.

Twitter login status check

Is there anyway to check twitter login status like FB.getLoginStatus(response)? I want to hide the pop-up window if the user already logged. Right now the user has to click the button and every time the window will pop up whether the user logged or not:
$('#twitter').change(function() {
if($(this).prop('checked')){
hello('twitter').login(function(e){
if(e.error){
$('#twitter').prop('checked',false);
}else{
var twitter_oauth_token = hello('twitter').getAuthResponse().oauth_token;
var twitter_secret = hello('twitter').getAuthResponse().oauth_token_secret;
}
});
}else {
}
});
Looking for the same. I remember doing it years ago with testing image cache, but I am pretty sure that hack is long gone by now. I will have to check some old code. If only I could think of the project I used it on.

If confirm message idle force to close browser in javascript

i have a confirm message which is showing when page is idle for some time in javascript . The confirm pop up came once the session time out. At that time if we give any response to the confirmation pop up it will do the corresponding process. My requirement is if we are not giving any response means i need to force the browser to close.. can anybody help me on this?
window.setTimeout('checkIfContinue()', 5*1000);
var sFlag=0;
function checkIfContinue()
{
window.setTimeout('idleTime()', 5*1000);
if(sFlag == 0)
{
sFlag = 1;
if(confirm("Your Session Expired!. Do you wish to continue?"))
{
window.setTimeout('checkIfContinue()', 5*1000); //start the timer again
sFlag = 0;
}
else
{
// logout
}
}
}
This is the query which i used to call the pop up after session time out. I need to close the browser when no responses given to the responses..
I believe that in this case you'll need to use some other than system dialog.
E.g. showing a DIV with the question and buttons, so you'll be able to close the window even with opened "dialog".
In jquery, there are plugins for this kind of dialogs like Jquery UI, or select on of theese:
http://choosedaily.com/1178/15-jquery-popup-modal-dialog-plugins-tutorials/
Also remember, that the browser window can be closed by script only when it was opened by the script. Otherwise the function window.Close(); will ask the user about closing.

How to block users from closing a window in Javascript?

Is it possible to block users from closing the window using the exit button [X]? I am actually providing a close button in the page for the users to close the window.Basically what I'm trying to do is to force the users to fill the form and submit it. I don't want them to close the window till they have submitted it.
I really appreciate your comments, I'm not thinking of hosting on any commercial website. Its an internal thing, we are actually getting all the staff to participate in this survey we have designed....
I know its not the right way but I was wondering if there was a solution to the problem we have got here...
Take a look at onBeforeUnload.
It wont force someone to stay but it will prompt them asking them whether they really want to leave, which is probably the best cross browser solution you can manage. (Similar to this site if you attempt to leave mid-answer.)
<script language="JavaScript">
window.onbeforeunload = confirmExit;
function confirmExit() {
return "You have attempted to leave this page. Are you sure?";
}
</script>
Edit: Most browsers no longer allow a custom message for onbeforeunload.
See this bug report from the 18th of February, 2016.
onbeforeunload dialogs are used for two things on the Modern Web:
Preventing users from inadvertently losing data.
Scamming users.
In an attempt to restrict their use for the latter while not stopping the former, we are going to not display the string provided by the webpage. Instead, we are going to use a generic string.
Firefox already does this[...]
If you don't want to display popup for all event you can add conditions like
window.onbeforeunload = confirmExit;
function confirmExit() {
if (isAnyTaskInProgress) {
return "Some task is in progress. Are you sure, you want to close?";
}
}
This works fine for me
What will you do when a user hits ALT + F4 or closes it from Task Manager
Why don't you keep track if they did not complete it in a cookie or the DB and when they visit next time just bring the same screen back...:BTW..you haven't finished filling this form out..."
Of course if you were around before the dotcom bust you would remember porn storms, where if you closed 1 window 15 others would open..so yes there is code that will detect a window closing but if you hit ALT + F4 twice it will close the child and the parent (if it was a popup)
This will pop a dialog asking the user if he really wants to close or stay, with a message.
var message = "You have not filled out the form.";
window.onbeforeunload = function(event) {
var e = e || window.event;
if (e) {
e.returnValue = message;
}
return message;
};
You can then unset it before the form gets submitted or something else with
window.onbeforeunload = null;
Keep in mind that this is extremely annoying. If you are trying to force your users to fill out a form that they don't want to fill out, then you will fail: they will find a way to close the window and never come back to your mean website.
How about that?
function internalHandler(e) {
e.preventDefault(); // required in some browsers
e.returnValue = ""; // required in some browsers
return "Custom message to show to the user"; // only works in old browsers
}
if (window.addEventListener) {
window.addEventListener('beforeunload', internalHandler, true);
} else if (window.attachEvent) {
window.attachEvent('onbeforeunload', internalHandler);
}
If your sending out an internal survey that requires 100% participation from your company's employees, then a better route would be to just have the form keep track of the responders ID/Username/email etc. Every few days or so just send a nice little email reminder to those in your organization to complete the survey...you could probably even automate this.
It's poor practice to force the user to do something they don't necessarily want to do. You can't ever really prevent them from closing the browser.
You can achieve a similar effect, though, by making a div on your current web page to layer over top the rest of your controls so your form is the only thing accessible.
Well you can use the window.onclose event and return false in the event handler.
function closedWin() {
confirm("close ?");
return false; /* which will not allow to close the window */
}
if(window.addEventListener) {
window.addEventListener("close", closedWin, false);
}
window.onclose = closedWin;
Code was taken from this site.
In the other hand, if they force the closing (by using task manager or something in those lines) you cannot do anything about it.

Categories