- Fixed a bunch of bugs for existing paginations
[FlickrHacks.git] / _greasemonkey_ / replacesmiley.user.js
blob856ad38be28c96dfb4d6c439248fa73ea4e90100
1 // ==UserScript==
2 // @name        Replace Smiley
3 // @namespace   http://6v8.gamboni.org/
4 // @description Replace replace img smiley by a text
5 // @version        0.1
6 // @identifier  http://6v8.gamboni.org/IMG/js/replacesmiley.user.js
7 // @date           2007-03-07
8 // @creator        Pierre Andrews (mortimer.pa@free.fr)
9 // @include http://*flickr.com*
10 // 
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
42 var TOTEXT = false;
44 (function () {
46         //update information
47         var SCRIPT = {
48                 name: "Replace Smiley",
49                 namespace: "http://6v8.gamboni.org/",
50                 description: "Replace smileys by an image, or replace img smiley by a text",
51                 identifier: "http://6v8.gamboni.org/IMG/js/replacesmiley.user.js",
52                 version: "0.1",                                                         // version
53                 date: (new Date("2007-03-07"))          // update date
54                 .valueOf()
55         };
56         
57         
58         function M8_log() {
59                 if(unsafeWindow.console)
60                         unsafeWindow.console.log(arguments);
61                 else
62                         GM_log(arguments);
63         }
65         /*
66           Xpath trickery, from:
67           http://ecmanaut.blogspot.com/2006/07/expressive-user-scripts-with-xpath-and.html
68          */
69         function $x( xpath, root )
70                 {
71                         var doc = root ? root.evaluate?root:root.ownerDocument : document;
72                         var got = doc.evaluate( xpath, root||doc, null, 0, null ), next;
73                         var result = [];
74                         while( next = got.iterateNext() )
75                                 result.push( next );
76                         return result;
77                 }
80         function $x1(xpath) {
81                 return document.evaluate(
82                                                                  xpath,
83                                                                  document,
84                                                                  null,
85                                                                  XPathResult.FIRST_ORDERED_NODE_TYPE, null
86                                                                  ).singleNodeValue;
87         }
89         function foreach( xpath, cb, root )
90         {
91                 var nodes = $x( xpath, root ), e = 0;
92                 for( var i=0; i<nodes.length; i++ )
93                         e += cb( nodes[i], i ) || 0;
94                 return e;
95         }
99         function getObjectMethodClosure(object, method) {
100                 return function(arg) {
101                         return object[method](arg); 
102                 }
103         }
106         var replacesmiley = function() {this.init();}
107         var smileys = {
108                 '1': {regexp:":-?\\)",replace:':)',Name:"happy",URL:"http://us.i1.yimg.com/us.yimg.com/i/mesg/emoticons7/1.gif"},
109                 '2': {replace:':(',Name:"sad",URL:"http://us.i1.yimg.com/us.yimg.com/i/mesg/emoticons7/2.gif"},
110                 '3': {replace:';)',Name:"winking",URL:"http://us.i1.yimg.com/us.yimg.com/i/mesg/emoticons7/3.gif"},
111                 '4': {replace:':D',Name:"big grin",URL:"http://us.i1.yimg.com/us.yimg.com/i/mesg/emoticons7/4.gif"},
112                 '5': {replace:';;)',Name:"batting eyelashes",URL:"http://us.i1.yimg.com/us.yimg.com/i/mesg/emoticons7/5.gif"},
113                 '6': {replace:'>:D<',Name:"big hug",URL:"http://us.i1.yimg.com/us.yimg.com/i/mesg/emoticons7/6.gif"},
114                 '7': {replace:':-/',Name:"confused",URL:"http://us.i1.yimg.com/us.yimg.com/i/mesg/emoticons7/7.gif"},
115                 '8': {replace:':x',Name:"love struck",URL:"http://us.i1.yimg.com/us.yimg.com/i/mesg/emoticons7/8.gif"},
116                 '9': {replace:':">',Name:"blushing",URL:"http://us.i1.yimg.com/us.yimg.com/i/mesg/emoticons7/9.gif"},
117                 '10': {replace:':P',Name:"tongue",URL:"http://us.i1.yimg.com/us.yimg.com/i/mesg/emoticons7/10.gif"},
118                 '11': {replace:':-*',Name:"kiss",URL:"http://us.i1.yimg.com/us.yimg.com/i/mesg/emoticons7/11.gif"},
119                 '12': {replace:'=((',Name:"broken heart",URL:"http://us.i1.yimg.com/us.yimg.com/i/mesg/emoticons7/12.gif"},
120                 '13': {replace:':-O',Name:"surprise",URL:"http://us.i1.yimg.com/us.yimg.com/i/mesg/emoticons7/13.gif"},
121                 '14': {replace:'X(',Name:"angry",URL:"http://us.i1.yimg.com/us.yimg.com/i/mesg/emoticons7/14.gif"},
122                 '15': {replace:':>',Name:"smug",URL:"http://us.i1.yimg.com/us.yimg.com/i/mesg/emoticons7/15.gif"},
123                 '16': {replace:'B-)',Name:"cool",URL:"http://us.i1.yimg.com/us.yimg.com/i/mesg/emoticons7/16.gif"},
124                 '17': {replace:':-S',Name:"worried",URL:"http://us.i1.yimg.com/us.yimg.com/i/mesg/emoticons7/17.gif"},
125                 '18': {replace:'#:-S',Name:"whew!",URL:"http://us.i1.yimg.com/us.yimg.com/i/mesg/emoticons7/18.gif"},
126                 '19': {replace:'>:)',Name:"devil",URL:"http://us.i1.yimg.com/us.yimg.com/i/mesg/emoticons7/19.gif"},
127                 '20': {replace:':((',Name:"crying",URL:"http://us.i1.yimg.com/us.yimg.com/i/mesg/emoticons7/20.gif"},
128                 '21': {replace:':))',Name:"laughing",URL:"http://us.i1.yimg.com/us.yimg.com/i/mesg/emoticons7/21.gif"},
129                 '22': {replace:':|',Name:"straight face",URL:"http://us.i1.yimg.com/us.yimg.com/i/mesg/emoticons7/22.gif"},
130                 '23': {replace:'/:)',Name:"raised eyebrow",URL:"http://us.i1.yimg.com/us.yimg.com/i/mesg/emoticons7/23.gif"},
131                 '24': {replace:'=))',Name:"rolling on the floor",URL:"http://us.i1.yimg.com/us.yimg.com/i/mesg/emoticons7/24.gif"},
132                 '25': {replace:'O:)',Name:"angel",URL:"http://us.i1.yimg.com/us.yimg.com/i/mesg/emoticons7/25.gif"},
133                 '26': {replace:':-B',Name:"nerd",URL:"http://us.i1.yimg.com/us.yimg.com/i/mesg/emoticons7/26.gif"},
134                 '27': {replace:'=;',Name:"talk to the hand",URL:"http://us.i1.yimg.com/us.yimg.com/i/mesg/emoticons7/27.gif"},
135                 '101': {replace:':-c',Name:"call me - New!",URL:"http://us.i1.yimg.com/us.yimg.com/i/mesg/emoticons7/101.gif"},
136                 '100': {replace:':)]',Name:"on the phone - New!",URL:"http://us.i1.yimg.com/us.yimg.com/i/mesg/emoticons7/100.gif"},
137                 '102': {replace:'~X(',Name:"at wits' end - New!",URL:"http://us.i1.yimg.com/us.yimg.com/i/mesg/emoticons7/102.gif"},
138                 '103': {replace:':-h',Name:"wave - New!",URL:"http://us.i1.yimg.com/us.yimg.com/i/mesg/emoticons7/103.gif"},
139                 '104': {replace:':-t',Name:"time out - New!",URL:"http://us.i1.yimg.com/us.yimg.com/i/mesg/emoticons7/104.gif"},
140                 '105': {replace:'8->',Name:"daydreaming - New!",URL:"http://us.i1.yimg.com/us.yimg.com/i/mesg/emoticons7/105.gif"},
141                 '28': {replace:'|-)',Name:"sleepy",URL:"http://us.i1.yimg.com/us.yimg.com/i/mesg/emoticons7/28.gif"},
142                 '29': {replace:'8-|',Name:"rolling eyes",URL:"http://us.i1.yimg.com/us.yimg.com/i/mesg/emoticons7/29.gif"},
143                 '30': {replace:'L-)',Name:"loser",URL:"http://us.i1.yimg.com/us.yimg.com/i/mesg/emoticons7/30.gif"},
144                 '31': {replace:':-&',Name:"sick",URL:"http://us.i1.yimg.com/us.yimg.com/i/mesg/emoticons7/31.gif"},
145                 '32': {replace:':-$',Name:"don't tell anyone",URL:"http://us.i1.yimg.com/us.yimg.com/i/mesg/emoticons7/32.gif"},
146                 '33': {replace:'[-(',Name:"not talking",URL:"http://us.i1.yimg.com/us.yimg.com/i/mesg/emoticons7/33.gif"},
147                 '34': {replace:':O)',Name:"clown",URL:"http://us.i1.yimg.com/us.yimg.com/i/mesg/emoticons7/34.gif"},
148                 '35': {replace:'8-}',Name:"silly",URL:"http://us.i1.yimg.com/us.yimg.com/i/mesg/emoticons7/35.gif"},
149                 '36': {replace:'<:-P',Name:"party",URL:"http://us.i1.yimg.com/us.yimg.com/i/mesg/emoticons7/36.gif"},
150                 '37': {replace:'(:|',Name:"yawn",URL:"http://us.i1.yimg.com/us.yimg.com/i/mesg/emoticons7/37.gif"},
151                 '38': {replace:'=P~',Name:"drooling",URL:"http://us.i1.yimg.com/us.yimg.com/i/mesg/emoticons7/38.gif"},
152                 '39': {replace:':-?',Name:"thinking",URL:"http://us.i1.yimg.com/us.yimg.com/i/mesg/emoticons7/39.gif"},
153                 '40': {replace:'#-o',Name:"d'oh",URL:"http://us.i1.yimg.com/us.yimg.com/i/mesg/emoticons7/40.gif"},
154                 '41': {replace:'=D>',Name:"applause",URL:"http://us.i1.yimg.com/us.yimg.com/i/mesg/emoticons7/41.gif"},
155                 '42': {replace:':-SS',Name:"nailbiting",URL:"http://us.i1.yimg.com/us.yimg.com/i/mesg/emoticons7/42.gif"},
156                 '43': {replace:'@-)',Name:"hypnotized",URL:"http://us.i1.yimg.com/us.yimg.com/i/mesg/emoticons7/43.gif"},
157                 '44': {replace:':^o',Name:"liar",URL:"http://us.i1.yimg.com/us.yimg.com/i/mesg/emoticons7/44.gif"},
158                 '45': {replace:':-w',Name:"waiting",URL:"http://us.i1.yimg.com/us.yimg.com/i/mesg/emoticons7/45.gif"},
159                 '46': {replace:':-<',Name:"sigh",URL:"http://us.i1.yimg.com/us.yimg.com/i/mesg/emoticons7/46.gif"},
160                 '47': {replace:'>:P',Name:"phbbbbt",URL:"http://us.i1.yimg.com/us.yimg.com/i/mesg/emoticons7/47.gif"},
161                 '48': {replace:'<):)',Name:"cowboy",URL:"http://us.i1.yimg.com/us.yimg.com/i/mesg/emoticons7/48.gif"}
162                 };
164         replacesmiley.prototype = {
166                 init: function() {
167                         if(TOTEXT) {
168                                 foreach("//img[contains(@src,'http://us.i1.yimg.com/us.yimg.com/i/mesg/emoticons7/')]",function(node, i) {
169                                         var smil = node.src.replace(/http:\/\/us.i1.yimg.com\/us.yimg.com\/i\/mesg\/emoticons7\/([0-9]+)\.gif/,"$1");
170                                         var replace = smileys[smil];
171                                         if(replace) {
172                                                 var txt = document.createElement('abbr');
173                                                 txt.innerHTML = replace.replace;
174                                                 txt.title = replace.Name;
175                                                 node.parentNode.insertBefore(txt,node);
176                                                 node.parentNode.removeChild(node);
177                                         }
178                                 }, document);
179                         } else {
180                                 //NOT WORKING, anyone for an implementation?
181                                 foreach("//text()",function(node, i) {
182                                         s = node.data;
183                                         for (key in smileys) {
184                                                 var p = smileys[key].regexp;//.replace(/([\(\)\[\]\\)\\*])/g,"\\$1");
185                                                 //M8_log(p);
186                                                 if(p) {
187                                         var reg = new RegExp(p);
188                                         M8_log(reg.toSource());
189                                         s = s.replace(reg, "$1"+smileys[key].URL+"$2");
190                                                 }
191                                         }
192                                         node.data = s;
193                                 },document);
194                         }
196                    }
197         }
198         //======================================================================
199         // launch
200         /*try {
201                 window.addEventListener("load", function () {
202                                                                         try {
203                                                                                 
204                                                                                 // update automatically (http://userscripts.org/scripts/show/2296)
205                                                                                 win.UserScriptUpdates.requestAutomaticUpdates(SCRIPT);
206                                                                         } catch (ex) {} 
207         */
208                                                                         var flickrgp = new replacesmiley();
209                                                                         /*      }, false);
210                                                                         } catch (ex) {}*/
211 })();