Chrome extension breaks when tab is changed - javascript

I'm working on a chrome extension that simulates a keypress to submit a form. This works great as long as I stay on the page and do not switch tabs. When I switch tab the form doesn't get fill out the same.
What's strange is that it's the replaceCommentHtmlAndSetSender function that doesn't seem to propagate information properly on after one comment has been submitted and keypress event is fired. If I click back in to the page then the extension works.
I've tried viewing all events in the console using monitorEvents(window) and they seem to be the same. I've also tried adding a window.focus() event in replaceCommentHtmlAndSetSender without luck.
What events / differences could leaving the tab lead to this behavior?
var replaceCommentHtmlAndSetSender = function(reply_link, resp, id) {
$(document.activeElement).sendkeys("txt")
setTimeout(function() {
var str = '<span data-text="true" data-comment-sender="true">txt >/span>'
$(document.activeElement).replaceWith(str)
})
submitComment()
}
var submitComment = function() {
var foundElementScript = document.createElement("script")
foundElementScript.textContent = "foundElement()"
document.head.appendChild(foundElementScript)
foundElementScript.parentNode.removeChild(foundElementScript)
}
// this script is injected into the page
function foundElement(){
found=document.querySelectorAll('[data-comment-sender="true"]')[0];
found.setAttribute("data-comment-sender", "false");
submitComment(found);
}
}
function submitComment(element) {
var event;
if(document.createEvent) {
event = document.createEvent("HTMLEvents");
event.initEvent('keydown', true, true);
} else {
event = document.createEventObject();
event.eventType = 'keydown';
}
event.eventName = 'keydown';
event.keyCode = 13;
event.which = 13;
if(document.createEvent) {
element.dispatchEvent(event);
} else {
element.fireEvent("on" + event.eventType, event);
}
}

Related

Not able to remove the beforeunload event

We are using following code to show default pop up on refresh, tab close events
var myEvent = window.attachEvent || window.addEventListener;
var chkevent = window.attachEvent ? 'onbeforeunload' : 'beforeunload'; /// make IE7, IE8 compitable
myEvent(chkevent, function (e) { // For >=IE7, Chrome, Firefox
if ($('#timeExpiredtxt').hasClass('hide') && $("#submittedAnsResponseText").hasClass("hide")) {
var confirmationMessage = 'You are attempting an assessment. Are you sure you want to leave this assessment?';
(e || window.event).returnValue = confirmationMessage;
return confirmationMessage;
}
});
I want to remove and unbind this event at runtime. I tried the following code but no luck.
$(window).off("beforeunload");
$(window).off("onbeforeunload");
window.onbeforeunload = null;
$(window).unbind("beforeunload");
Had to change the event binding code.
function closeIt() {
if ($('#timeExpiredtxt').hasClass('hide') && $("#submittedAnsResponseText").hasClass("hide")) {
return "Any string value here forces a dialog box to \n" +
"appear before closing the window.";
}
}
window.onbeforeunload = closeIt;
and unbind used below code
window.onbeforeunload = null;

show javascript message to user using onbeforeunload event only for browser close event

As per the project requirement, on which I am working now, I need to show user a javascript alert only in the event of browser close using javascript. All other page events like url click, button click, F5 key press etc. are to be disregarded. I have tried with the following code but with no use.
var isPostBack = false;
$(function() {
// You would copy this for select and any other form elements I forgot about
$('input').live('click', function() { isPostBack = true; });
$('a').live('click', function() { isPostBack = true; });
document.onkeydown = function(e) { //attach to key down event to detect the F5 key
isPostBack = false;
if (!e) { //Firefox and Safari gets argument directly.
e = window.event;
}
var key = e.keyCode ? e.keyCode : e.which;
try {
if (key == 116) { //F5 Key detected
isPostBack = true;
}
}
catch (ex) { }
}
});
window.onbeforeunload = check;
function check() {
if (!isPostBack) {
// Do your unload code
isPostBack = false;
var strPath = window.location.pathname;
if (strPath.indexOf('CustomerPortal') >= 0) {
alert('Customer, you are leaving our page.');
}
else {
alert('User, you are leaving our page.');
}
return "Are you sure you want to exit this page?";
}
}
Please help to achieve my target requirement with your valuable comments and help.

how to detect if a link was clicked when window.onbeforeunload is triggered?

