Skip to content

Instantly share code, notes, and snippets.

@annaliahsiao
Last active December 12, 2018 08:39
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save annaliahsiao/4fe60a53fcc943364c856d2f0f2e6df1 to your computer and use it in GitHub Desktop.
Save annaliahsiao/4fe60a53fcc943364c856d2f0f2e6df1 to your computer and use it in GitHub Desktop.
facebook api v3.1
var fb_share_url = 'https://www.yourwebsite.com', //帶入網址
fb_name, //使用者的FB名稱
fb_id, //使用者的FB ID
perms, //FB帶入參數
fb_app_id = '123456789'; //FB APP_ID
var is_uiwebview = /(iPhone|iPod|iPad).*AppleWebKit(?!.*Safari)/i.test(navigator.userAgent); // 判斷瀏覽裝置
var userAgent = window.navigator.userAgent.toLowerCase(),
line = /line/.test(userAgent), //LINE內建瀏覽器
fbav = /fbav/.test(userAgent); //FB內建瀏覽器
window.fbAsyncInit = function() {
FB.init({
appId : fb_app_id,
cookie : true,
xfbml : true,
version : 'v3.1'
});
getLoginStatus(); //判斷登入狀態
FB.AppEvents.logPageView();
};
(function(d, s, id){
var js, fjs = d.getElementsByTagName(s)[0];
if (d.getElementById(id)) {return;}
js = d.createElement(s); js.id = id;
js.src = "https://connect.facebook.net/en_US/sdk.js";
fjs.parentNode.insertBefore(js, fjs);
}(document, 'script', 'facebook-jssdk'));
function getLoginStatus(){
FB.getLoginStatus(function(response) {
FB.getLoginStatus(function(response) {
if (response.status === 'connected') {
fb_id = response.authResponse.userID;
FB.api('/'+fb_id, function(response) {
fb_name = response.name;
});
}
});
});
}
function login(){
if( navigator.userAgent.match('CriOS') ){
window.open('https://www.facebook.com/dialog/oauth?client_id='+fb_app_id+'&redirect_uri='+document.location.href, '', null);
}else{
FB.login(function(response) {
if (response.authResponse) {
fb_id = response.authResponse.userID;
FB.api('/'+fb_id, function(response) {
fb_name = response.name;
});
}
},{scope: perms});
}
}
var publish,
profile,
blob;
function fb_share(){
if(line){ //LINE瀏覽器使用
location.href = 'https://www.facebook.com/dialog/share?app_id='+fb_app_id+'&display=touch&href='+fb_share_url+'&redirect_uri='+encodeURIComponent(fb_share_url)+'%3Ffrom%3Dfbshare%26';
}else{
if(fbav){ //FB瀏覽器使用
publish = {
method: 'share_open_graph',
action_type: 'og.shares',
mobile_iframe: true,
action_properties: JSON.stringify({
object : {
'og:url': fb_share_url,
'og:title': '', //標題
'og:description': '', //說明
'og:og:image:width': '1200',
'og:image:height': '630',
'og:image': '' //圖片網址
}
})
};
}
else{ //一般瀏覽器使用
publish = {
method: 'share_open_graph',
action_type: 'og.shares',
action_properties: JSON.stringify({
object : {
'og:url': fb_share_url,
'og:title': '', //標題
'og:description': '', //說明
'og:og:image:width': '1200',
'og:image:height': '630',
'og:image': '' //圖片網址
}
})
};
}
FB.ui(publish,function(response) {
// callback
if (response && !response.error_message || navigator.userAgent.match('CriOS')) {
console.log('successful');
}
});
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment