- Fixed a bunch of bugs for existing paginations
[FlickrHacks.git] / _greasemonkey_ / flickrpooldate.user.js
bloba3fd44f40c8758b8841bb7322c673c4e6e942ccf
1 // ==UserScript==
2 // @name         Flickr Pool Date
3 // @namespace   http://6v8.gamboni.org/
4 // @description Show when the photo was added to the group pool
5 // @version        0.5
6 // @identifier  http://6v8.gamboni.org/IMG/js/flickrpooldate.user.user.js
7 // @date           2008-01-19
8 // @creator        Pierre Andrews (mortimer.pa@free.fr)
9 // @include http://*flickr.com/groups/*/pool*
10 // 
11 // ==/UserScript==
13 // --------------------------------------------------------------------
15 // This is a Greasemonkey user script.
17 // To install, you need Greasemonkey: http://greasemonkey.mozdev.org/
18 // Then restart Firefox and revisit this script.
19 // Under Tools, there will be a new menu item to "Install User Script".
20 // Accept the default configuration and install.
22 // --------------------------------------------------------------------
23 // Copyright (C) 2006 Pierre Andrews
24 // 
25 // This program is free software; you can redistribute it and/or
26 // modify it under the terms of the GNU General Public License
27 // as published by the Free Software Foundation; either version 2
28 // of the License, or (at your option) any later version.
29 // 
30 // This program is distributed in the hope that it will be useful,
31 // but WITHOUT ANY WARRANTY; without even the implied warranty of
32 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
33 // GNU General Public License for more details.
34 // 
35 // The GNU General Public License is available by visiting
36 //   http://www.gnu.org/copyleft/gpl.html
37 // or by writing to
38 //   Free Software Foundation, Inc.
39 //   51 Franklin Street, Fifth Floor
40 //   Boston, MA  02110-1301
41 //   USA
44 (function () {
46         //update information
47         var SCRIPT = {
48                 name: " Flickr Pool Date",
49                 namespace: "http://6v8.gamboni.org/",
50                 description: "Show when the photo was added to the group pool",
51                 identifier: "http://6v8.gamboni.org/IMG/js/flickrpooldate.user.user.js",
52                 version: "0.5",                                                         // version
53                 date: (new Date("2008-01-19"))          // update date
54                 .valueOf()
55         };
57         
58         function M8_log() {
59                 if(unsafeWindow.console)
60                         unsafeWindow.console.log(arguments);
61                 else
62                         GM_log(arguments);
63         }
65         
66         function getObjectMethodClosure(object, method) {
67                 return function() {
68                         return object[method](); 
69                 }
70         }
72         var FlickrPoolDate = function() {this.init();}
74         FlickrPoolDate.prototype = {
75                 page: 1,
76                 group_id: null,
78                 init: function() {
80                         var matches = /\/page([0-9]+)\/?$/.exec(document.location.pathname);
81                         if(matches)
82                                 this.page = matches[1];
84                         var self = this;
85                         
86                         var listener = {
87                                 flickr_urls_lookupGroup_onLoad: function(success, responseXML, responseText, params){
88                                         if(success) {
89                                                 var rsp = responseText.replace(/<\?xml.*\?>/,'');
90                                                 rsp = new XML(rsp);
91                                                 self.group_id = rsp.group.@id;  
92                                         } else
93                                                 M8_log(responseText);
94                                 }
95                         };
96                         
97                         unsafeWindow.F.API.callMethod('flickr.urls.lookupGroup', {
98                                 url:document.location                              
99                         }, listener);
100                         this.waitForGroupId();
101                 },
103                 waitForGroupId: function() {
104                         if(this.group_id) {
105                                 
106                                 var self = this;                        
107                                 var listener = {
108                                         flickr_groups_pools_getPhotos_onLoad: function(success, responseXML, responseText, params){
109                                                 if(success) {
110                                                         var rsp = responseText.replace(/<\?xml.*\?>/,'');
112                                                         rsp = new XML(rsp);
113                                                         self.insertDates(rsp);
114                                                 } else
115                                                         M8_log(responseText);
116                                         }
117                                 };
118                                 
119                                 var params =  {
120                                         group_id:this.group_id,
121                                         per_page:30,
122                                         page: this.page
123                                 };
125                                 if(matches = /\/pool\/([0-9]+@N[0-9]{2})\/?/.exec(document.location.pathname)) {
126                                         params['user_id'] = matches[1];
127                                 } 
128                                 if(matches = /\/pool\/tags\/([^\/]+)/.exec(document.location.pathname)) {
129                                         params['tags'] = matches[1];
130                                         params['per_page'] = 24;
131                                 }
132                                 unsafeWindow.F.API.callMethod('flickr.groups.pools.getPhotos',params, listener);
133                         } else {
134                                 setTimeout(getObjectMethodClosure(this,'waitForGroupId'),1000);
135                         }
136                 },
138                 formatDate: function(seconds) {
139                         var now = new Date();
140                         now = now.getTime()+now.getTimezoneOffset() * 60*1000;
141                         var when = (now/1000) - seconds;
142                         var days = parseInt(when/(3600*24));
143                         if(days > 10) {
144                                 var date = new Date(seconds*1000);
145                                 return "on "+date.toLocaleDateString();
146                         } else {
147                                 var ret = '';
148                                 if(days > 0)
149                                         ret = days + ' day'+((days>1)?'s':'');
150                                 var rest = when - days*3600*24;
151                                 if(when%(3600*24) > 0) {
152                                         if(rest/3600 > 1) {
153                                                 if(ret) ret += ', ';
154                                                 ret += parseInt(rest/3600)+' h';
155                                         } else if(days < 1)
156                                                 ret = "less than an hour";
157                                 } else if(days < 1)
158                                                 ret = "less than an hour";
159                                 return ret +' ago';
160                         }
161                 },
163                 insertDates: function(photos) {
164                                 var images = document.evaluate(
165                                                                                          "//div/p[contains(@class,'PoolList')]//a/img",
166                                                                                   document, null, XPathResult.UNORDERED_NODE_SNAPSHOT_TYPE, null); 
167                                 for(var i = 0; i < images.snapshotLength; i++) {  
168                                         var image = images.snapshotItem(i);
169                                         var matches = /\/([0-9]+)_/.exec(image.src);
170                                         if(matches) {
171                                                 var photo = photos..photo.(@id == matches[1]);
172                                                 if(photo) {
173                                                         var date = this.formatDate(photo.@dateadded);
174                                                         var div = image.parentNode.parentNode.appendChild(document.createElement('div'));
175                                                         var abbr = document.createElement('abbr');
176                                                         abbr.title = new Date(photo.@dateadded*1000).toLocaleString();
177                                                         abbr.setAttribute('style','font-size:90%;color: #6C6C6C;');
178                                                         abbr.appendChild(document.createTextNode('posted '+ date));
179                                                         div.appendChild(abbr);
180                                                 }
181                                         }
182                                 }
183                 }
185                 
186         }
187         //======================================================================
188         // launch
189         try {
190                 window.addEventListener("load", function () {
191                                                                         try {
192                                                                                 
193                                                                                 // update automatically (http://userscripts.org/scripts/show/2296)
194                                                                                 win.UserScriptUpdates.requestAutomaticUpdates(SCRIPT);
195                                                                         } catch (ex) {} 
196                                                                         
197                                                                         var flickrgp = new FlickrPoolDate();
198                 }, false);
199         } catch (ex) {}
200 })();