I am trying to use the Facebook Share in AngularJS. Below is my function that is called when the user clicks on the FB icon.
$scope.shareFB = function(){
// Get configuration ID from service
configuratorService.storeConfiguration($scope.modelCode, function(configID){
// Use saved configuration id to create share link
var base = $location.absUrl().replace($location.url(), '');
var byoUrl = base + "/" + $scope.modelCode + "/resume/" + configID;
console.log(byoUrl);
var fbpopup = window.open("https://www.facebook.com/sharer/sharer.php?u=" + byoUrl, "pop", "width=600, height=400, scrollbars=no");
});
}
This function works fine when I try to share a url like "https://www.google.com/"
the Facebook Popup then has the URL = "https://www.facebook.com/sharer/sharer.php?u=https://www.google.com/"
When I use the function above:
byoUrl = "http://localhost:8000/#/15K6/resume/9295316837"
and the resulting FB popup has URL = "https://www.facebook.com/15K6/resume/9295316837"
Why does the "/sharer/sharer.php?=http://localhost:8000/#/" get cut off?
You shouldn't even try to share a localhost URL, as Facebook will never be able to scrape it. That's very likely why your URL gets cut off. Facebook tries to resolve it and scrape it, but it will never find it, so it makes a best effort to redirect within itself. Example:
https://www.facebook.com/sharer/sharer.php?u=http://localhost:8000/#/coke
Try to put your share logic in the controller. Something along these lines.
// Share posts
$scope.fbShare = function(post){
FB.ui(
{
method: 'feed',
name: post.title,
link: 'http://www.cengkuru.com/'+post.slug,
picture: '',
caption: '',
description: $filter('limitTo')($scope.post.body, 150),
message: ''
});
}
<div id="fb-root"></div>
<script>
window.fbAsyncInit = function() {
FB.init({appId: 'YOUR_APP_ID', status: true, cookie: true,
xfbml: true});
};
(function() {
var e = document.createElement('script'); e.async = true;
e.src = document.location.protocol +
'//connect.facebook.net/en_US/all.js';
document.getElementById('fb-root').appendChild(e);
}());
</script>
Related
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'); }
I am trying to integrate bit.ly on my website in JS to short my url. All my url are too long, what will be the most straight foward way to use the bit.ly restful api for sharing button on a static website in HTML/javascript.
The result I want to get is when my user click share on my website the url is automatically shortened by bit.ly
here is the code I am currently using to share my pages dynamically on twitter:
<script type="text/javascript" charset="utf-8" src="http://bit.ly/javascript-api.js?version=latest&login=LOGINID&apiKey=APIKEY"></script>
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.3.2/jquery.js"></script>
<script>
function tweetCurrentPage()
{ window.open("https://twitter.com/share?url=" + escape(window.location.href) + "&text=" + document.title, '', 'menubar=no,toolbar=no,resizable=yes,scrollbars=yes,height=300,width=600'); return false; }
var TweetThisLink = {
shorten : function(e) {
e.preventDefault();
var url = this.href.substr(this.href.indexOf('http:',5))
BitlyClient.shorten(url, 'TweetThisLink.response');
},
response : function(data) {
var bitly_link = null;
for (var r in data.results) {
bitly_link = data.results[r]['shortUrl'];
break;
}
var tweet_text = "I am reading documentation of"
document.location = "http://twitter.com/share?url=" + encodeURIComponent(tweet_text + ' ' + bitly_link);
}
}
jQuery('.tweetlink').bind('click', TweetThisLink.shorten);
</script>
tweet this link
Not sure if this is on purposefully obfuscated for the sake of the question, but
In your script tag the src is:
"http://bit.ly/javascript-api.js?version=latest&login=LOGINID&apiKey=APIKEY".
LOGINID & apiKey are placeholders are in-place. You need to replace them with the appropriate keys you should receive from bitly.
if this is on purpose for the sake of the question please ignore this answer.
don't know why but my function "tweetCurrentPage()" for dynamic url won't work it's giving me a respond INVALID_URI from bit.ly, but if I hard code the href value like this twitter.com/share?url=+exemple.com"; it's working...
I am using this code to login to form. I get error.
var casper = require('casper').create();
var url = "www.example.com";
casper.start(url,function(){
this.echo(this.getTitle());
});
casper.waitForResource(url,function() {
this.fillSelectors("form[method='post']", {
'input[name="userName"]' : 'usr1',
'input[name="password"]': 'pswed1'
}, true);
});
casper.run();
http://prntscr.com/7jkia1
I pretty much copied examples I found on stack but still I get error... what to do to fix this?
It seems that casper is not able to do a selector for form[method='post'] ( using fillSelectors ), its not working for me either. But if you specify any other atribute like name, id, class, enctype, action, etc, it works.
var casper = require('casper').create();
var url = "https://careershub.bbc.co.uk/members/";
casper.start(url,function(){
this.echo(this.getTitle());
});
casper.waitForResource(url,function() {
this.fillSelectors('form#registration-form', {
'input[name="name_first"]' : 'firstname',
'input[name="name_last"]': 'lastname'
}, true);
this.captureSelector('test.png', 'form');
});
casper.run();
I have a Facebook application, a Facebook page and a website. When someone adds a comment to my website, I retrieve the comment text from the code below. What I want to do after that is to let my Facebook application post the same comment text to my Facebook page.
This is the JavaScript code I have so far on my website:
window.fbAsyncInit = function() {
FB.init({
appId: " . drupal_to_js($appid) . ",
status: true,
cookie: true,
xfbml: true,
channelUrl: " . drupal_to_js($channel_url) . "
});
FB.Event.subscribe('comment.create', function(response) {
var commentQuery = FB.Data.query('SELECT text FROM comment WHERE post_fbid=\'' + response.commentID + '\' AND object_id IN (SELECT comments_fbid FROM link_stat WHERE url=\'' + response.href + '\')');
FB.Data.waitOn([commentQuery], function () {
var commentRow = commentQuery.value[0];
var commentText = commentRow.text;
//TODO Post commentText to the Facebook page.
});
}); };
After an extensive search, here is the answer for those who are looking for it. Please read the comments inside the code, they will give you more information.
window.fbAsyncInit = function() {
FB.init({
appId: " . drupal_to_js($appid) . ",
status: true,
cookie: true,
xfbml: true,
channelUrl: " . drupal_to_js($channel_url) . "
});
FB.Event.subscribe('comment.create', function(response) { //trigger when comment is created
var commentQuery = FB.Data.query('SELECT fromid, text FROM comment WHERE post_fbid=\'' + response.commentID + '\' AND object_id IN (SELECT comments_fbid FROM link_stat WHERE url=\'' + response.href + '\')');
var userQuery = FB.Data.query('SELECT name FROM user WHERE uid in (select fromid from {0})', commentQuery);
FB.Data.waitOn([commentQuery, userQuery], function () {
var commentRow = commentQuery.value[0];
var userRow = userQuery.value[0];
var commentText = commentRow.text;
var commentUsername = userRow.name;
//Call this function to send an Ajax request to Facebook.
post_to_fb(commentText, commentUsername, response.href);
});
}); };
//This function will post to a Facebook page that has the ID $fb_page_id.
//The post format is like this, [NAME said:] POST e.g: [ObyYou said:] this is a test.
//Of course, you can change the format the way you want.
//You have to have an access key to have the permission to post on that page.
//Use the two sites at the bottom of this answer for help, (remember if you
//want to hard-code the access token, you have to create a permenant access token).
//Note that some variables and functions are PHP, since my JavaScript code is
//actually inside a PHP file.
function post_to_fb(commentText, commentUsername, commentLink) {
var strURL = 'https://graph.facebook.com/" . $fb_page_id . "/feed';
var params = 'link=' + commentLink + '&message=[' + commentUsername +'+said:]+' + commentText + '&access_token=" . $fb_page_access_token . "';
var xmlHttpReq;
xmlHttpReq = new XMLHttpRequest();
xmlHttpReq.open('POST', strURL, true);
xmlHttpReq.setRequestHeader('Content-type','application/x-www-form-urlencoded');
xmlHttpReq.send(params);
}
Create A (permanent) access_token:
http://www.testically.org/2011/09/27/5-steps-to-automatically-write-on-your-facebook-page-wall-using-the-graph-api-without-a-logged-in-user/
http://php-academy.blogspot.com/2011/04/how-to-post-from-facebook-app-to.html
I am working on a facebook app. I have the following code which works fine if I run it like this
<div id="fb-root"></div>
<script>
window.fbAsyncInit = function() {
FB.init({
appId : '<?php echo $facebook->getAppId(); ?>',
session : <?php echo json_encode($session); ?>,
status : true,
cookie : true,
xfbml : true
);
$('#stgame').click(sendRequest);
function sendRequest() {
document.getElementById('gameSetup').style.display = 'block';
FB.ui({
method: 'apprequests',
message: '<?=$me[name];?> has invited you to a fun game of Towers. To play, just click on accept. Towers is a "3D" tile stacking word game.',
},
function (response) {
if (response && response.request_ids) {
var requests = response.request_ids.join(',');
$.post('handle_requests.php',{uid: <?php echo $uid; ?>, request_ids: requests},function(resp) {
window.location.replace("play.php?" + resp);
});
} else {
document.getElementById('gameSetup').style.display = 'none';
}
});
return false;
}
};
(function() {
var e = document.createElement('script');
e.src = document.location.protocol + '//connect.facebook.net/en_US/all.js';
e.async = true;
document.getElementById('fb-root').appendChild(e);
}());
</script>
however, I need to alter it so that I can send a variable to "sendRequest" and change the trigger to an inline "onClick"
To do this I have created a link on the page like this:
<a><img src=/images/start.png onClick=sendRequest('1234556');></a>
and change the sendRequest function to sendRequest(variable) so that it can take the variable
However when I do this, each time I click on the button that has the onClick trigger, it gives me an error "cant find variable sendRequest"
I think this is because the onClick cant see the sendRequest function.
So my question is, how do I call that function from the onClick. bearing in mind that there will be multiple buttons on the page that will need to call the function giving the function a different variable value on each of them.
My current code looks like this:
<a><img src=/images/start.png onClick=sendRequest('123');></a>
<a><img src=/images/start.png onClick=sendRequest('456');></a>
<div id="fb-root"></div>
<script>
window.fbAsyncInit = function() {
FB.init({
appId : '<?php echo $facebook->getAppId(); ?>',
session : <?php echo json_encode($session); ?>,
status : true,
cookie : true,
xfbml : true
}
);
function sendRequest(opponent) {
document.getElementById('gameSetup').style.display = 'block';
FB.ui({
method: 'apprequests',
to:opponent,
message: '<?=$me[name];?> has invited you to a fun game" tile stacking word game.',
},
function (response) {
if (response && response.request_ids) {
var requests = response.request_ids.join(',');
$.post('handle_requests.php',{uid: <?php echo $uid; ?>, request_ids: requests},function(resp) {
window.location.replace("play.php?" + resp);
});
} else {
document.getElementById('gameSetup').style.display = 'none';
}
});
return false;
}
};
(function() {
var e = document.createElement('script');
e.src = document.location.protocol + '//connect.facebook.net/en_US/all.js';
e.async = true;
document.getElementById('fb-root').appendChild(e);
}());
</script>
If anyone could tell me how I can call this function I would appreciate it. as far as I am aware, the function needs to stay inside the window,fbAsyncInit function in order to actually work.
Any help would be greatly appreciated.
First of all, I haven't looked to all your code but I really think that you should do more effort on it (formatting and logic).
Anyway, to the point:
You can have a flag that will only set to true when the JS-SDK is loaded, and you can "block" any process outside the window.fbAsyncInit till the flag is set. Something like:
<div id="fb-root"></div>
<script>
var isLoaded = false;
window.fbAsyncInit = function() {
isLoaded = true;
...
...
}
function myFunc(myVar) {
if(isLoaded) {
// Do FB related stuff
}
}
I think the problem is that the sendRequest method is inside the function scope, so not available in the global namespace (where the dom could get to it). You might try changing:
function sendRequest()...
to
window.sendRequest = function()...
I should also mention that there is almost never a good reason to attach events directly to the dom like that. Even where you are rendering that JavaScript variable data in a templating language like php it is far better to set the variable values in an associative way that does not couple the event to the dom (but could couple the data to the element).