- Fixed a bunch of bugs for existing paginations
[FlickrHacks.git] / _greasemonkey_ / flyrpool.user.js
blob97ac1b5dc8b712ab3d7c0ff9eddc1200de7000dd
1 // ==UserScript==
2 // @name        Flyr Pool
3 // @namespace   http://6v8.gamboni.org/
4 // @description display directly a google map of the pool using Flyr. Works on most of the pages showing multiple photos (group pool, tag pool, search, user page, set pool) 
5 // @version        0.3
6 // @identifier  http://6v8.gamboni.org/IMG/js/flyrpool.user.js
7 // @date           2006-05-18
8 // @creator        Pierre Andrews (mortimer.pa@free.fr)
9 // @include http://*flickr.com/photos/*
10 // @include http://*flickr.com/groups/*
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
43 (function () {
45         var SCRIPT = {
46                 name: "Flyr Pool",
47                 namespace: "http://6v8.gamboni.org/",
48                 description: "Display directly a google map of the pool using Flyr.",
49                 source: "http://6v8.gamboni.org/Flyr-Pool.html",                        // script homepage/description URL
50                 identifier: "http://6v8.gamboni.org/IMG/js/flyrpool.user.js",
51                 version: "0.3",                                                         // version
52                 date: (new Date(2006, 05, 18))          // update date
53                 .valueOf()
54                 };
56         var win = (unsafeWindow || window.wrappedJSObject || window);   
57         var API_KEY = "e8c3239ff04c102ce2d6ed885bf99005";
59         win.FlyrPool = function() {;}
61         win.FlyrPool.prototype = {
62                 photos: new Array(),
63                 photoCnt: 0,
64                 cntTries: 20,
65                 cnt: 0,
66                 geotag: false,
69                 checkTags: function(photoId) {
70                         var url = 'http://www.flickr.com/services/rest/?method=flickr.tags.getListPhoto&api_key='
71                         +API_KEY
72                         +"&photo_id="
73                         +photoId;
75                         var self = this;
77                         function onRez(result) {
78                                         self.cnt++;
79                                         var doc = result.responseText.replace(/<\?xml.*\?>/,'');
80                                         doc = new XML(doc);
81                                         for each (tag in doc..tag) {
82                                                 if(tag.@raw.match(/geo:(lat|long?)=/)) {
83                                                         self.geotag = true;
84                                                         self.photos.push(photoId);
85                                                         break;
86                                                 }
87                                         }
88                         }
90                         GM_xmlhttpRequest({
91                                         method: 'GET',
92                                                 url: url,
93                                                 headers: {
94                                                 'User-agent': 'Mozilla/4.0 (compatible) Greasemonkey Flyr Pool',
95                                                         'Accept': 'application/atom+xml,application/xml,text/xml'
96                                                         },
97                                                 onload: onRez});
98                 },
100                 getPhotos: function() {
101                         var tagNodesSnapshot = 
102                         document.evaluate( "/html/body/div[@id='Main']/div[1]//a|//p[@class='Photo']/a",
103                                                            document.body, null, XPathResult.UNORDERED_NODE_SNAPSHOT_TYPE, null );                       
104                         for(var i=0; i<tagNodesSnapshot.snapshotLength; i++)
105                                 {
106                                         var streamListNode = tagNodesSnapshot.snapshotItem(i);
107                                         href = streamListNode.href;
108                                         matches = /\/([0-9]+)\//.exec(href);
109                                         if(matches) {
110                                                 photoId = matches[1];
111                                                 this.photoCnt++;
112                                                 this.checkTags(photoId);
113                                         }
114                                 }
115                 },
117                 insertFlyr: function() {
118                         
119                 
120                         var gmd = document.createElement("div");
121                         gmd.setAttribute("style","font-size:85%; margin-bottom: 1em;clear: right;");
122                         //Group/Set pool
123                         var goodStuffs;
124                         var xpath = document.evaluate("//div[@class='HoldPhotos']",
125                                                                                   document.body,
126                                                                                   null,
127                                                                                   XPathResult.ANY_UNORDERED_NODE_TYPE,
128                                                                                   null);
129                         if(xpath) {
130                                 goodStuffs = xpath.singleNodeValue;
131                         }
132                         if(goodStuffs) {
133                                 goodStuffs.insertBefore(gmd,goodStuffs.firstChild);
134                         } else {
135                                 //When we see the calendar
136                                 var xpath = document.evaluate("//table[@class='SurfsUp']",
137                                                                                           document.body,
138                                                                                           null,
139                                                                                           XPathResult.ANY_UNORDERED_NODE_TYPE,
140                                                                                           null);
141                                 if(xpath) {
142                                         goodStuffs = xpath.singleNodeValue;
143                                         if(goodStuffs) goodStuffs.parentNode.insertBefore(gmd,goodStuffs.nextSibling);
144                                         else {                          
145                                                 xpath = null;
146                                                 //in the user page
147                                                 xpath = document.evaluate("//div[@class='StreamView']",
148                                                                                                   document.body,
149                                                                                                   null,
150                                                                                                   XPathResult.ANY_UNORDERED_NODE_TYPE,
151                                                                                                   null);
152                                                 if(xpath) {
153                                                         goodStuffs = xpath.singleNodeValue;
154                                                         if(goodStuffs) {
155                                                                 goodStuffs = goodStuffs.parentNode.parentNode.parentNode.insertBefore(document.createElement('tr'),goodStuffs.parentNode.parentNode);
156                                                                 goodStuffs = goodStuffs.appendChild(document.createElement('td'));
157                                                                 goodStuffs.setAttribute('colspan',2);
158                                                                 goodStuffs.appendChild(gmd);
160                                                         } else {
161                                                                 goodStuffs = document.getElementById('setThumbs');
162                                                                 if(goodStuffs) goodStuffs.insertBefore(gmd,goodStuffs.firstChild);
163                                                                 else gmd = null;
164                                                         }
165                                                 }
166                                         }
167                                 }
168                         }
169                         
170                         if(gmd) {
171                         
172                                 //highly inspired by greasemap
173                                 function insertFlyrMap() {                              
174                                         var f = document.createElement('iframe');
175                                         var close = document.createElement("a");
176                                         close.innerHTML = "&nbsp;Close";
177                                         close.href="#";
178                                         close.addEventListener('click',function() {
179                                                                                            gmd.removeChild(f);
180                                                                                            gmd.removeChild(close);
181                                                                                         },false);
182                                         gmd.appendChild(close);
183                                         
184                                         f.setAttribute('width', '812');
185                                         f.setAttribute('height', '420');
186                                         f.setAttribute('src', mapurl);
187                                         gmd.appendChild(f);
188                                 }
190                                 var mapurl = 'http://flyr.whatfettle.com/maps?ids='
191                                                 + encodeURIComponent(this.photos.join(','));
192                                 gmd.innerHTML = this.photos.length+" geotagged photos have been found. <a target=\"_blank\" href=\""+mapurl+"\">Open in new window</a>,";
193                                 var direct = document.createElement("a");
194                                 direct.setAttribute("style","font-weight:bold;");
195                                 direct.innerHTML = "&nbsp;Display Here";
196                                 direct.href="#";
197                                 direct.addEventListener('click',function() {
198                                                                            insertFlyrMap();
199                                                                                 },false);
200                                 gmd.appendChild(direct);
201                         }
202                 },
204                 waitForResults: function() {
205                         this.cntTries--;
206                         if(this.cnt >= this.photoCnt) {
207                                 if(this.geotag) this.insertFlyr();
208                         } else {
209                                 var self = this;
210                                 if(this.cntTries>0) setTimeout(function() {self.waitForResults();},1000);
211                         }
212                 }
213                 
214         }
215         
216         
217         // update automatically (http://userscripts.org/scripts/show/2296)
218         try {
219                 window.addEventListener("load", function () {
220                                                                         try {
221                                                                                 win.UserScriptUpdates.requestAutomaticUpdates(SCRIPT);
222                                                                         } catch (ex) {} 
223                                                                         var fltags = new win.FlyrPool();
224                                                                         fltags.getPhotos();
225                                                                         fltags.waitForResults();
226                                                                 }, false);
227         } catch (ex) {}
231 })();