简体   繁体   English

使用JavaScript修改Facebook共享内容

[英]Modifying Facebook share content with JavaScript

I've been successfully using the Sharrre plugin to add a Facebook share button to a page but I now need to change the title and description of the shared link. 我已经成功使用Sharrre插件在页面上添加了Facebook共享按钮,但是现在我需要更改共享链接的标题和描述。

I believe this was possible in the past before Facebook updated sharer.php, with the new version no longer accepting parameters for title, etc. I can change the og:title attribute content via JavaScript but when Facebook requests the page these changes are (obviously) not present. 我相信过去在Facebook更新sharer.php之前是有可能的,新版本不再接受title参数,等等。我可以通过JavaScript更改og:title属性的内容,但是当Facebook请求页面时,这些更改是(显然) 不存在。

How are sites like Buzzfeed accomplishing this with their quizzes, where the outcome is shared? 像Buzzfeed这样的网站如何通过测验(共享结果)来完成此任务?

You need to detect the Facebook scraper server side, they use unique User Agent string. 您需要检测Facebook爬虫服务器端,它们使用唯一的用户代理字符串。

I use this one 我用这个

preg_match('/facebookexternalhit/i', $_SERVER['HTTP_USER_AGENT'])

Based on this, change og:title and og:description, when FB scrapes your website it will see changed params. 基于此,更改og:title和og:description,当FB抓取您的网站时,它将看到已更改的参数。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM