v 0.2 adds sparkles a bit everywhere where stats apply and fix the style of the graph
[FlickrHacks.git] / _greasemonkey_ / flickr_more_sparkles.user.js
blob58b730eb1691fd6d2bee331e7f6b46726528f99f
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 // @include *flickr.com
11 // @include *flickr.com/
12 // @include *flickr.com/activity*
13 // @include *flickr.com/photos/*
14 // @include *flickr.com/photos/*/stats*
15 // @exclude *flickr.com/photos/*/alltags*
16 // @exclude *flickr.com/photos/organize*
17 // ==/UserScript==
19 // --------------------------------------------------------------------
21 // This is a Greasemonkey user script.
23 // To install, you need Greasemonkey: http://greasemonkey.mozdev.org/
24 // Then restart Firefox and revisit this script.
25 // Under Tools, there will be a new menu item to "Install User Script".
26 // Accept the default configuration and install.
28 // --------------------------------------------------------------------
29 // Copyright (C) 2008 Pierre Andrews
31 // This program is free software; you can redistribute it and/or
32 // modify it under the terms of the GNU General Public License
33 // as published by the Free Software Foundation; either version 2
34 // of the License, or (at your option) any later version.
36 // This program is distributed in the hope that it will be useful,
37 // but WITHOUT ANY WARRANTY; without even the implied warranty of
38 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
39 // GNU General Public License for more details.
41 // The GNU General Public License is available by visiting
42 //   http://www.gnu.org/copyleft/gpl.html
43 // or by writing to
44 //   Free Software Foundation, Inc.
45 //   51 Franklin Street, Fifth Floor
46 //   Boston, MA  02110-1301
47 //   USA
50 (function () {
52         //update information
53         var SCRIPT = {
54                 name: "Flickr More Sparkles",
55                 namespace: "http://6v8.gamboni.org/",
56                 description: "Add Sparkle lines for stats of individual photos",
57                 identifier: "http://6v8.gamboni.org/IMG/js/flickr_more_sparkles.user.js",
58                 version: "0.1",                                                         // version
59                 date: (new Date("2008-12-10"))          // update date
60                 .valueOf()
61         };
64         /***********************************************************************
65          * Flickr Localisation
66          **********************************************************************/
68         var FlickrLocaliser = function(locals) {
69                 this.init(locals);
70         }
71         FlickrLocaliser.prototype = {
72                 selectedLang: undefined,
73                 localisations: undefined,
74                 getLanguage: function() {
75                         if(!this.selectedLang) {
76                                 var langA = $x1("//p[@class='LanguageSelector']//a[contains(@class,'selected')]");
77                                 if(langA) {
78                                         var matches = /\/change_language.gne\?lang=([^&]+)&.*/.exec(langA.href);
79                                         if(matches && matches[1]) {
80                                                 this.selectedLang = matches[1];
81                                                 return this.selectedLang;
82                                         }
83                                 }
84                                 return false;
85                         } else return this.selectedLang;
86                 },
88                 init: function(locals) {
89                         this.localisations = locals;
90                 },
92                 localise: function(string, params) {
93                         if(this.localisations && this.getLanguage()) {
94                                 var currentLang = this.localisations[this.selectedLang];
95                                 if(!currentLang) currentLang = this.localisations[this.localisations.defaultLang];
96                                 var local = currentLang[string];
97                                 if(!local) return string;
98                                 for(arg in params) {
99                                         var rep = new RegExp('@'+arg+'@','g');
100                                         local = local.replace(rep,params[arg]);
101                                 }
102                                 local =local.replace(/@[^@]+@/g,'');
103                                 return local;
104                         } else return undefined;
105                 }
107         }
109         /*****************************Flickr Localisation**********************/
113         function M8_log() {
114                 if(unsafeWindow.console)
115                         unsafeWindow.console.log(arguments);
116                 else
117                         GM_log(arguments);
118         }
120         /*
121           Xpath trickery, from:
122           http://ecmanaut.blogspot.com/2006/07/expressive-user-scripts-with-xpath-and.html
123          */
124         function $x( xpath, root )
125                 {
126                         var doc = root ? root.evaluate?root:root.ownerDocument : document;
127                         var got = doc.evaluate( xpath, root||doc, null, 0, null ), next;
128                         var result = [];
129                         while( next = got.iterateNext() )
130                                 result.push( next );
131                         return result;
132                 }
135    function $x1(xpath, root) {
136                                 var doc = root ? root.evaluate?root:root.ownerDocument : document;
137                 return document.evaluate(
138                                                                  xpath,
139                                                                  root||doc,
140                                                                  null,
141                                                                  XPathResult.FIRST_ORDERED_NODE_TYPE, null
142                                                                  ).singleNodeValue;
143         }
145         function foreach( xpath, cb, root )
146         {
147                 var nodes = $x( xpath, root ), e = 0;
148                 for( var i=0; i<nodes.length; i++ )
149                         e += cb( nodes[i], i ) || 0;
150                 return e;
151         }
155         function getObjectMethodClosure(object, method) {
156                 return function(arg) {
157                         return object[method](arg);
158                 }
159         }
162         var flickrmoresparkles = function() {this.init();}
164         flickrmoresparkles.prototype = {
166         localiser: new FlickrLocaliser({
167                                                                          'en-us' : {'photo_stats': 'Photo stats'},
168                                                                          'fr-fr':{},
169                                                                          defaultLang:'en-us'
170                                                                            }),
172           makeSpark: function(url, insert, withLink, fill,bg) {
173                   GM_xmlhttpRequest({
174                         method:"GET",
175                                                           url:url,
176                                                           headers: { 'Cookie': document.cookie },
177                                                           onload:function(details) {
178                                                                 var start = details.responseText.indexOf("F.photoViews = {");
179                                                                 if(start >= 0) {
180                                                                 var end = details.responseText.indexOf("</script>",start);
181                                                                 var code =      details.responseText.substring(start,end).replace("F.photoViews = {","").replace("foreGraph:","");
182                                                                 code = code.substring(0,code.lastIndexOf("]"))+"]";
183                                                                 var evaled = eval(code);
184                                                                 var values = '';
185                                                                   var max = 0;
186                                                                 var min = -1;
187                                                                 for(i=0;i<evaled.length;i++) {
188                                                                   var val = parseInt(evaled[i].views);
189                                                                   if(isNaN(val)) val =0;
190                                                                   if(min<0 || min>val)
191                                                                         min = val;
192                                                                   if(max<val)
193                                                                         max = val;
194                                                                   if(val > 0)
195                                                                   values += ','+val;
196                                                                   else
197                                                                         values += ',0';
198                                                                 }
199                                                                 if(max>0) {
200                                                                 values=values.substring(1);
201                                                                   var imgtxt = "<img src=\"http://chart.apis.google.com/chart?"
202                                                                   + "cht=ls"
203                                                                 + "&chds="+0+','+max
204                                                                         + "&chs="+(fill?"100x32":"81x22")
205                                                                   + "&chd=t:"+values
206                                                                   + "&chco=78A2FF"
207                                                                   + "&chls=1,1,0"
208                                                                         + "&chm=o,990000,0,81,4"+(fill?"|B,EBEAFF,0,0,0":"")
209                                                                   + "&chxt=r"
210                                                                   + "&chxs=1,990000,1,0,_"
211                                                                   + "&chxl=0:|"+val
212                                                                         + "&chxp=0,"+(val*100/max)
213                                                                         +(bg?"&chf=bg,s,f5f5f5":"")
214                                                                   + "\">";
215                                                                   if(withLink) {
216                                                                         insert.innerHTML += "<a href="+url+">"+imgtxt+"</a>";
217                                                                   } else  {
218                                                                                                                                                 insert.innerHTML +=imgtxt;
219                                                                   }
220                                                                 }
221                                                                 }
222                                                                 if(insert.innerHTML == '') {
223                                                                   insert.innerHTML = '0';
224                                                                 }
226                                                           }
227                                                         })
229           },
231           init: function() {
232                  if(document.title == "Flickr: Your Photostream"){
233                   var self = this;
234                   foreach("//p[@class='Activity']",function(el) {
235                                         var ael = $x1("a",el);
236                                         self.makeSpark(ael.href+"/stats",el,true);
237                                   });
238                  } else if(document.title.indexOf('Stats for your account')>=0) {
239                    var self = this;
240                    var cnt = 0;
241                    foreach("//div[@class='yesterday']/table//span[contains(@class,'photo_container')]/a",function(el) {
243                                          var ael = $x1("td[@class='views']",el.parentNode.parentNode.parentNode);
244                                                  ael.innerHTML = '';
245                                          self.makeSpark(el.href,ael,false,false,(++cnt%2>0));
246                                    } );
247                  } else if(document.title.indexOf('All your photos and videos') >= 0) {
248                    var self = this;
249                    var cnt = 0;
250                    foreach("//table//span[contains(@class,'photo_container')]/a",function(el) {
252                                          var ael = $x1("td[@class='yesterday']",el.parentNode.parentNode.parentNode);
253                                                  ael.innerHTML = '';
254                                          self.makeSpark(el.href+"/stats",ael,false,false,(++cnt%2>0));
255                                    } );
256                  } else {
257                    var statIT = $x1("//td[@class='RHS']/ul/li[3]/a");
258                    if(!statIT || statIT.innerHTML != "Photo stats") {
259                          statIT = $x1("//td[@class='RHS']/ul/li[2]/a");
260                    }
261                    if(statIT && statIT.innerHTML == "Photo stats") {
262                          var url = statIT.href;
263                          this.makeSpark(url,statIT);
264                    } else {
265                          var stats = $x1("//a[@title='Your Stats']");
266                          if(stats) {
267                            stats.innerHTML = '';
268                            this.makeSpark(stats.href,stats,false,true);
269                            var self = this;
270                            foreach("//ul[contains(@class,'tt-stats-list')]/li/p/a",function(el) {
271                                                  var ael = $x1("div[@class='tt-stats-count']",el.parentNode.parentNode);
272                                                  ael.innerHTML = '';
273                                                  self.makeSpark(el.href,ael);
274                                            });
276                          }
277                    }
278                  }
279                 }
281         }
282         //======================================================================
283         // launch
284         try {
285                 window.addEventListener("load", function () {
286                                                                         try {
288                                                                                 // update automatically (http://userscripts.org/scripts/show/2296)
289                                                                                 win.UserScriptUpdates.requestAutomaticUpdates(SCRIPT);
290                                                                         } catch (ex) {}
292                                                                         var flickrgp = new flickrmoresparkles();
293                 }, false);
294         } catch (ex) {}
295 })();