javascript 如何获取 google plus +1 按钮的计数器?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/7403553/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How do I get the counter of a google plus +1 button?
提问by seemann
I have added a google +1 button to a website, but I want to get it's counter so i can do some math over it. is it possible to enter the iframe created by the standard method of creating the +1 button or do I need to make some adjustment?
我在网站上添加了一个 google +1 按钮,但我想得到它的计数器,以便我可以对其进行一些数学计算。是否可以输入通过创建 +1 按钮的标准方法创建的 iframe 或者我需要进行一些调整?
<script type="text/javascript" src="https://apis.google.com/js/plusone.js"></script>
<g:plusone></g:plusone>
I've tried this link:1, but this is not very accurate
我试过这个链接:1,但这不是很准确
回答by Aron Cederholm
If you can access curl/file_get_contents/readfile/wget or some way to fetch an external URL, this is quite simple.
如果您可以访问 curl/file_get_contents/readfile/wget 或某种方式来获取外部 URL,这很简单。
Load the following URL: https://plusone.google.com/_/+1/fastbutton?url=URLENCODED_URI
(UPDATED URL, see note below *)
加载以下网址:https://plusone.google.com/_/+1/fastbutton?url=URLENCODED_URI
(更新的URL,见下文*注)
URLENCODED_URI is the site you wish to know the number of +1's for, e.g. http://www.google.com(http%3A%2F%2Fwww.google.com)
URLENCODED_URI 是您希望了解 +1 次数的网站,例如http://www.google.com(http%3A%2F%2Fwww.google.com)
For example, fetch the URI https://plusone.google.com/_/+1/fastbutton?url=http://www.google.com/
(UPDATED URI) and locate the first occurance of window.__SSR = {'c': 32414.0 ,'si'
. Preferably use regexp for this, but I'll leave the implementation to you and your chosen programming language (server side or client side).
例如,获取 URI https://plusone.google.com/_/+1/fastbutton?url=http://www.google.com/
( UPDATED URI) 并定位window.__SSR = {'c': 32414.0 ,'si'
. 最好为此使用正则表达式,但我会将实现留给您和您选择的编程语言(服务器端或客户端)。
The float number following 'c'
is the number of +1's the site have. For google.com this is 32,414. Don't worry about the float, you can safely convert it to an integer.
下面的浮点数'c'
是该站点拥有的 +1 的数量。对于 google.com,这是 32,414。不要担心浮点数,您可以安全地将其转换为整数。
* UPDATE:The URL has been updated as the old URL started to 404. Remember, this is expected as this is an unofficial method. There exist no official method (yet).
* 更新:URL 已更新,因为旧 URL 开始为 404。请记住,这是预期的,因为这是一种非官方方法。不存在官方方法(还)。
回答by barro32
Could you use a callback function to grab the value of the div that displays the count?
您可以使用回调函数来获取显示计数的 div 的值吗?
function count() {
var count = $('#aggregateCount').html();
}
<g:plusone callback="count()"></g:plusone>
I'm basing this off the bubble annotation button, I haven't tested it but something like this should work.
我基于气泡注释按钮,我还没有测试过它,但像这样的东西应该可以工作。
回答by Eduardo
A pure client-side solution that works for me to get the Google Plus counter is as follows. It does not need an API key.
对我来说,获取 Google Plus 计数器的纯客户端解决方案如下。它不需要 API 密钥。
var url = "http://www.yoursite-to-be-counted.com";
var data = {
"method":"pos.plusones.get",
"id": url,
"params":{
"nolog":true,
"id": url,
"source":"widget",
"userId":"@viewer",
"groupId":"@self"
},
"jsonrpc":"2.0",
"key":"p",
"apiVersion":"v1"
};
$.ajax({
type: "POST",
url: "https://clients6.google.com/rpc",
processData: true,
contentType: 'application/json',
data: JSON.stringify(data),
success: function(r){
setCount($(".google-plus-count"), r.result.metadata.globalCounts.count);
}
});
var setCount = function($item, count) {
if (count) {
$item.text(count);
}
};
Then I have some html with
然后我有一些 html
<div class="google-plus-count"></div>
Credits here goes to thisanswer.
这里的积分来自这个答案。