How to use onUrlChanged? - javascript

I'm trying to log in into gmail account using javascript, and I have a problem:
after I insert my email and press 'next', the page redirect me to new url asking for my password. my question is, How can I monitor the current url and know when it changes? Im trying to use page.onUrlChanged but it doesn't work
test.open(url, function(status) {
test.page.evaluate(function (email, password) {
document.getElementById('Email').value = email;
document.getElementById('next').click();
test.page.onUrlChanged = function(targetUrl) {
console.log(targetUrl);
}
}, email, password);});

you can use phatomjs for log URL change..below is simple code for that
var webPage = require('webpage');
var page = webPage.create();
page.onUrlChanged = function(targetUrl) {
console.log('New URL: ' + targetUrl);
};
find more about phantomjs
http://phantomjs.org/api/webpage/handler/on-url-changed.html
or
You can use
window.onblur = function() { console.log('blur'); }

Related

Email RegEx Javascript with RPC

I have a few codes here of js. And I wanted to put a validation for email and prevents user from submitting a form when it is not valid. Please bear with me since I am new with this technology. Thank you for understanding :)
var session = require('web.session');
var db_name = $("input[name='partner_name']").val();
var contact = $("input[name='contact_name']").val();
var email = $("input[name='email_from']").val();
var url = '/page/website_crm.contactus_thanks';
$(function(){
$("#start_trial").click(function(){ // no idea how to put email validation here
session.rpc('/custom/createaccount', {
db_name : db_name,
contact_name: contact,
email_from: email
});
}).then(function () {
console.log("Creating user account")
});
// that won't proceed here
setTimeout(function (){
window.location = url;
}, 5000);
});
if you want to test if email is ok, lot of solution, this pattern does the job:
var email = $("input[name='email_from']").val();
var pattern = /^\w+#[a-zA-Z_]+?\.[a-zA-Z]{2,3}$/;
if(!pattern.test(email))
{
alert('e-mail address is not valid');
}​

Casperjs Google Login Not working

I having been working on some code to access my google finance portfolio, but the problem is I need to sign in with my google account. So i have made this:
var casper = require('casper').create();
casper.start('https://accounts.google.com/Login?hl=EN', function() {
this.evaluate(function(username, password) {
this.echo(this.getTitle());
document.querySelector('input#Email').value = username;
document.querySelector('#next').click();
document.querySelector('input#Passwd').value = password;
document.querySelector('#signIn').click();
}, 'GOOGLE EMAIL', 'PASSWORD');
});
casper.then(function() {
this.echo(this.getHTML()); // => 'The text included in the <h1 id=foobar>'
casper.thenOpen('https://www.google.com/finance/portfolio?action=view&pid=1&ei=pBrbVoDhM4iFjAGB-bKIAg', function() {
this.echo(this.getHTML());
this.echo(this.getTitle());
});
});
casper.run();
Which does not log me in!
In my original code I was selecting the wrong input boxes from the google page, instead it should look like this:
var casper = require('casper').create();
casper.start("https://accounts.google.com/Login?hl=EN", function() {
console.log("page loaded...");
//console.log(this.getHTML());
//document.querySelector('#Email').value = "kpfromer#gmail.com";
//document.querySelector('#next').click();
this.fillSelectors('form#gaia_loginform', {
'input[name="Email"]': 'EMAIL',
}); //Fills the email box with email
this.click("#next"); //Fills the email box with email
this.wait(500, function() { //Wait for next page to load
console.log("Inside WAIT...");
this.waitForSelector("#Passwd", //Wait for password box
function success() {
console.log("SUCCESS...");
this.fillSelectors('form#gaia_loginform', {
'input[name="Passwd"]': 'PASSWORD',
}); //Fill password box with PASSWORD
this.click("#signIn"); //Click sign in button
this.wait(500, function() {}); //Wait for it fully sigin
},
function fail() {
console.log("FAIL...");
}
);
});
});
casper.run();
The reason why there are waits is that it takes a little bit for the page to fully load, and swap to other pages.

How do I set a cookie in iOS Phonegap?

