- Fixed a bunch of bugs for existing paginations
[FlickrHacks.git] / _greasemonkey_ / flickrautotag.user.js
blob4b00cfa8c33fa29468269d753dcd9bc669325dc4
1 // Flickr Auto Tag
2 // v0.3
3 // 2007-26-06
4 // Copyright (c) 2007, Pierre Andrews.
5 // Released under the GPL license
6 // http://www.gnu.org/copyleft/gpl.html
7 //
8 // ==UserScript==
9 // @name        Flickr Auto Tag
10 // @namespace   http://6v8.gamboni.org/Flickr-Auto-Tag.html
11 // @description Save and reuse quickly the tags you often use.
12 // @version        0.3
13 // @date           2007-06-26
14 // @creator        Pierre Andrews (mortimer.pa@free.fr)
15 // @include http://*flickr.com/photos/*
16 // @exclude http://*flickr.com/photos/organize*
17 // ==/UserScript==
19 (function () {
21         var win = (unsafeWindow || window.wrappedJSObject || window);
23         //update information
24         var SCRIPT = {
25                 name: "Flickr Auto Tag",
26                 namespace: "http://6v8.gamboni.org/Flickr-Auto-Tag.html",
27                 description: "Save and reuse quickly the tags you often use.",
28                 source: "http://6v8.gamboni.org/Flickr-Auto-Tag.html",                  // script homepage/description URL
29                 identifier: "http://6v8.gamboni.org/IMG/js/flickrautotag.user.js",
30                 version: "0.3",                                                         // version
31                 date: (new Date(2007, 6, 26))           // update date
32                 .valueOf()
33         };
35         //======================================================================
36         //to do the closure and get the right this.
37         //adapted from http://persistent.info/greasemonkey/gmail.user.js
39         function getObjectMethodClosure0(object, method,args) {
40                 return function() {
41                         return object[method](args); 
42                 }
43         }
45                         /***********************************************************************
46          * Flickr Localisation
47          **********************************************************************/
49         function $x1(xpath) {
50                 return document.evaluate(
51                                                                  xpath,
52                                                                  document,
53                                                                  null,
54                                                                  XPathResult.FIRST_ORDERED_NODE_TYPE, null
55                                                                  ).singleNodeValue;
56         }
58         var FlickrLocaliser = function(locals) {
59                 this.init(locals);
60         }
61         FlickrLocaliser.prototype = {
62                 selectedLang: undefined,
63                 localisations: undefined,
64                 getLanguage: function() {
65                         if(!this.selectedLang) {
66                                 var langA = $x1("//p[@class='LanguageSelector']//a[contains(@class,'selected')]");
67                                 if(langA) {
68                                         var matches = /\/change_language.gne\?lang=([^&]+)&.*/.exec(langA.href);
69                                         if(matches && matches[1]) {
70                                                 this.selectedLang = matches[1];
71                                                 return this.selectedLang;
72                                         }
73                                 }
74                                 return false;
75                         } else return this.selectedLang;
76                 },
78                 init: function(locals) {
79                         this.localisations = locals;
80                 },
82                 localise: function(string, params) {
83                         if(this.localisations && this.getLanguage()) {
84                                 var currentLang = this.localisations[this.selectedLang];
85                                 if(!currentLang) currentLang = this.localisations[this.localisations.defaultLang];
86                                 var local = currentLang[string];
87                                 if(!local) {
88                                         local = this.localisations[this.localisations.defaultLang][string];
89                                 } 
90                                 if(!local) return string;
91                                 for(arg in params) {
92                                         var rep = new RegExp('@'+arg+'@','g');
93                                         local = local.replace(rep,params[arg]);
94                                 }
95                                 local =local.replace(/@[^@]+@/g,'');
96                                 return local;
97                         } else return undefined;
98                 }
100         }
102         /*****************************Flickr Localisation**********************/
105         /*function M8_log() {
106                 if(unsafeWindow.console)
107                         unsafeWindow.console.log(arguments);
108                 else
109                         GM_log(arguments);
110                         }*/
112         var localiser = new FlickrLocaliser({
113                         'en-us' : {
114                                 'add' : 'add',
115                                 'save' : 'save',
116                                 'clear' : "FlickrAutoTag: Clear"
117                         },
118                         'fr-fr' : {
119                                 'add' : 'ajouter',
120                                 'save' : 'sauver',
121                                 'clear' : "FlickrAutoTag: Vider les tags"
122                         },
123                         'es-us' : {
124                                 'add' : 'agregar',
125                                 'save' : 'guardar',
126                                 'clear' : "FlickrAutoTag: Borrar las etiquetas"
127                         },
128                         'it-it' : {
129                                 'add' : 'aggiungi',
130                                 'save' : 'salva',
131                                 'clear' : "FlickrAutoTag: Cancella i tag"
132                         },
133                         'pt-br' : {
134                                 'add' : 'adicionar',
135                                 'save' : 'salvar',
136                                 'clear' : "FlickrAutoTag: Limpar suas tags"
137                         },
138                         'zh-hk': {
139                                 // A
140                                 'add' : '增加',
141                                 // S
142                                 'save' : '儲存'
143                         },
144                         'de-de': {
145                                 // A
146                                 'add' : 'hinzfügen',
147                                 // S
148                                 'save' : 'speichern'
149                         },
150                         'ko-kr': {
151                                 // A
152                                 'add' : '추가',
153                                 // S
154                                 'save' : '저장'
155                         },
156                         defaultLang: 'en-us'
157                 });
159         win.FlickrAutoTag = function() {
160                 this.init();
161         };
163         win.FlickrAutoTag.prototype = {
164                 init: function() {      
165                         var existing_tags;
166                         
167                         var matches = /\/photos\/[^\/]+\/([0-9]+)/.exec(document.location.pathname);
168                         if(matches) {
169                                 //use the tags flickr already retrieved for use, thanks
170                                 existing_tags = unsafeWindow.global_photos[matches[1]].tags_rawA;
171                         }
172                         var tags = GM_getValue('tags');
173                         if(tags) {
174                                 var tagit = document.getElementById('tagadderlink');
175                                 if(tagit) {
176                                         stags = tags.split('#@#');
177                                         html = '';
178                                         for each(tag in stags) {
179                                                 var show = true;
180                                                 if(existing_tags) {
181                                                         //This will not resist to "
182                                                         var split = tag.split(' ');
183                                                         var cnt = 0;
184                                                         for each(t in split) {
185                                                                 if(existing_tags.indexOf(t) > 0) cnt++;
186                                                         }
187                                                         if(cnt == split.length) show = false;
188                                                 }
189                                                 if(show) {
190                                                         tag = tag.replace(/\"/g,'"');
191                                                         html += '<option>'+tag+'</option>';
192                                                 }
193                                         }
194                                         if(html) {
195                                                 var span = tagit.appendChild(document.createElement('span'));
196                                                 span.style.display = 'block';
197                                                 span.style.fontSize = '80%';
198                                                 var select = span.appendChild(document.createElement('select'));
199                                                 select.innerHTML = html;
200                                                 var button = span.appendChild(document.createElement('button'));
201                                                 button.innerHTML = localiser.localise('add');
202                                                 button.className = 'SmallButt';
203                                                 button.addEventListener('click',getObjectMethodClosure0(this,'addTags',select),true);
204                                         }
205                                 }
206                         }
208                         
209                         var adder = document.getElementById('tagadderform');
210                         if(adder) {
211                                 var label = adder.parentNode.insertBefore(document.createElement('label'),adder.nextSibling);
212                                 label.innerHTML = localiser.localise('save');
213                                 label.style.fontSize = 'small';
214                                 label.style.color = 'grey';
215                                 label.htmlFor = 'savetag';
216                                 var save = adder.parentNode.insertBefore(document.createElement('input'),adder.nextSibling);
217                                 save.type = 'checkbox';
218                                 save.id = 'savetag';
219                                 adder.elements[2].addEventListener('click',function() {
220                                         if(save.checked) {
221                                                 if(!tags || (tags.split('#@#').indexOf(adder.elements[1].value) < 0)) {
222                                                         if(tags) tags += '#@#'; //this will not resist tags containing #@#
223                                                         else tags = '';
224                                                         tags += adder.elements[1].value;
225                                                         GM_setValue('tags',tags);
226                                                 }
227                                         }
228                                 },true);
229                         }
230                 },
231                 
232                 addTags: function(select) {
233                         
234                         var adder = document.getElementById('tagadderform');
235                         
236                         var matches = /\/photos\/[^\/]+\/([0-9]+)/.exec(document.location.pathname);
237                         if(matches) {
238                                 //use flickr function to do the work
239                                 unsafeWindow.tagrs_addTag(matches[1],select.value);
240                         }
241                 }
242         };
245         //======================================================================
246                 
247         //======================================================================
248         // launch
249         try {
250                 window.addEventListener("load", function () {
251                                                                         try {
252                                                                                 
253                                                                                 // update automatically (http://userscripts.org/scripts/show/2296)
254                                                                                 win.UserScriptUpdates.requestAutomaticUpdates(SCRIPT);
255                                                                         } catch (ex) {} 
256                                                                         
257                                                                         var flickrgp = new win.FlickrAutoTag();
258                 }, false);
259                 //a menu item to clear the saved tags.
260                 GM_registerMenuCommand( localiser.localise('clear'), function() {GM_setValue('tags','');} );
261         } catch (ex) {}
263 })();