Auto Refresh for Google+

Auto Post Refresh for Google+

目前为 2017-03-27 提交的版本。查看 最新版本

  1. /*
  2.  
  3. Following code belongs to Auto Refresh for Google+.
  4. Copyright (C) 2017 Jackson Tan
  5. This program is free software: you can redistribute it and/or modify
  6. it under the terms of the GNU General Public License as published by
  7. the Free Software Foundation, either version 3 of the License, or
  8. (at your option) any later version.
  9.  
  10. This program is distributed in the hope that it will be useful,
  11. but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. GNU General Public License for more details.
  14.  
  15. You should have received a copy of the GNU General Public License
  16. along with this program. If not, see <http://www.gnu.org/licenses/>.
  17.  
  18. */
  19.  
  20. // ==UserScript==
  21. // @id GPlusAutoRefresh
  22. // @name Auto Refresh for Google+
  23. // @version 0.1.0
  24. // @namespace gplus.autorefresh
  25. // @author Jackson Tan
  26. // @description Auto Post Refresh for Google+
  27. // @include https://plus.google.com/*
  28. // @exclude /https://plus\.google\.com(/u/\d+)?/?stream/circles/.+/i
  29. // @exclude /https://plus\.google\.com(/u/\d+)?/?b/.+/i
  30. // @run-at document-end
  31. // @grant GM_xmlhttpRequest
  32. // ==/UserScript==
  33.  
  34.  
  35. (function () {
  36. 'use strict';
  37. var constants = {
  38. POST_CLASS_NAME: 'V2SCpf vCjazd',
  39. POST_UID_ATTRIBUTE: 'data-iid',
  40. POST_COLUMN_CONTAINER: 'H68wj jxKp7'
  41. };
  42.  
  43. function getCurrentPostList() {
  44. var elements = document.getElementsByClassName(constants.POST_CLASS_NAME);
  45. var list = [];
  46.  
  47. Array.prototype.forEach.call(elements, function (e) {
  48. list.push(e.getAttribute(constants.POST_UID_ATTRIBUTE));
  49. });
  50.  
  51. return list;
  52. }
  53.  
  54. function handleResponse(newPost, lastInsertIndex) {
  55. var currentPost = getCurrentPostList();
  56. var newNodes = newPost.filter(
  57. function (e) {
  58. return currentPost.indexOf(e.getAttribute(constants.POST_UID_ATTRIBUTE)) < 0;
  59. }
  60. ).reverse();
  61.  
  62. var parentNodes = document.getElementsByClassName(constants.POST_COLUMN_CONTAINER);
  63. var parentLength = parentNodes.length;
  64.  
  65. Array.prototype.forEach.call(newNodes, function (e, i) {
  66. insertIndex = (lastInsertIndex + 1) % parentLength;
  67. var parentNode = parentNodes[insertIndex];
  68. insertIndex === 0 ? parentNode.insertBefore(e, parentNode.firstChild.nextSibling) : parentNode.insertBefore(e, parentNode.firstChild);
  69. });
  70. }
  71.  
  72. function getUpdatePostList(handler, lastInsertIndex) {
  73. var http = new XMLHttpRequest();
  74. var url = document.URL.match(/https:\/\/plus\.google\.com(\/b\/\d+)/) != undefined ? document.URL.match(/https:\/\/plus\.google\.com(\/b\/\d+)/)[0] : document.URL.match(/https:\/\/plus\.google\.com(\/u\/\d)?/)[0];
  75. url += location.search.indexOf("?") != -1 ? '/' + location.search + '&_reqid=' + (new Date().getTime() % 1000000) + '&rt=j' : '/?_reqid=' + (new Date().getTime() % 1000000) + '&rt=j';
  76. http.open("GET", url, true);
  77.  
  78. http.setRequestHeader("Content-type", "application/x-www-form-urlencoded;charset=UTF-8");
  79. http.responseType = 'document';
  80. http.onreadystatechange = function () {
  81. if (http.readyState == 4 && http.status == 200) {
  82. var body = http.responseXML.body;
  83. var elements = body.getElementsByClassName(constants.POST_CLASS_NAME);
  84. var list = [];
  85.  
  86. Array.prototype.forEach.call(elements, function (e) {
  87. list.push(e);
  88. });
  89.  
  90. handler(list, lastInsertIndex);
  91. }
  92. };
  93. http.send();
  94. }
  95.  
  96. var insertIndex = -1;
  97. setInterval(function () { getUpdatePostList(handleResponse, insertIndex); }, 10000);
  98. })();