I'm using the reddit API and can successfully login and receive a cookie value in return. The code I use is as follows
loginPost = $.ajax({
type: 'POST',
url: 'http://www.reddit.com/api/login/' + username + '?user=' + username + '&passwd=' + password + '&api_type=json',
xhrFields: {
withCredentials: true
},
dataType: "json",
success: function() {
console.log("Define response variables");
var header = loginPost.getAllResponseHeaders();
var responseText = loginPost.responseText;
var match = header.match(/(Set-Cookie|set-cookie): reddit_session=(.+?);/);
if (match) {
reddit_session = match[2];
window.localStorage.setItem("reddit_session", reddit_session);
window.localStorage.setItem("reddit_username", username);
window.localStorage.setItem("reddit_password", password);
console.log("Logged in!");
//alert(responseText);
$('.loginWrapper').slideUp('fast', function() {
$('#feedWrapper').css("top", "44px");
$('#feedWrapper').css("bottom", "0");
// Animation complete.
});
}
else {
reddit_session = null;
window.localStorage.setItem("reddit_session", null);
navigator.notification.alert('Your username or password is incorrect. Please try again.');
console.log("Login Failed");
}
},
});
I am storing the cookie using
var header = loginPost.getAllResponseHeaders();
var match = header.match(/(Set-Cookie|set-cookie): reddit_session=(.+?);/);
if(match){
reddit_session = match[2];
window.localStorage.setItem("reddit_session", reddit_session);
But the cookie isn't sent with a simple request to http://www.reddit.com/api/me.json. All that is returned is an empty JSON response like this {}. If I browse to that link in my browser (and am, of course, logged into reddit) it returns a JSON string with all the user data.
Any idea how I can store the cookie so that it is usable in a UIWebView?
Any idea how I can store the cookie so that it is usable in a UIWebView?
It appears that iOS cookies have been problematic since phonegap 2.5; please see [Handling cookies in PhoneGap/Cordova
I can't see any more current (phonegap 3.3) info [http://docs.phonegap.com/en/3.3.0/_index.html]

Issue in the faceebook auth dialog box

I have integrated Facebook login in my website using javascript SDK.. Everything is working fine except for the auth dialog box, which is showing only once for a user.Next time when the user logs it redirects without showing the auth dialog box.I want to show the auth dialog box each time the user logs.
This is the code I am using
window.fbAsyncInit = function() {
FB.init({
appId : 'xxxxxxxx',
status : true,
cookie : true,
xfbml : true,
oauth : true
});
if (window!=window.top) {
FB.Canvas.setAutoResize();
};
FB.getLoginStatus(function(response) {
if (response.authResponse) {
window.FBlogin = function(){
FB.login(function(response) {
if (response.authResponse) {
FB.api('/me', function(response) {
console.log('Good to see you, ' + response.name + '.');
var query = FB.Data.query('select name,username, email, hometown_location, sex, pic_square from user where uid={0}', response.id);
query.wait(function(rows) {
//document.getElementById('name').innerHTML = '<img src="' + rows[0].pic_square + '" alt="" />';
var name=rows[0].name;
var email=rows[0].email;
var uid=response.id;
var username=rows[0].username;
var pic=rows[0].pic_square;
});
});
}
else {
alert("error");
}
}, {scope: 'email'});
};
}
else {
var authbox = document.getElementById('FBauth');
authbox.innerHTML="";
var a = document.createElement('a');
a.setAttribute("href","javascript:void();");
a.setAttribute("onclick","FBlogin();");
authbox.appendChild(a);
window.FBlogin = function(){
FB.login(function(response) {
if (response.authResponse) {
FB.api('/me', function(response) {
console.log('Good to see you, ' + response.name + '.');
var query = FB.Data.query('select name,username, email, hometown_location, sex, pic_square from user where uid={0}', response.id);
query.wait(function(rows) {
var name=rows[0].name;
var email=rows[0].email;
var uid=response.id;
var username=rows[0].username;
var pic=rows[0].pic_square;
});
});
}
}, {scope: 'email'});
};
}
});
FB.Event.subscribe('auth.login', function () {
FB.api('/me', function(response) {
console.log('Good to see you, ' + response.name + '.');
var query = FB.Data.query('select name,username,email, hometown_location, sex, pic_square from user where uid={0}', response.id);
query.wait(function(rows) {
var name=rows[0].name;
var uid=response.id;
var email=rows[0].email;
var username=rows[0].username;
var pic=rows[0].pic_square;
var page='';
$('<input />').attr({'type':'hidden', 'id':'fbname','value':email}).appendTo('#fbaccess');
});
})
});
FB.Event.subscribe('auth.logout', function(response) {
});
};
Everything is working fine except for the auth dialog box, which is showing only once for a user.Next time when the user logs it redirects without showing the auth dialog box.
If the user is already connected to your app and has given all of the requested permissions – then this is the intended behavior.
Calling FB.login again in this situation will open the popup, and close it again immediately.
I want to show the auth dialog box each time the user logs.
I can’t see the benefit of an app behaving in such a way.
Anyways, there is a parameter to explicitly request that the user re-authenticate: auth_type=reauthenticate. This will force the user to re-enter their password. See https://developers.facebook.com/docs/authentication/reauthentication/#client-side for details.
But I’m not sure that this is what you actually want.
Another way could be to delete permissions via your app, and then have them requested again via the scope parameter. That should bring up the Auth dialog again. For details on that, see https://developers.facebook.com/docs/reference/api/user/#permissions

facebook javascript sdk - how to check if user likes page without fb.login pop up

I have a javascript function that uses fb.login to retrieve the users info and checks to see if the user likes my fb page. fb.login causes a pop up where the user must click a login button. If the user likes my page i need to first create a cookie then redirect them to the main app:
function checkUser() {
var page_id = "my id goes here"; //
FB.login(function (response) {
if (response.authResponse) {
console.log('Welcome! Fetching your information.... ');
FB.api('/me', function (response) {
var fql_query = "SELECT uid FROM page_fan WHERE page_id = " + page_id + "and uid=" + response.id;
var the_query = FB.Data.query(fql_query);
the_query.wait(function (rows) {
if (rows.length == 1 && rows[0].uid == response.id) {
//$("#container_like").show();/
//set cookie
document.cookie = "fbId=" + response.id;
window.location = "/kisses.aspx";
} else {
$("#likepageholder").show();
//$("#container_notlike").show();
//window.location = "/kisses.aspx";
//and here you could get the content for a non liker in ajax...
}
});
});
} else {
console.log('User cancelled login or did not fully authorize.');
}
});
}
If there a way around using fb.login to do this?
Nope. Unfortunately, you need the user to log in (and grant your app basic permissions [based on the appId you use in FB.init) in order to query the user's likes.
That being said, it looks like your code should work.

Categories

Resources