I have window.onbeforeunload triggering properly. It's displaying a confirmation box to ensure the user knows they are navigating (closing) the window and that any unsaved work will be erased.
I have a unique situation where I don't want this to trigger if a user navigates away from the page by clicking a link, but I can't figure out how to detect if a link has been clicked inside the function to halt the function. This is what I have for code:
window.onbeforeunload = function() {
var message = 'You are leaving the page.';
/* If this is Firefox */
if(/Firefox[\/\s](\d+)/.test(navigator.userAgent) && new Number(RegExp.$1) >= 4) {
if(confirm(message)) {
history.go();
}
else {
window.setTimeout(function() {
window.stop();
}, 1);
}
}
/* Everything else */
else {
return message;
}
}
You're looking for deferred event handling. I'll explain using jQuery, as it is less code:
window._link_was_clicked = false;
window.onbeforeunload = function(event) {
if (window._link_was_clicked) {
return; // abort beforeunload
}
// your event handling
};
jQuery(document).on('click', 'a', function(event) {
window._link_was_clicked = true;
});
a (very) poor man's implementation without jQuery's convenient delegation handling could look like:
document.addEventListener("click", function(event) {
if (this.nodeName.toLowerCase() === 'a') {
window._link_was_clicked = true;
}
}, true);
this allows all links on your page to leave without invoking the beforeunload handler. I'm sure you can figure out how to customize this, should you only want to allow this for a specific set of links (your question wasn't particularly clear on that).
var link_was_clicked = false;
document.addEventListener("click", function(e) {
if (e.target.nodeName.toLowerCase() === 'a') {
link_was_clicked = true;
}
}, true);
window.onbeforeunload = function() {
if(link_was_clicked) {
link_was_clicked = false;
return;
}
//other code here
}
You can differ between a link unload or a reload/user entering a different address unload s by using a timer. This way you know the beforeunload was triggered directly after the link click.
Example using jQuery:
$('a').on('click', function(){
window.last_clicked_time = new Date().getTime();
window.last_clicked = $(this);
});
$(window).bind('beforeunload', function() {
var time_now = new Date().getTime();
var link_clicked = window.last_clicked != undefined;
var within_click_offset = (time_now - window.last_clicked_time) < 100;
if (link_clicked && within_click_offset) {
return 'You clicked a link to '+window.last_clicked[0].href+'!';
} else {
return 'You are leaving or reloading the page!';
}
});
(tested in Chrome)

Trying to get Chrome to show a about to leave page using onbeforeunload

I am working on a Javascript that is suppose to do a click feature on an element as well as showing a pop-up asking if you want to really leave the site (close the tab). Now The code works fine on IE and Firefox. But Chrome while it does do the important thing in terms of doing the click(); It will not show a pop-up asking if I want to leave or not. I Don't know if its a feature I need to enable in the Chrome browser or something else. Here is the code I am using. Any help would be much appreciated.
var validNavigation = false;
function wireUpEvents() {
var dont_confirm_leave = 0;
var leave_message = document.getElementById("kioskform:broswerCloseSubmit");
function goodbye(e) {
if (!validNavigation) {
if (dont_confirm_leave!==1) {
if(!e) e = window.event;
//for IE
e.cancelBubble = true;
e.returnValue = leave_message.click();
//e.stopPropagation works in Firefox.
if (e.stopPropagation) {
e.stopPropagation();
e.preventDefault();
}
//return works for Chrome and Safari
return leave_message.click();
alert("Removing information.");
//add the code to delete the kiosk information here.
// this is what is to be done.
}
}
}
window.onbeforeunload=goodbye;
// Attach the event keypress to exclude the F5 refresh
jQuery('document').bind('keypress', function(e) {
if (e.keyCode == 116){
validNavigation = true;
}
});
// Attach the event click for all links in the page
jQuery("a").bind("click", function() {
validNavigation = true;
});
// Attach the event submit for all forms in the page
jQuery("form").bind("submit", function() {
validNavigation = true;
});
// Attach the event click for all inputs in the page
jQuery("input[type=submit]").bind("click", function() {
validNavigation = true;
});
}
// Wire up the events as soon as the DOM tree is ready
jQuery(document).ready(function() {
wireUpEvents();
});
You have to return a string in the onbeforeunload function to show the message to the user, see also Setting onbeforeunload on body element in Chrome and IE using jQuery

Remove onbeforeunload from JS execution in main window from frame

I tried to disable onbeforeunload event from frame script with this command:
window.parent.onbeforeunload = null;
but received this dialog:
I tried to debug and onbeforeunload becomes null. But how I can do so this dialog not shown?
For additional information, I need to trigger this event with JS. At start of the page I set:
window.parent.onbeforeunload = confirm;
where confirm is my own function. But in some places of code I need to disable this event and after that enable with the same command.
This could be happening because null is basically an object in Javascript. Here is how I had written it:
var confirmCloseFn = function(evt) {
if (!captureClose) return;
var message = getLogoffMsg();
evt = (evt) ? evt : window.event;
if(message) {
if (evt) evt.returnValue = message;
return message;
}
else {
if (evt) evt.returnValue = null;
return null;
}
};

Categories