v0.1 of flickr sparkles adds spaklines for individual photos on the stream and photo...
[FlickrHacks.git] / _greasemonkey_ / flickr_more_sparkles.user.js
blob428c21dcdf49c3e656480cc23a7a6ef4fdc82e6b
1 // ==UserScript==
2 // @name        Flickr More Sparkles
3 // @namespace   http://6v8.gamboni.org/
4 // @description Add Sparkle lines for stats of individual photos
5 // @version        0.1
6 // @identifier  http://6v8.gamboni.org/IMG/js/flickr_more_sparkles.user.js
7 // @date           2008-12-10
8 // @creator        Pierre Andrews (mortimer.pa@free.fr)
9 // @include *flickr.com/photos/*
10 // @exclude *flickr.com/photos/*/alltags*
11 // @exclude *flickr.com/photos/organize*
12 // ==/UserScript==
14 // --------------------------------------------------------------------
16 // This is a Greasemonkey user script.
18 // To install, you need Greasemonkey: http://greasemonkey.mozdev.org/
19 // Then restart Firefox and revisit this script.
20 // Under Tools, there will be a new menu item to "Install User Script".
21 // Accept the default configuration and install.
23 // --------------------------------------------------------------------
24 // Copyright (C) 2008 Pierre Andrews
26 // This program is free software; you can redistribute it and/or
27 // modify it under the terms of the GNU General Public License
28 // as published by the Free Software Foundation; either version 2
29 // of the License, or (at your option) any later version.
31 // This program is distributed in the hope that it will be useful,
32 // but WITHOUT ANY WARRANTY; without even the implied warranty of
33 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
34 // GNU General Public License for more details.
36 // The GNU General Public License is available by visiting
37 //   http://www.gnu.org/copyleft/gpl.html
38 // or by writing to
39 //   Free Software Foundation, Inc.
40 //   51 Franklin Street, Fifth Floor
41 //   Boston, MA  02110-1301
42 //   USA
45 (function () {
47         //update information
48         var SCRIPT = {
49                 name: "Flickr More Sparkles",
50                 namespace: "http://6v8.gamboni.org/",
51                 description: "Add Sparkle lines for stats of individual photos",
52                 identifier: "http://6v8.gamboni.org/IMG/js/flickr_more_sparkles.user.js",
53                 version: "0.1",                                                         // version
54                 date: (new Date("2008-12-10"))          // update date
55                 .valueOf()
56         };
59         /***********************************************************************
60          * Flickr Localisation
61          **********************************************************************/
63         var FlickrLocaliser = function(locals) {
64                 this.init(locals);
65         }
66         FlickrLocaliser.prototype = {
67                 selectedLang: undefined,
68                 localisations: undefined,
69                 getLanguage: function() {
70                         if(!this.selectedLang) {
71                                 var langA = $x1("//p[@class='LanguageSelector']//a[contains(@class,'selected')]");
72                                 if(langA) {
73                                         var matches = /\/change_language.gne\?lang=([^&]+)&.*/.exec(langA.href);
74                                         if(matches && matches[1]) {
75                                                 this.selectedLang = matches[1];
76                                                 return this.selectedLang;
77                                         }
78                                 }
79                                 return false;
80                         } else return this.selectedLang;
81                 },
83                 init: function(locals) {
84                         this.localisations = locals;
85                 },
87                 localise: function(string, params) {
88                         if(this.localisations && this.getLanguage()) {
89                                 var currentLang = this.localisations[this.selectedLang];
90                                 if(!currentLang) currentLang = this.localisations[this.localisations.defaultLang];
91                                 var local = currentLang[string];
92                                 if(!local) return string;
93                                 for(arg in params) {
94                                         var rep = new RegExp('@'+arg+'@','g');
95                                         local = local.replace(rep,params[arg]);
96                                 }
97                                 local =local.replace(/@[^@]+@/g,'');
98                                 return local;
99                         } else return undefined;
100                 }
102         }
104         /*****************************Flickr Localisation**********************/
108         function M8_log() {
109                 if(unsafeWindow.console)
110                         unsafeWindow.console.log(arguments);
111                 else
112                         GM_log(arguments);
113         }
115         /*
116           Xpath trickery, from:
117           http://ecmanaut.blogspot.com/2006/07/expressive-user-scripts-with-xpath-and.html
118          */
119         function $x( xpath, root )
120                 {
121                         var doc = root ? root.evaluate?root:root.ownerDocument : document;
122                         var got = doc.evaluate( xpath, root||doc, null, 0, null ), next;
123                         var result = [];
124                         while( next = got.iterateNext() )
125                                 result.push( next );
126                         return result;
127                 }
130    function $x1(xpath, root) {
131                                 var doc = root ? root.evaluate?root:root.ownerDocument : document;
132                 return document.evaluate(
133                                                                  xpath,
134                                                                  root||doc,
135                                                                  null,
136                                                                  XPathResult.FIRST_ORDERED_NODE_TYPE, null
137                                                                  ).singleNodeValue;
138         }
140         function foreach( xpath, cb, root )
141         {
142                 var nodes = $x( xpath, root ), e = 0;
143                 for( var i=0; i<nodes.length; i++ )
144                         e += cb( nodes[i], i ) || 0;
145                 return e;
146         }
150         function getObjectMethodClosure(object, method) {
151                 return function(arg) {
152                         return object[method](arg);
153                 }
154         }
157         var flickrmoresparkles = function() {this.init();}
159         flickrmoresparkles.prototype = {
161         localiser: new FlickrLocaliser({
162                                                                          'en-us' : {'photo_stats': 'Photo stats'},
163                                                                          'fr-fr':{},
164                                                                          defaultLang:'en-us'
165                                                                            }),
167           makeSpark: function(url, insert, withLink) {
168                   GM_xmlhttpRequest({
169                         method:"GET",
170                                                           url:url,
171                                                           onload:function(details) {
172                                                                 var start = details.responseText.indexOf("F.photoViews = {");
173                                                                 var end = details.responseText.indexOf("</script>",start);
174                                                                 var code =      details.responseText.substring(start,end).replace("F.photoViews = {","").replace("foreGraph:","");
175                                                                 code = code.substring(0,code.lastIndexOf("]"))+"]";
176                                                                 var evaled = eval(code);
177                                                                 var values = '';
178                                                                   var max = 0;
179                                                                 var min = -1;
180                                                                 for(i=0;i<evaled.length;i++) {
181                                                                   var val = parseInt(evaled[i].views);
182                                                                   if(min<0 || min>val)
183                                                                         min = val;
184                                                                   if(max<val)
185                                                                         max = val;
186                                                                   if(val > 0)
187                                                                   values += ','+val;
188                                                                   else
189                                                                         values += ',0';
190                                                                 }
191                                                                 if(max>0) {
192                                                                 values=values.substring(1);
193                                                                   var imgtxt = "<img src=\"http://chart.apis.google.com/chart?"
194                                                                   + "cht=ls"
195                                                                 + "&chds="+min+','+max
196                                                                   + "&chs=60x30"
197                                                                   + "&chd=t:"+values
198                                                                   + "&chco=0063DC"
199                                                                   + "&chls=1,1,0"
200                                                                   + "&chm=o,990000,0,60,4"
201                                                                   + "&chxt=r,x,y"
202                                                                   + "&chxs=0,990000,11,0,_|1,990000,1,0,_|2,990000,1,0,_"
203                                                                   + "&chxl=0:|"+val+"|1:||2:||"
204                                                                   + "&chxp=0,"+val
205                                                                   + "\">";
206                                                                   if(withLink) {
207                                                                         insert.innerHTML += "<a href="+url+">"+imgtxt+"</a>";
208                                                                   } else  {
209                                                                                                                                                 insert.innerHTML +=imgtxt;
210                                                                   }
211                                                                 }
212                                                           }
213                                                         });
215           },
217           init: function() {
218                 var statIT = $x1("//td[@class='RHS']/ul/li[3]/a");
219                 if(!statIT || statIT.innerHTML != "Photo stats") {
220                  statIT = $x1("//td[@class='RHS']/ul/li[2]/a");
221                 }
222                 if(statIT && statIT.innerHTML == "Photo stats") {
223                   var url = statIT.href;
224                   this.makeSpark(url,statIT);
225                 } else if(document.title == "Flickr: Your Photostream"){
226                   var self = this;
227                   foreach("//p[@class='Activity']",function(el) {
228                                         var ael = $x1("a",el);
229                                         self.makeSpark(ael.href+"/stats",el,true);
230                                   });
231           }
232                 }
234         }
235         //======================================================================
236         // launch
237         try {
238                 window.addEventListener("load", function () {
239                                                                         try {
241                                                                                 // update automatically (http://userscripts.org/scripts/show/2296)
242                                                                                 win.UserScriptUpdates.requestAutomaticUpdates(SCRIPT);
243                                                                         } catch (ex) {}
245                                                                         var flickrgp = new flickrmoresparkles();
246                 }, false);
247         } catch (ex) {}
248 })();