【发布时间】:2012-01-24 21:57:48
【问题描述】:
亲爱的程序员们,
我正在开发我的 B&B 网站 www.bordemundo.com,并希望在我的网站标题中包含我最新的 Facebook 页面状态更新。
由于我对 JQuery 不是很擅长,所以我运行了 Google 并编辑了一个运行良好的脚本。我设法摆脱了我实际上不需要的日期戳,因为状态每天会更改一次。现在我注意到,当我分享另一个页面的朋友的相册或任何链接或数字信息时,它也会更新。
我宁愿只获取我自己输入的状态更新。你们中有人知道如何使用我编辑的脚本轻松管理这个吗?我附上了下面的代码。
感谢您的考虑,非常感谢您的帮助!
(function($) {
$.fn.faceFeed = function(options) {
/**
* Configuration
*
* `pageName:` The name of your Facebook page. Required.
* `tokenGenerator:` Path to a file that will return a JSON access_token. If defined, this will take
* priority over `accessToken`.
* `accessToken:` A token you generate at <https://developers.facebook.com/tools/explorer>.
* `dateClass:` The class of the `<span>` that contains your date "ago in words". Default: `post-date`
*/
var config = {
pageName: '',
tokenGenerator: '', // default: token.php
accessToken: '',
postsToFetch: 1
};
$.fn.extend(config, options);
/**
* Converts "http://" links into <a> tags.
*
* @param {String} a block of text for which all "http://" links need conversion
* @return {String} the same block of text with URLs re-formatted.
*/
function linkify(text){
if (text) {
text = text.replace(
/((https?\:\/\/)|(www\.))(\S+)(\w{2,4})(:[0-9]+)?(\/|\/([\w#!:.?+=&%@!\-\/]))?/gi,
function(url){
var full_url = url;
if (!full_url.match('^https?:\/\/')) {
full_url = 'http://' + full_url;
}
return '<a href="' + full_url + '">' + url + '</a>';
}
);
}
return text;
}
/**
* Requests your page's status feed from the Open Graph and injects it as HTML into the
* element.
*
* @param {String} accessToken - A generated or provided access token for authorizing
* with the API.
*/
function getPosts(accessToken, self) {
$.ajax({
url: 'https://graph.facebook.com/'+config.pageName+'/feed',
type: 'GET',
data: {
access_token: accessToken,
limit: config.postsToFetch
},
dataType: 'json',
success: function(response) {
self.html('');
for (var c=0; c < response.data.length; c++) {
var status = response.data[c];
var statusMessage = (status.message) ? status.message : status.story;
var txt = linkify(statusMessage);
var row = $('<span class="status"></span>').html(txt);
self.append(row);
}
}
});
}
/*
* Runtime.
*/
return this.each(function() {
var self = $(this);
self.html('<p>Lade Neuigkeiten...</p>');
if (config.tokenGenerator) {
$.ajax({
url: config.tokenGenerator,
type: 'GET',
dataType: 'json',
success: function(generator) {
getPosts(generator.access_token, self);
}
})
} else {
getPosts(config.accessToken, self);
}
});
};
})(jQuery)
【问题讨论】:
标签: jquery post fetch facebook-page