google+共享和oneninteraction-未确认

google+ share and onendinteraction - no confirm

本文关键字:确认 oneninteraction- 共享 google+      更新时间:2023-09-26

我正在设置一个google+共享按钮,想知道何时有人共享了链接,以便我可以执行操作。您可以使用onendinteraction属性在共享按钮上注册回调,文档中指出,每当关闭共享框和完成共享时,都会调用该回调。

我的函数是在窗口关闭时调用的,但不是在链接实际共享时调用的:

function redirectGooglePlus(jsonParam) {
    alert(jsonParam.type);
}
<div class="g-plus" data-action="share" data-annotation="vertical-bubble" data-height="60"
                 data-href="http://mywebsite.com" data-onendinteraction="redirectGooglePlus"></div>

我的函数redirectGooglePlus只在hover类型时被调用,而从不调用confirm(它应该表示共享已经完成。

有人知道为什么不使用confirm调用函数吗?

仅供参考,谷歌共享文档如下:https://developers.google.com/+/web/share/

因此,这似乎是google+共享按钮当前实现的一个错误:

https://code.google.com/p/google-plus-platform/issues/detail?id=396

我现在使用的(可怕)解决方法是为onendinteraction查找2个悬停事件。如果事件很快连续发生(不到1秒),那么他们很可能已经共享了该项目。

这一次做得很晚,所以可能不相关,但根据他们网络平台的谷歌开发者页面,你似乎可以使用类似于-的javascript代码进入当前用户活动列表

var request = gapi.client.plus.activities.list({
  'userId' : 'me',
  'collection' : 'public'
});
request.execute(function(resp) {
  var numItems = resp.items.length;
  for (var i = 0; i < numItems; i++) {
    console.log('ID: ' + resp.items[i].id + ' Content: ' +
      resp.items[i].object.content);
  }
});

生成和测试查询终点的在线工具在开发者页面上

生成一个自定义查询字符串并将其附加到用户正在共享的链接的末尾,可以解析从端点返回的JSON,以检查该特定链接是否已在用户活动流上共享。返回的JSON如下所示-

{
 "items": [
  {
   "title": "",
   "published": "2015-06-12T16:39:11.176Z",
   "url": "https://plus.google.com/+UserID/posts/PostID",
   "object": {
    "content": "",
    "attachments": [
     {
      "objectType": "article",
      "url": "http://www.example.com"
     }
    ]
   }
  }
 ]
}

如果与自定义查询的链接在其中一个返回项的附件部分,Voila!它已被共享。

解决此问题的可能方案如下。

  1. 让用户打开一个新窗口,其中只包含传递要共享的url的共享按钮代码
  2. 在窗口加载时将变量设置为0
  3. 在函数重定向中,当调用data-oninteraction时,GooglePlus会将变量更新为1
  4. 检查窗口是否关闭,并验证变量是否设置为

    function closeWin(){
     if(x==0){
        //not shared before leaving code;
     }else{
        //shared before window closed;
     }
    }    
    body onbeforeunload="closeWin()"