- Fixed a bunch of bugs for existing paginations
[FlickrHacks.git] / _greasemonkey_ / flickrexifinfo.user.js
blobcd6d1a817e510ea34dd3ce66e3ca3f4220816df1
1 // ==UserScript==
2 // @name        Flickr Exif Info
3 // @namespace   http://6v8.gamboni.org/
4 // @description Select which exif info you want to see on the photo page
5 // @version        0.3
6 // @identifier  http://6v8.gamboni.org/IMG/js/flickrexifinfo.user.user.js
7 // @date           2006-08-28
8 // @creator        Pierre Andrews (mortimer.pa@free.fr)
9 // @include http://*flickr.com/photos/*/*
10 // @include http://*flickr.com/photo_exif.gne?id=*
11 // @exclude http://*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) 2006 Pierre Andrews
25 // 
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.
30 // 
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.
35 // 
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 () {
46         
47         //This is an array to help in different conversion of the raw data.
48         // you can map a exif value name as displayed by flickr
49         // with: 
50         // - it's real name in the EXIF info
51         // - it's unit
52         // - the name of the EXIF info defining the unit
53         // - a replacement regexp for when you add this info in the tags (%u in in the replacement will be replaced by the unit if any)
54         // see the existing values to understand more.
56         var MAPPING = {
57                 'Image Width': {map:'Pixel X-Dimension',direct_unit:'pixel'},
58                 'Image Height': {map:'Pixel Y-Dimension',direct_unit:'pixel'},
59                 'Y-Resolution': {indirect_unit:'Resolution Unit'},
60                 'X-Resolution': {indirect_unit:'Resolution Unit'},
61                 'ISO Speed' : {addtag:{regexp:/([0-9]+)/,replacement:'iso:$1'}},
62                 'Focal Length' : {addtag:{regexp:/([0-9]+) mm/,replacement:'$1mm'}},
63                 'Exposure' : {addtag:{regexp:/.*\(([\/0-9]+)\)/,replacement:'$1s'}},
64         };
66         
67         //conversion for the units, I have no idea what the 0 and 1 stands for, but 2 is for dpi apparently :D
68         var UNITS = new Array('?','?','dpi');
71         //update information
72         var SCRIPT = {
73                 name: "Flickr Exif Info",
74                 namespace: "http://6v8.gamboni.org/",
75                 description: "Select which exif info you want to see on the photo page",
76                 identifier: "http://6v8.gamboni.org/IMG/js/flickrexifinfo.user.js",
77                 version: "0.3",                                                         // version
78                 date: (new Date("2006-08-28"))          // update date
79                 .valueOf()
80         };
81         
82         function getObjectMethodClosure11(object, method,arg1) {
83                 return function(arg) {
84                         return object[method](arg,arg1); 
85                 }
86         }       
88         function M8_log() {
89                 if(unsafeWindow.console)
90                         unsafeWindow.console.log(arguments);
91                 else
92                         GM_log(arguments);
93         }
95         var flickrexifinfo = function() {this.init();}
97         flickrexifinfo.prototype = {
98                 config: GM_getValue('ExifInfoConfig'),
100                 init: function() {
101                         if(document.location.pathname.indexOf("photo_exif.gne") >= 0) {
102                                 this.showConfigurationBoxes();
103                         } else {
104                                 this.showMoreExif();
105                         }
106                 },
107                 
108                 showConfigurationBoxes: function() {
109                         var infos = document.evaluate(
110                                                                                            "//table[@id='Inbox']/tbody/tr/td[1]/b",
111                                                                                            document, null, XPathResult.UNORDERED_NODE_SNAPSHOT_TYPE, null); 
112                                 for(var i = 0; i < infos.snapshotLength; i++) {  
113                                                 var exif = infos.snapshotItem(i);
114                                                 var name = exif.innerHTML.replace(/:$/,'');
115                                                 var check = document.createElement('input');
116                                                 check.type = 'checkbox';
117                                                 check.addEventListener('change',getObjectMethodClosure11(this,'addToConf',name),false);
118                                                 if(i == 0) {
119                                                         check.checked = true;
120                                                         check.disabled = true;
121                                                 } else if(this.config && (this.config.indexOf(','+name+',') >= 0)) {
122                                                         check.checked = true;
123                                                 }
124                                                 var td = document.createElement('td');
125                                                 td.style.width = '13px';
126                                                 td.appendChild(check);
127                                                 exif.parentNode.parentNode.insertBefore(td,exif.parentNode);
128                                 }
129                 },
131                 addToConf: function(event, name) {
132                         var newConfig = this.config;
133                         if(!newConfig) newConfig = ',';
134                         if(event.target.checked) {
135                                 newConfig += name+',';
136                         } else {
137                                 newConfig = newConfig.replace(','+name+',',',');
138                         }       
139                         this.config = newConfig;
140                         M8_log(this.config);
141                         GM_setValue('ExifInfoConfig',this.config);
142                 },
144                 showMoreExif: function() {
145                         var moreLot = document.evaluate(
146                                                                                  "//td[@class='RHS']/ul//li/a[@class='Plain']",
147                                                                                  document, null, XPathResult.UNORDERED_NODE_SNAPSHOT_TYPE, null
148                                                                                  );
149                         for(var i = 0; i < moreLot.snapshotLength; i++) {  
150                                 var more = moreLot.snapshotItem(i);
151                                 if(more.innerHTML == "More properties") {
152                                         var photoid = more.href.replace(/http:\/\/(www.)?flickr.com\/photo_exif.gne\?id=([0-9]+)/,'$2');
153                                         if(this.config.length > 1) {
154                                                 var ul = document.createElement('ul');
155                                                 
156                                                 var self = this;
157                                                 var listener = {
158                                                         flickr_photos_getExif_onLoad: function(success, responseXML, responseText, params){
159                                                                 var rsp = responseText.replace(/<\?xml.*\?>/,'');
160                                                                 rsp = new XML(rsp);
161                                                                 var finds = self.config.split(',');
162                                                                 for(var i=0;i<finds.length;i++) {
163                                                                         if(finds[i]) {
164                                                                                 var mapping = MAPPING[finds[i]];
165                                                                                 var map = null;
166                                                                                 if(mapping)
167                                                                                 map = mapping.map;
168                                                                                 var ex = '';
169                                                                                 if(!map) {
170                                                                                         ex = rsp..exif.(@label == finds[i]);
171                                                                                 }  else {
172                                                                                         ex = rsp..exif.(@label == map);
173                                                                                 }
174                                                                                 if(new String(ex.clean).length > 0)
175                                                                                         ex = ex.clean;
176                                                                                 else 
177                                                                                         ex = ex.raw;
178                                                                                 if(new String(ex).length > 0) {
179                                                                                         var unit = '';
180                                                                                         if(mapping) {
181                                                                                                 unit =  mapping.direct_unit;
182                                                                                                 if(!unit && mapping.indirect_unit) {
183                                                                                                         unit = UNITS[parseInt(rsp..exif.(@label == mapping.indirect_unit).raw)];
184                                                                                                 }
185                                                                                         }
186                                                                                         var li = ul.appendChild(document.createElement('li'));
187                                                                                         li.className = 'Stats';
188                                                                                         li.innerHTML = '<b>'+finds[i]+':</b> '+ex;      
189                                                                                         if(unit && new String(unit).length > 0)
190                                                                                                 li.innerHTML += ' '+unit;
191                                                                                         if(document.getElementById('tagadderlink')) {
192                                                                                                 var addtag = li.appendChild(document.createElement('a'));
193                                                                                                 addtag.innerHTML = ' [+]';
194                                                                                                 addtag.title ="add this as a tag";
195                                                                                                 addtag.href="javascript:;";
196                                                                                                 addtag.setAttribute('style','text-decoration:none;color: #C9C9C9;');
197                                                                                                 addtag.addEventListener('click',getObjectMethodClosure11(self,'addtag',new Array(photoid,finds[i],ex,unit)),false);
198                                                                                         }
199                                                                                 }
200                                                                         }
201                                                                 }
202                                                                 more.parentNode.appendChild(ul);
203                                                                 var li = document.createElement('li');
204                                                                 li.className = 'Stats';
205                                                                 li.appendChild(more);
206                                                                 ul.appendChild(li);
207                                                         }
208                                                 };
209                                                 
210                                                 unsafeWindow.F.API.callMethod('flickr.photos.getExif', {
211                                                                 photo_id:photoid                                   
212                                                                         }, listener);
213                                                 
214                                         }
215                                         break;
216                                 }
217                         }
218                 },
220                 addtag: function(evt,args) {
221                         var id =args[0];
222                         var title = args[1];
223                         var value = new String(args[2]);
224                         var unit = args[3];
225                         var mapping = MAPPING[title];
226                         if(mapping && mapping.addtag) {
227                                 value = value.replace(mapping.addtag.regexp,mapping.addtag.replacement);
228                                 if(unit) value.replace(/%u/g,unit);
229                         }
230                         unsafeWindow.tagrs_addTag(id,value);
231                 }
232         }
233         //======================================================================
234         // launch
235         try {
236                 window.addEventListener("load", function () {
237                                                                         try {
238                                                                                 
239                                                                                 // update automatically (http://userscripts.org/scripts/show/2296)
240                                                                                 win.userScriptUpdates.requestAutomaticUpdates(SCRIPT);
241                                                                         } catch (ex) {} 
242                                                                         
243                                                                         var flickrgp = new flickrexifinfo();
244                 }, false);
245         } catch (ex) {}
246 })();