google-plus-one.pl 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  1. # Copyright (C) 2011–2015 Alex Schroeder <alex@gnu.org>
  2. #
  3. # This program is free software: you can redistribute it and/or modify it under
  4. # the terms of the GNU General Public License as published by the Free Software
  5. # Foundation, either version 3 of the License, or (at your option) any later
  6. # version.
  7. #
  8. # This program is distributed in the hope that it will be useful, but WITHOUT
  9. # ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
  10. # FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.
  11. #
  12. # You should have received a copy of the GNU General Public License along with
  13. # this program. If not, see <http://www.gnu.org/licenses/>.
  14. use strict;
  15. # use warnings;
  16. use v5.10;
  17. AddModuleDescription('google-plus-one.pl', 'Google Plus One Module');
  18. our ($q, $RCName, @MyFooters, @MyAdminCode, %Action, @MyInitVariables, $CommentsPrefix);
  19. my $data_uri = 'data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAEwAAAAYCAMAAABnVuv4AAAAhFBMVEUAAAD////MzMz5+fkWau+zs7Pg4OD39/fh4eHu7u7z8/Pk5OTm5ubX19fx8fHj4+N+fn5jY2Ojo6N/f3/r7Ov29vbq6ero6Oj19fUVau0XafC3zO3q6ulKie2dve89ge1yoexkmeyNsepZk+/T3u4weu4jcu7E1Ox9p+rExMTX4vLi6PBsAh6oAAAAAXRSTlMAQObYZgAAAWtJREFUOMvFldluwjAQRU24LgHTpAsEgglL2Nv//7/OeGLLqlTXD5V6HM/c2NJRMJGilClSGCW8DbwEXgPvA+QqRylKI66kLNiKUZri72X6OQOdKxtn8E+yCTMOjQdBhbvEbNkkA5EBoJKULdc0ltHksv447ZaM7JJMNO1PstLZ9Gw2W9PgSxJXe4V1QZZIxiIGI4AbWs5hso1lFY0BwLXzEcBnWK8qJ9uIDKxlMbjyKnex6SoCcO2CKy7xupxZG57GKSBBbsSmp56btYC1t+l5v9vhNI0QmZyZVC+DPKa4IlkHR8f5gD4hQyzb0AhntvAcuo5U3YHzvrWLCJZFmlZ+pp/Rv9k022a75do0ABWKHCTJ9e0923Bmn6ij92xOrAaA1YpvLPZzCrJO+yxLE2Rm7rnfpfc4UjV+x2TLzEBtAvURvYnIl9UDJU8qcsmSL9myMoNs2VMGWtVZHxT9+N310ErVRYpaCToD9QXtvTL0OWiBLQAAAABJRU5ErkJggg==';
  20. # Make sure journal pages set a global variable which we then use to
  21. # hide the +1 button.
  22. *OldGooglePlusPrintJournal = \&PrintJournal;
  23. *PrintJournal = \&NewGooglePlusPrintJournal;
  24. my $GooglePlusThisPagePrintedJournal;
  25. push(@MyInitVariables, sub {
  26. $GooglePlusThisPagePrintedJournal = 0;
  27. });
  28. sub NewGooglePlusPrintJournal {
  29. $GooglePlusThisPagePrintedJournal = 1;
  30. return OldGooglePlusPrintJournal(@_);
  31. }
  32. # Use a two step Google +1 button to protect your privacy: show this
  33. # at the bottom of every page but not on comment pages, Recent
  34. # changes, and pages that printed a journal.
  35. unshift(@MyFooters, \&GooglePlusPrintFooter);
  36. sub GooglePlusPrintFooter {
  37. my ($id, $rev, $comment) = @_;
  38. if (not $GooglePlusThisPagePrintedJournal
  39. and (GetParam('action', 'browse') eq 'plusone'
  40. or (GetParam('action', 'browse') eq 'browse'
  41. and $id
  42. and $id ne $RCName
  43. and (not $CommentsPrefix or $id !~ /^$CommentsPrefix(.*)/)))) {
  44. return q{
  45. <!-- start of Google+ -->
  46. <script type="text/javascript">
  47. function loadScript(jssource) {
  48. // add javascript
  49. var jsnode = document.createElement('script');
  50. jsnode.setAttribute('type','text/javascript');
  51. jsnode.setAttribute('src',jssource);
  52. document.getElementsByTagName('head')[0].appendChild(jsnode);
  53. // add button
  54. var butn = document.createElement('div');
  55. butn.setAttribute('class', 'g-plusone');
  56. butn.setAttribute('id', 'my_plusone');
  57. var link = document.getElementById('plus1');
  58. link.parentNode.insertBefore(butn, link);
  59. // hide the link
  60. link.innerHTML = "";
  61. // when looking at action=plusone
  62. var ul = document.getElementById('plus1s');
  63. var children = ul.children;
  64. for (var i = 0; i < children.length; i++) {
  65. var li = children[i];
  66. butn = document.createElement('g:plusone');
  67. butn.setAttribute('href', li.firstElementChild.getAttribute('href'));
  68. butn.setAttribute('id', 'my_plusone' + i);
  69. li.appendChild(butn);
  70. }
  71. }
  72. </script>
  73. <p id="plus1">
  74. <a href="javascript:loadScript('https://apis.google.com/js/plusone.js')">
  75. <img src="/pics/plusone-h24.png" alt="Show Google +1" />
  76. </a>
  77. </p>
  78. <!-- end of Google+ -->
  79. };
  80. }
  81. }
  82. # Google +1 list
  83. push(@MyAdminCode, sub {
  84. my ($id, $menuref, $restref) = @_;
  85. push(@$menuref, ScriptLink('action=plusone',
  86. T('Google +1 Buttons'),
  87. 'plusone'));
  88. });
  89. $Action{plusone} = \&DoPlusOne;
  90. sub DoPlusOne {
  91. print GetHeader('', T('All Pages +1'), ''),
  92. $q->start_div({-class=>'content plusone'});
  93. print $q->p(T("This page lists the twenty last diary entries and their +1 buttons."));
  94. my @pages;
  95. foreach my $id (AllPagesList()) {
  96. push(@pages, $id) if $id =~ /^\d\d\d\d-\d\d-\d\d/;
  97. }
  98. splice(@pages, 0, $#pages - 19); # last 20 items
  99. print '<ul id="plus1s">';
  100. foreach my $id (@pages) {
  101. print $q->li(GetPageLink($id), ' ');
  102. }
  103. print "</ul>";
  104. print $q->end_div();
  105. PrintFooter();
  106. }