Subversion Repositories SmartDukaan

Rev

Go to most recent revision | Details | Last modification | View Log | RSS feed

Rev Author Line No. Line
13532 anikendra 1
function fInitializeReachliBookMarklet() { 
2
	var hosturl = "http://api.letushaggle.com/";
3
	function parseURL(url) {
4
		var a = document.createElement('a');
5
		a.href = url;
6
		return {
7
			source: url,
8
			protocol: a.protocol.replace(':',''),
9
			host: a.hostname,
10
			port: a.port,
11
			query: a.search,
12
			params: (function(){
13
			var ret = {},
14
				seg = a.search.replace(/^\?/,'').split('&'),
15
				len = seg.length, i = 0, s;
16
				for (;i<len;i++) {
17
				if (!seg[i]) { continue; }
18
				s = seg[i].split('=');
19
				ret[s[0]] = s[1];
20
			}
21
			return ret;
22
			})(),
23
			file: (a.pathname.match(/\/([^\/?#]+)$/i) || [,''])[1],
24
			hash: a.hash.replace('#',''),
25
			path: a.pathname.replace(/^([^\/])/,'/$1'),
26
			relative: (a.href.match(/tp:\/\/[^\/]+(.+)/) || [,''])[1],
27
			segments: a.pathname.replace(/^\//,'').split('/')
28
		};
29
	}
30
 
31
	function base64_encode (data) {
32
	  // http://kevin.vanzonneveld.net
33
	  // +   original by: Tyler Akins (http://rumkin.com)
34
	  // +   improved by: Bayron Guevara
35
	  // +   improved by: Thunder.m
36
	  // +   improved by: Kevin van Zonneveld (http://kevin.vanzonneveld.net)
37
	  // +   bugfixed by: Pellentesque Malesuada
38
	  // +   improved by: Kevin van Zonneveld (http://kevin.vanzonneveld.net)
39
	  // +   improved by: RafaƂ Kukawski (http://kukawski.pl)
40
	  // *     example 1: base64_encode('Kevin van Zonneveld');
41
	  // *     returns 1: 'S2V2aW4gdmFuIFpvbm5ldmVsZA=='
42
	  // mozilla has this native
43
	  // - but breaks in 2.0.0.12!
44
	  //if (typeof this.window['btoa'] == 'function') {
45
	  //    return btoa(data);
46
	  //}
47
	  var b64 = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/=";
48
	  var o1, o2, o3, h1, h2, h3, h4, bits, i = 0,
49
	    ac = 0,
50
	    enc = "",
51
	    tmp_arr = [];
52
 
53
	  if (!data) {
54
	    return data;
55
	  }
56
 
57
	  do { // pack three octets into four hexets
58
	    o1 = data.charCodeAt(i++);
59
	    o2 = data.charCodeAt(i++);
60
	    o3 = data.charCodeAt(i++);
61
 
62
	    bits = o1 << 16 | o2 << 8 | o3;
63
 
64
	    h1 = bits >> 18 & 0x3f;
65
	    h2 = bits >> 12 & 0x3f;
66
	    h3 = bits >> 6 & 0x3f;
67
	    h4 = bits & 0x3f;
68
 
69
	    // use hexets to index into b64, and append result to encoded string
70
	    tmp_arr[ac++] = b64.charAt(h1) + b64.charAt(h2) + b64.charAt(h3) + b64.charAt(h4);
71
	  } while (i < data.length);
72
 
73
	  enc = tmp_arr.join('');
74
 
75
	  var r = data.length % 3;
76
 
77
	  return (r ? enc.slice(0, r - 3) : enc) + '==='.slice(r || 3);
78
 
79
	}
80
 
81
	function ResetBookmarklet() {
82
		if (document.getElementById('copublishbookmarklet')) document.body.removeChild(document.getElementById('copublishbookmarklet')); 
83
		if (document.getElementById('cancelcopublishbookmarklet')) document.body.removeChild(document.getElementById('cancelcopublishbookmarklet'));
84
		if (document.getElementById('watch-player')) setCss(document.getElementById('watch-player'), {visibility: 'visible'});
85
	}
86
 
87
	function setCss(el, css) {
88
		for(var property in css) el.style[property] = css[property];
89
	}
90
 
91
	function getSelText() {
92
	    var txt = '';
93
	    if (window.getSelection) txt = window.getSelection();
94
	    else if (document.getSelection) txt = document.getSelection();
95
	    else if (document.selection) txt = document.selection.createRange().text;
96
		return txt;
97
	}
98
 
99
	function fThrowError(message) {
100
		if (document.getElementById('errorcopublishbookmarklet')) {
101
			setCss(document.getElementById('errorcopublishbookmarklet'), {display: 'block'});
102
			var timeout = setTimeout(function(){if (document.getElementById('closecopublishbookmarklet')) document.getElementById('closecopublishbookmarklet').click();}, 4000);
103
			document.getElementById('errorcopublishbookmarklet').innerHTML = message + '<br /><a href="#" id="closecopublishbookmarklet"><img src=hosturl+"img/close.png" alt="" style="margin-top: 10px;" /></a>';
104
			document.getElementById('closecopublishbookmarklet').onclick = function(){clearTimeout(timeout);ResetBookmarklet(); return false;}			
105
		}
106
	}
107
 
108
	ResetBookmarklet()
109
	window.scrollTo(0,0);
110
 
111
	var maxwidth=Math.max(document.body.scrollWidth,document.body.offsetWidth,document.documentElement.scrollWidth,document.documentElement.offsetWidth, 1000);
112
	var maxheight=Math.max(document.body.scrollHeight,document.body.offsetHeight,document.documentElement.scrollHeight,document.documentElement.offsetHeight);
113
 
114
	var o = document.createElement('div'); o.setAttribute('id', 'copublishbookmarklet');
115
	setCss(o, {
116
		   position: 'absolute',
117
		   overflow: 'hidden',
118
		   zIndex: 100000002,
119
		   opacity: '0.95',
120
		   backgroundColor: '#ffffff',
121
		   left: '0px',
122
		   top: '0px',
123
		   width: maxwidth + 'px',
124
		   minHeight: maxheight + 'px'	   
125
	});
126
 
127
	document.body.appendChild(o);
128
 
129
 
130
	var UserSelectedText = new String(getSelText());
131
	if (UserSelectedText != '') { 
132
		UserSelectedText = UserSelectedText.substr(0,500);	
133
	}
134
 
135
	var imgs = [];
136
	var youtube_img = false;
137
	var vimeo_img = false;
138
	var pinterest_img = false;
139
 
140
	var url = document.location.href;
141
	var url_parsed = parseURL(url);
142
	var store = url_parsed.host.toLowerCase();
143
	var images = [];
144
 
145
	if (youtube_img) {images[0] = {src: youtube_img.src, alt: '', w: 480, h: 360};
146
		 if (document.getElementById('watch-player')) setCss(document.getElementById('watch-player'), {visibility: 'hidden'});
147
	} else {
148
 
149
		imgs = document.getElementsByTagName('img');
150
		var l = imgs.length;
151
		var name = price = description = '';	
152
		if(UserSelectedText && UserSelectedText.length>0) {
153
			price = UserSelectedText;
154
		}
155
		try{
156
			for(var i = 0; i < l; i++) {
157
				var metatags = document.getElementsByTagName('meta');
158
				for (var m = 0; m < metatags.length; m++) {
159
					if(price.length==0) {
160
						if (metatags[m].getAttribute('itemprop') == 'price') {
161
							price = metatags[m].getAttribute('content');
162
						}
163
					}
164
					if (metatags[m].getAttribute('property') == 'og:description') {
165
						var description = metatags[m].getAttribute('content');
166
					}	
167
					if (metatags[m].getAttribute('property') == 'og:title') { 
168
						var name = metatags[m].getAttribute('content');
169
					}		
170
				}
171
				if (imgs.item(i).width > 80 && imgs.item(i).height > 80) {
172
					images.push({src: imgs.item(i).src, alt: imgs.item(i).alt, w: imgs.item(i).width, h: imgs.item(i).height, name: name, description: description,price: price});
173
				}
174
			}
175
		}catch(ex){
176
			console.log(ex);
177
		}
178
 
179
		l = images.length;
180
 
181
		var a = [];
182
		    for(var i=0; i < l; i++) {
183
		      for(var j=i+1; j < l; j++) {
184
			if (images[i].src == images[j].src) j = ++i;
185
		      }
186
		      a.push(images[i]);
187
		    }
188
		images = a;	l = images.length;
189
 
190
		var temp = 0, temp_img = {};
191
 
192
		for(var i=0; i < l; i++) {
193
			temp = images[i].w * images[i].h;
194
			for(var j = i + 1; j < l; j++) {
195
				if (temp < images[j].w * images[j].h) {			
196
					temp = images[j].w * images[j].h;			
197
					temp_img = images[i]; images[i] = images[j]; images[j] = temp_img;
198
				}
199
			}
200
		}
201
	}
202
	var l = images.length;
203
	var top_div=document.createElement('div');
204
	top_div.setAttribute('id', 'topcopublishbookmarklet');
205
 
206
	setCss(top_div, {
207
		   borderBottom: '1px solid #cccccc',
208
		   width: (maxwidth - 50) +'px',
209
		   height: '31px',
210
		   padding: '40px 25px 40px 25px',
211
		   fontSize: '19px',
212
		   backgroundColor: '#f9f8f6',
213
		   left: '0px',
214
		   top: '0px',
215
		   position: 'fixed',
216
		   textAlign: 'center',
217
		   overflow: 'hidden',
218
		   color: '#000000',
219
		   zIndex: 2
220
	});
221
 
222
	var top_text=document.createTextNode('Post a Product'); top_div.appendChild(top_text);
223
	o.appendChild(top_div);
224
 
225
	var logo = document.createElement('img');
226
	logo.src = hosturl+'img/logo.png';
227
	setCss(logo, {display: 'block', position: 'absolute', margin: '0px', left: '25px', top: '10px',zIndex: 3});
228
	top_div.appendChild(logo);
229
 
230
	var blank_div = document.createElement('div');
231
	setCss(blank_div, {width: maxwidth + 'px', height: '102px'});
232
	o.appendChild(blank_div);
233
 
234
	var error_div = document.createElement('div');
235
	error_div.setAttribute('id', 'errorcopublishbookmarklet');
236
	setCss(error_div, {width: '500px', height: 'auto', padding: '10px', textAlign: 'center', backgroundColor: '#ffffff', color: '#000000', fontSize: '14px', margin: '30px auto 30px auto', display: 'none'});
237
	o.appendChild(error_div);
238
 
239
	if (l == 0) {fThrowError('No vaild images found!'); return;}
240
	if (url_parsed.protocol.toLowerCase() != 'http' && url_parsed.protocol.toLowerCase() != 'https') {fThrowError('Images are available only for HTML websites!'); return;}
241
 
242
	var imgdivsize = Math.round((maxwidth / 5) - 2), imgsize = Math.round((imgdivsize / 100) * 83);
243
	var div;
244
 
245
	for(var i = 0; i < l; i++) { 
246
			if (UserSelectedText != '') images[i].alt = UserSelectedText;
247
			else images[i].alt = '';
248
		    div = document.createElement('div');
249
			setCss(div, {
250
				   width: imgdivsize + 'px',
251
				   height: imgdivsize + 'px',
252
				   border: '1px solid #cccccc',
253
				   cssFloat: 'left',
254
				   position: 'relative',
255
				   zIndex: 1,
256
				   overflow: 'hidden',
257
				   textAlign: 'center',
258
				   fontSize: '10px',
259
				   color: '#4a4a4a'
260
			});
261
			o.appendChild(div);
262
 
263
			var atag = document.createElement('a');
264
			setCss(atag, {
265
				   display: 'block', 
266
				   width: imgsize + 'px',
267
				   height: imgsize + 'px',
268
				   textAlign: 'center',
269
				   overflow: 'visible',
270
				   margin: '0px auto 10px auto',
271
				   backgroundColor: '#f6f6f6'
272
			});
273
			atag.setAttribute('href',hosturl+'products/viaweb/img:' + encodeURIComponent(base64_encode(images[i].src)) + '/url:' + encodeURIComponent(base64_encode(url)) + '/des:' + encodeURIComponent(base64_encode(images[i].description)) + '/name:' + encodeURIComponent(base64_encode(images[i].name)) + '/price:' + images[i].price + '/store:' + encodeURIComponent(base64_encode(store)));
274
			atag.onclick=function() {ResetBookmarklet(); window.open(this.getAttribute('href'),'_blank','status=no,resizable=yes,scrollbars=yes,personalbar=no,directories=no,location=no,toolbar=no,menubar=no,width=765,height=640,left=0,top=0'); return false;}
275
			div.appendChild(atag);
276
 
277
			var img = document.createElement('img');
278
			img.src = images[i].src;		
279
			setCss(img, {
280
				   maxWidth: imgsize + 'px',
281
				   maxHeight: imgsize + 'px',
282
				   border: 'none'			   
283
			});
284
			atag.appendChild(img);
285
 
286
			if (!youtube_img) {
287
			if (img.offsetHeight < imgsize) setCss(img, {marginTop: Math.round((imgsize - img.offsetHeight) * 0.5) + 'px'});
288
			else setCss(img, {marginTop: '0px'});
289
			}
290
			else setCss(img, {marginTop:'20px'});
291
 
292
			var copublish_this = document.createElement('img');
293
			copublish_this.src = hosturl+'img/menu_submit.png';
294
			copublish_this.className = 'copublish_this';
295
			setCss(copublish_this, {
296
				   position: 'absolute',
297
				   top: '50%',
298
				   marginTop: '-44px',
299
				   left: '50%',
300
				   marginLeft: '-73px',
301
				   display: 'none'
302
			});
303
			atag.appendChild(copublish_this);
304
 
305
			div.onmouseover = function(){setCss(this.getElementsByClassName('copublish_this')[0], {display: 'block'});}
306
			div.onmouseout = function(){setCss(this.getElementsByClassName('copublish_this')[0], {display: 'none'});}
307
			div.appendChild(document.createTextNode(images[i].w + ' x ' + images[i].h));
308
 
309
	}
310
 
311
	var cancel_div = document.createElement('div');
312
	cancel_div.setAttribute('id', 'cancelcopublishbookmarklet');
313
	setCss(cancel_div, {
314
		   borderTop: '1px solid #cccccc',
315
		   width: maxwidth + 'px',
316
		   padding: '10px 0px',
317
		   textAlign: 'center',
318
		   fontSize: '14px',
319
		   fontWeight: 'bold',
320
		   backgroundColor: '#f9f8f6',
321
		   cursor: 'pointer',
322
		   position: 'fixed',
323
		   left: '0px',
324
		   bottom: '0px',
325
		   zIndex: 100000003
326
	});
327
	document.body.appendChild(cancel_div);
328
	cancel_div.appendChild(document.createTextNode('Cancel Pin'));
329
	cancel_div.onclick = function() {ResetBookmarklet();}
330
	cancel_div.onmouseover = function(){setCss(this, {color: '#ffffff', backgroundColor: 'blue'});}
331
	cancel_div.onmouseout = function(){setCss(this, {color: '#000000', backgroundColor: '#f9f8f6'});}
332
}
333
fInitializeReachliBookMarklet();