update version number and date
[FlickrHacks.git] / _greasemonkey_ / flickrpooldate.user.js
blob436ea4db5b15a0effd13f35227cd4f1826bf28d8
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.4
6 // @identifier  http://6v8.gamboni.org/IMG/js/flickrpooldate.user.user.js
7 // @date           2006-10-06
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.4",                                                         // version
53                 date: (new Date("2006-10-06"))          // 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                         
101                         this.waitForGroupId();
102                 },
104                 waitForGroupId: function() {
105                         if(this.group_id) {
106                                 
107                                 var self = this;                        
108                                 var listener = {
109                                         flickr_groups_pools_getPhotos_onLoad: function(success, responseXML, responseText, params){
110                                                 if(success) {
111                                                         var rsp = responseText.replace(/<\?xml.*\?>/,'');
113                                                         rsp = new XML(rsp);
114                                                         self.insertDates(rsp);
115                                                 } else
116                                                         M8_log(responseText);
117                                         }
118                                 };
119                                 
120                                 var params =  {
121                                         group_id:this.group_id,
122                                         per_page:30,
123                                         page: this.page
124                                 };
126                                 if(matches = /\/pool\/([0-9]+@N[0-9]{2})\/?/.exec(document.location.pathname)) {
127                                         params['user_id'] = matches[1];
128                                 } 
129                                 if(matches = /\/pool\/tags\/([^\/]+)/.exec(document.location.pathname)) {
130                                         params['tags'] = matches[1];
131                                         params['per_page'] = 24;
132                                 }
133                                 unsafeWindow.F.API.callMethod('flickr.groups.pools.getPhotos',params, listener);
134                         } else {
135                                 setTimeout(getObjectMethodClosure(this,'waitForGroupId'),1000);
136                         }
137                 },
139                 formatDate: function(seconds) {
140                         var now = new Date();
141                         now = now.getTime()+now.getTimezoneOffset() * 60*1000;
142                         var when = (now/1000) - seconds;
143                         var days = parseInt(when/(3600*24));
144                         if(days > 10) {
145                                 var date = new Date(seconds*1000);
146                                 return "on "+date.toLocaleDateString();
147                         } else {
148                                 var ret = '';
149                                 if(days > 0)
150                                         ret = days + ' day'+((days>1)?'s':'');
151                                 var rest = when - days*3600*24;
152                                 if(when%(3600*24) > 0) {
153                                         if(rest/3600 > 1) {
154                                                 if(ret) ret += ', ';
155                                                 ret += parseInt(rest/3600)+' h';
156                                         } else if(days < 1)
157                                                 ret = "less than an hour";
158                                 } else if(days < 1)
159                                                 ret = "less than an hour";
160                                 return ret +' ago';
161                         }
162                 },
164                 insertDates: function(photos) {
165                                 var images = document.evaluate(
166                                                                                          "//div/p[contains(@class,'PoolList')]/a/img",
167                                                                                   document, null, XPathResult.UNORDERED_NODE_SNAPSHOT_TYPE, null); 
168                                 for(var i = 0; i < images.snapshotLength; i++) {  
169                                         var image = images.snapshotItem(i);
170                                         var matches = /\/([0-9]+)_/.exec(image.src);
171                                         if(matches) {
172                                                 var photo = photos..photo.(@id == matches[1]);
173                                                 if(photo) {
174                                                         var date = this.formatDate(photo.@dateadded);
175                                                         var div = image.parentNode.parentNode.appendChild(document.createElement('div'));
176                                                         var abbr = document.createElement('abbr');
177                                                         abbr.title = new Date(photo.@dateadded*1000).toLocaleString();
178                                                         abbr.setAttribute('style','font-size:90%;color: #6C6C6C;');
179                                                         abbr.appendChild(document.createTextNode('posted '+ date));
180                                                         div.appendChild(abbr);
181                                                 }
182                                         }
183                                 }
184                 }
186                 
187         }
188         //======================================================================
189         // launch
190         try {
191                 window.addEventListener("load", function () {
192                                                                         try {
193                                                                                 
194                                                                                 // update automatically (http://userscripts.org/scripts/show/2296)
195                                                                                 win.UserScriptUpdates.requestAutomaticUpdates(SCRIPT);
196                                                                         } catch (ex) {} 
197                                                                         
198                                                                         var flickrgp = new FlickrPoolDate();
199                 }, false);
200         } catch (ex) {}
201 })();