source: trunk/www.guidonia.net/wp/wp-content/plugins/sociable/wists.js@ 44

Last change on this file since 44 was 44, checked in by luciano, 14 years ago
File size: 8.4 KB
Line 
1var Wistlet = {
2 init : function () {
3 var o = document.getElementsByTagName('body').item(0);
4
5 //var text = (typeof(document.selection == 'undefined') ) ? getSelection() : document.selection.createRange().text;
6
7 function getSel() {
8 var txt = '';
9 if (window.getSelection) {
10 txt = window.getSelection();
11 } else if (document.getSelection) {
12 txt = document.getSelection();
13 } else if (document.selection) {
14 txt = document.selection.createRange().text;
15 } else {
16 return;
17 }
18 return txt;
19 }
20
21
22 var s = '' +
23 '<form name="f" id="f" action="http://www.wists.com/s.php" method="get">'+
24 '<input name="c" id="c" type="hidden" value="null" />'+
25 '<input name="r" id="r" type="hidden" value="'+location+'" />'+
26 '<input name="u" id="u" type="hidden" value="" />'+
27 '<input name="title" id="title" type="hidden" value="'+document.title +'" />'+
28 '<input name="m" type="hidden" value="'+getSel()+'" />'+
29 '</form>'+
30 '<div>'+
31 '<a href="http://wists.com/"><img src="http://wists.com/mainimages/logo_top_left.gif" border=0></a><br />'+
32 '<p>Click on the most appropriate image below to create a thumbnail image for your bookmark:</a></p>'+
33 '<table width=800><tr width=800><td width=800>\n';
34
35 var x = this.scrape_images();
36 if (x == 0)
37 {
38 location = 'http://www.wists.com/s.php?c=&r='+location+'&title='+document.title;
39 return true;
40 }
41 s += x +
42
43 '</td></tr></table>\n'+
44 '<p>Don\'t like any of these images? <a href="http://www.wists.com/s.php?c=&r='+location+'&title='+document.title +'">Create a thumbnail screenshot</a> instead!</p>'+
45 '</div>';
46
47 o.innerHTML = s;
48 this.strip_document();
49 this.style_document();
50 return true;
51 },
52 make_thumbnail : function (r) {
53 var s = '' +
54 '<table style="float:left;"><tr><td width=120 height=90 valign=middle align=center><a href="http://www.wists.com/" onclick="return Wistlet.submit_form(\''+r+'\');">'+
55 '<img src="'+r+'" alt="" onload="resizeImage(this);" />'+
56 '</a></td></tr></table>\n';
57 return s;
58 },
59 submit_form : function (r) {
60 var f = document.getElementById('f'); if (!f) return false;
61 var u = document.getElementById('u'); if (!u) return false;
62 u.setAttribute("value", r);
63 f.submit();
64 return false;
65 },
66 get_background : function (o) {
67 var s = '';
68 if (window.getComputedStyle) s = window.getComputedStyle(o,null).getPropertyValue("background-image");
69 if (o.currentStyle) s = o.currentStyle.backgroundImage;
70 if (s == "none") s = "";
71 return s;
72 },
73 scrape_images : function () {
74 var a = document.getElementsByTagName('*');
75 var s = '';
76 var n = 0;
77 var l = [];
78 for (var i=0; i < a.length; i++)
79 {
80 var o = a[i];
81 if (o.tagName == "IMG" && o.src != "")
82 {
83 n++;
84 if (!l[o.src])
85 {
86 l[o.src] = "1";
87 s += this.make_thumbnail(o.src);
88 }
89 }
90 else
91 {
92 var x = /url\(["']?(.+[^'"])["']?\)/gi.exec(this.get_background(o));
93 if (x && x != "" && x.length > 1)
94 {
95 n++;
96 if (!l[x[1]])
97 {
98 l[x[1]] = "1";
99 s += this.make_thumbnail(x[1]);
100 }
101 }
102 }
103 }
104 if (n > 0)
105 {
106 return s
107 }
108 else {
109 return n;
110 }
111 },
112 strip_document : function () {
113 var a = document.getElementsByTagName('*');
114 for (var i=0; i < a.length; i++)
115 {
116 var o = a[i];
117 if (o.tagName == "LINK" || o.tagName == "STYLE")
118 {
119 this.destroy_element(o);
120 }
121 else if (o.tagName == "IMG")
122 {
123 if (o.offsetWidth <= 10 || o.offsetHeight <= 10)
124 {
125 // this.destroy_element(o.parentNode);
126 }
127 }
128 }
129 return true;
130 },
131 style_document : function () {
132 var x = document.getElementsByTagName('head').item(0);
133 var o = document.createElement('link');
134 if (typeof o != 'object') o = document.standardCreateElement('link');
135 o.setAttribute('href','http://www.wists.com/wistlet.css?x=' + Math.floor(Math.random() * 9999));
136 o.setAttribute('rel','stylesheet');
137 o.setAttribute('type','text/css');
138 x.appendChild(o);
139 return true;
140 },
141 destroy_element : function (o) {
142 while (o.childNodes.length > 0)
143 {
144 o.removeChild(o.childNodes[0]);
145 }
146 o.parentNode.removeChild(o);
147 }
148}
149
150function resizeImage(img) {
151
152 var img_width = img.offsetWidth;
153 var img_height = img.offsetHeight;
154 var img_aspect_ratio = Math.round((img_width / img_height) * 100) / 100;
155
156 var max_width = 120;
157 var max_height = 90;
158 var max_aspect_ratio = Math.round((max_width / max_height) * 100) / 100;
159
160// alert("orig image size is " + img_width + "x" + img_height + "\n" + "aspect ratio is " + img_aspect_ratio + "\n\n" + "max image size is " + max_width + "x" + max_height + "\n" + "max aspect ratio is " + max_aspect_ratio);
161
162 var new_img_width = 0;
163 var new_img_height = 0;
164 var new_aspect_ratio = 0;
165
166 // if no resize needed
167 if (img_width < 120 && img_height < 90) {
168 new_img_width = img_width;
169 new_img_height = img_height;
170
171 // if wider
172 } else if (img_aspect_ratio > max_aspect_ratio) {
173 new_img_width = max_width;
174 new_img_height = Math.round(new_img_width / img_aspect_ratio);
175
176 // if taller
177 } else if (img_aspect_ratio < max_aspect_ratio) {
178 new_img_height = max_height;
179 new_img_width = Math.round(new_img_height * img_aspect_ratio);
180
181 // equal
182 } else {
183 new_img_width = max_width;
184 new_img_height = max_height;
185 }
186
187 img.style.width = new_img_width + "px";
188 img.style.height = new_img_height + "px";
189 new_aspect_ratio = Math.round((new_img_width / new_img_height) * 100) / 100;
190
191// alert("new image size is " + new_img_width + "x" + new_img_height + "\n" + "new aspect ratio is " + new_aspect_ratio);
192}
193
194
195var sociable_old_onload = window.onload;
196window.onload = function () {
197 imgs = document.getElementsByTagName("img");
198 for(var i = 0; i < imgs.length; i++) {
199 var img = imgs[i];
200 if (img.className == 'sociable-hovers sociable_wists') {
201 img.onclick = function() { Wistlet.init(); return false; }
202 }
203 }
204 if (sociable_old_onload) sociable_old_onload();
205};
Note: See TracBrowser for help on using the repository browser.