I have a simple facebook validation i would like to implement to my jQuery button.
When the user clicks the button it should check if logged in, if TRUE then change the text.
I found this article which talks about returning a true/false state but when i tried to implement it in my code it didnt work.
Any suggestion where im going wrong please, thank you.
function fb_loginCheck(){
FB.getLoginStatus(function(response, e) {
if (response.status === 'connected') {
var uid = response.authResponse.userID;
var accessToken = response.authResponse.accessToken;
e.returnValue = true;
} else if (response.status === 'not_authorized') {
// the user is logged in to Facebook, but has not authenticated your app
fb_oAuth();
e.returnValue = false;
} else {
// the user isn't logged in to Facebook.
fb_oAuth();
e.returnValue = false;
}
}, true);
}
$('.myBttn').click(function(){
var io = return fb_loginCheck();
if (io){
$this = $(this).text();
if($this == 'yes')
$(this).text('no');
else
$(this).text('yes');
}
return false;
});
got it to work:
similar to potench answer but removing the e.returnValue did it
function fb_loginCheck(callBack){
FB.getLoginStatus(function(response) {
if (response.status === 'connected') {
var uid = response.authResponse.userID;
var accessToken = response.authResponse.accessToken;
callBack(true);
} else if (response.status === 'not_authorized') {
fb_oAuth();
callBack(false);
} else {
fb_oAuth();
callBack(false);
}
}, true);
}
Something like this might work. I've moved the methods around so they get fired when a response is returned from the FB.getLoginStatus method.
I'm passing in a callBack method which fires when the response from FB.getLoginStatus returns a result. Also note that I've had to re-scope the $(this) variable.
function fb_loginCheck(callBack){
FB.getLoginStatus(function(response, e) {
if (response.status === 'connected') {
var uid = response.authResponse.userID;
var accessToken = response.authResponse.accessToken;
e.returnValue = true;
callBack(true);
} else if (response.status === 'not_authorized') {
// the user is logged in to Facebook, but has not authenticated your app
fb_oAuth();
e.returnValue = false;
callBack(false);
} else {
// the user isn't logged in to Facebook.
fb_oAuth();
e.returnValue = false;
callBack(false);
}
}, true);
}
$('.myBttn').click(function(){
var targ = $(this);
fb_loginCheck(function (io) {
targ.text( (io) ? "yes" : "no" );
});
return false;
});
Related
I have a navigator.permissions.query that is not working
function handlePermission() {
navigator.permissions.query({name:'geolocation'}).then(function(result) {
if (result.state == 'granted') {
report(result.state);
geoBtn.style.display = 'none';
} else if (result.state == 'prompt') {
report(result.state);
geoBtn.style.display = 'none';
navigator.geolocation.getCurrentPosition(revealPosition,positionDenied,geoSettings);
} else if (result.state == 'denied') {
report(result.state);
geoBtn.style.display = 'inline';
}
result.onchange = function() {
report(result.state);
}
});
}
function report(state) {
document.getElementById("t").innerHTML = ('Permission ' + state);
}
handlePermission();
I have a live example here please check out my code and tell me where I am going wrong
You should read the example again and the docs for the functions you're using. The error messages in you live example are pretty clear.
navigator.geolocation.getCurrentPosition(revealPosition,positionDenied,geoSettings);
You have to hand the correct/valid parameters to this function. revealPosition is a function which is run, when the user granted the geolocation, positionDenied is a function which is run, when the user denies the access to the geolocation and geoSettings is an object which includes the setting for the call to .getCurrentPosition.
So once you create the missing parameters instead of handing undefined references to the call the function will run just fine.
I tried the old Facebook API method, it deprecated and won't work anymore. Right now I'm used /me/likes/{page-id} to retrieve whether my facebook account likes this page or not. I need to detect users are liking all the fan pages before specific action is taken.
FB.api("me/likes/184522495376661", function(response) {
if ( response.data.length === 1 ) {
FB.api("me/likes/1125511580839629", function(response) {
if ( response.data.length === 1 ) {
FB.api("me/likes/1603408753260758", function(response) {
if ( response.data.length === 1 ) {
// Specific action takes place here
} else {
alert("You haven't like third page!");
console.log(JSON.stringify(response));
}
});
} else {
alert("You haven't like second page!");
console.log(JSON.stringify(response));
}
});
} else {
alert("You haven't like our first page!");
console.log(JSON.stringify(response));
}
});
Each user required Facebook Login before detect the page likes.
FB.login(function (response) {
if (response.status === "connected") {
var uID = response.authResponse.userID;
fb_id = uID;
}else if (response.status === "not_authorized") {
}
},{ scope: 'email,user_likes,public_profile' });
The above codes work for my personal facebook account. But when it comes to other facebook id, the me/likes/{page-id} returns null, but that particular Facebook account already like the page, the API doesn't detect.
Now I have this code of redirect in function of the referer:
document.addEventListener('DOMContentLoaded', function() {
console.log(isMobile);
var referrer = document.referrer;
if(referrer.indexOf('site1.com') !== -1 || referrer.indexOf('site2.com') !== -1) {
if(isMobile.phone) {
window.location = "http://www.landingphone.com";
console.log('Is phone');
} else if(isMobile.tablet) {
window.location = "http://www.landingtablet.com";
console.log('Is tablet');
} else {
window.location = "http://www.landingdesktop.com";
console.log('Is desktop');
}
} else {
window.location = "http://www.anotherlanding.com";
}
});
Its ok this code for redirect in function of the referer site1 and site2.com, but if I need redirect also another referer (for example site3.com) to another landing (for example www.landingphone2.com, landingtablet2.com and landingdesktop2.com). What I need add in the code? what i need modify?
Thank you very much.
You can try something like this, adding another else if statement as referred to in the comment to your answer.
document.addEventListener('DOMContentLoaded', function() {
console.log(isMobile);
var referrer = document.referrer;
if (referrer.indexOf('site1.com') !== -1 || referrer.indexOf('site2.com') !== -1) {
if (isMobile.phone) {
window.location = "http://www.landingphone.com";
console.log('Is phone');
} else if (isMobile.tablet) {
window.location = "http://www.landingtablet.com";
console.log('Is tablet');
} else {
window.location = "http://www.landingdesktop.com";
console.log('Is desktop');
}
} else if (referrer.indexOf('site3.com') !== -1) {
// Do your other redirects here
} else {
window.location = "http://www.anotherlanding.com";
}
});
like the question asks, I'm trying to communicate with a serial device through a chrome app, via a webpage. The objective is to turn on a switch with a button on a webpage, and make sure the switch is in fact on (serial response).
So far I have been able to turn on the switch fine, however I need to validate that it is in fact enabled.
My chrome app code:
chrome.runtime.onMessageExternal.addListener(
function(request, sender, sendResponse) {
if (request.request == 'info') {
sendResponse(DEVICE_INFO);
} else if (request.request == 'turn_off') {
device_array.forEach(function(device){
if (device.id == request.device_id){
device.send('f');
}
});
//INSTEAD OF "OK" I NEED IT TO ASK THE DEVICE TO CONFIRM OFF/ON STATUS
sendResponse('OK');
} else if (request.request == 'turn_on') {
device_array.forEach(function(device){
if (device.id == request.device_id){
device.send('n');
}
});
sendResponse('OK');
}
return true;
});
If I send a "status" query to the device, it's going to take a few milliseconds for it to respond "off" or "on" to the serial buffer. Any ideas on how to go about this? Thanks in advance.
Ultimately I got it to work by requesting the device status within the listener block of code. Below are the modifications, crude but it gets the job done. DEVICE_STATUS is being updated by an onReceive listener on the serial connection.
chrome.runtime.onMessageExternal.addListener(
function(request, sender, sendResponse) {
if (request.request == 'info') {
sendResponse(DEVICE_INFO);
} else if (request.request == 'turn_off') {
device_array.forEach(function(device){
if (device.id == request.device_id){
device.send('f');
}
});
var time_loop = 0;
connection.send('s'); // s is for STATUS
var timer = setInterval(device_is_off, 200);
function device_is_off(){
if (time_loop > 5){ //Serial Communication Timeout at 1sec
sendResponse('ERROR ' + DEVICE_STATUS);
clearInterval(timer);
return;
}
if (DEVICE_STATUS == 0){
sendResponse('OK');
clearInterval(timer);
return
}
else time_loop++;
}
} else if (request.request == 'turn_on') {
device_array.forEach(function(device){
if (device.id == request.device_id){
device.send('n');
}
});
var time_loop = 0;
connection.send('s'); // s is for STATUS
var timer = setInterval(device_is_on, 200);
function device_is_on(){
if (time_loop > 5){
sendResponse('ERROR ' + DEVICE_STATUS);
clearInterval(timer);
return;
}
if (DEVICE_STATUS == 1){
sendResponse('OK');
clearInterval(timer);
return
}
else time_loop++;
}
}
return true;
});
I'm working on a chat app with Meteor and I don't want users to be able to copy/paste things into the form for obvious spam reasons. Is this possible? Here is the code I use to run the chat app:
Javascript:
// render all of our messages in the ui
Template.chatBox.helpers({
"messages": function() {
return chatCollection.find();
}
});
// get the value for handlerbar helper user
Template.chatMessage.helpers({
"user": function() {
if(this.userId == 'me') {
return this.userId;
} else if(this.userId) {
getUsername(this.userId);
return Session.get('user-' + this.userId);
} else {
return 'anonymous-' + this.subscriptionId;
}
}
});
// when Send Chat clicked at the message to the collection
Template.chatBox.events({
"click #send": function() {
if (Meteor.user() == null) {
alert("You must login to post");
return;
}
$('#messages').animate({"scrollTop": $('#messages')[0].scrollHeight}, "fast");
var message = $('#chat-message').val();
// check to see if the message has any characters in it
if (message.length < 1) {
alert("You must enter a message to post.");
return;
}
chatCollection.insert({
userId: 'me',
message: message
});
$('#chat-message').val('');
//Validation
var bot =Check_bots();
if(bot==false)
{
//add the message to the stream
chatStream.emit('chat', message);
}
else
{
alert("Slow down! No need to post that fast.");
return false;
}
},
"keypress #chat-message": function(e) {
if (Meteor.user() == null) {
alert("You must login to post");
return;
}
if (e.which == 13) {
//Validation
var bot =Check_bots();
if(bot==false)
{
$('#messages').animate({"scrollTop": $('#messages')[0].scrollHeight}, "fast");
console.log("you pressed enter");
e.preventDefault();
//repeat function from #send click event here
var message = $('#chat-message').val();
// check to see if the message has any characters in it
if (message.length < 1) {
alert("You must enter a message to post.");
return;
}
chatCollection.insert({
userId: 'me',
message: message
});
$('#chat-message').val('');
//add the message to the stream
chatStream.emit('chat', message);
}
else
{
alert("Slow down! No need to post that fast.");
return false;
}
}
}
});
chatStream.on('chat', function(message) {
chatCollection.insert({
userId: this.userId,
subscriptionId: this.subscriptionId,
message: message
});
});
var lastintime=0;
var defference=0;
var msg_count=0;
function Check_bots()
{
var seconds = new Date().getTime() / 1000;
seconds=parseInt(seconds);
if(lastintime < seconds)
{
defference = seconds -lastintime;
lastintime=seconds;
if(defference<=5 && msg_count>=3)
{
return true;
}
else
{
return false;
}
}
}
I have no idea where to even start with this. How do you prevent copy/pasting?
It's not a good idea. Internet Explorer has a onpaste event, and there's been a convoluted implementation on Stack Overflow, but in general it's messy, crosses lines that generally shouldn't be crossed in web design, impossible to pull off completely across all browsers, and can be circumvented without too much trouble.
It's a far better idea to put character-rate limits and detect red flags for spam, like high link density and repetition.