Mercurial > tinyurl_creator
comparison chrome/content/tinyurl.js @ 1:bebb38edfc61
removed dangling spaces.
author | Yoshiki Yazawa <yaz@honeyplanet.jp> |
---|---|
date | Mon, 11 Aug 2008 20:36:59 +0900 |
parents | c14d52a3b2fe |
children | 0d369fd33074 |
comparison
equal
deleted
inserted
replaced
0:c14d52a3b2fe | 1:bebb38edfc61 |
---|---|
8 function tinyurl_overlayInit(){ | 8 function tinyurl_overlayInit(){ |
9 try{ | 9 try{ |
10 var oContext = document.getElementById("contentAreaContextMenu"); | 10 var oContext = document.getElementById("contentAreaContextMenu"); |
11 oContext.setAttribute("onpopupshowing", "tinyurl_contextShowing(event); "+ oContext.getAttribute("onpopupshowing")); | 11 oContext.setAttribute("onpopupshowing", "tinyurl_contextShowing(event); "+ oContext.getAttribute("onpopupshowing")); |
12 } catch(err) { alert(err); } | 12 } catch(err) { alert(err); } |
13 | 13 |
14 // Update all links with preview title attribute | 14 // Update all links with preview title attribute |
15 if(gBrowser && !tinyurl_loaded){ | 15 if(gBrowser && !tinyurl_loaded){ |
16 | 16 |
17 gBrowser.addEventListener("load", function(){ | 17 gBrowser.addEventListener("load", function(){ |
18 var tiny = new TinyUrl(); | 18 var tiny = new TinyUrl(); |
19 var doc = gBrowser.selectedBrowser.contentDocument; | 19 var doc = gBrowser.selectedBrowser.contentDocument; |
20 var anchors = doc.getElementsByTagName("a"); | 20 var anchors = doc.getElementsByTagName("a"); |
21 for(var i = 0; i < anchors.length; i++){ | 21 for(var i = 0; i < anchors.length; i++){ |
22 if(anchors[i].href.indexOf("http://tinyurl.com/") == 0){ | 22 if(anchors[i].href.indexOf("http://tinyurl.com/") == 0){ |
23 tiny.addPreview(anchors[i]); | 23 tiny.addPreview(anchors[i]); |
24 } | 24 } |
25 } | 25 } |
26 }, true); | 26 }, true); |
27 | 27 |
28 tinyurl_loaded = true; | 28 tinyurl_loaded = true; |
29 } | 29 } |
30 } | 30 } |
31 | 31 |
32 /** | 32 /** |
35 function tinyurl_load(){ | 35 function tinyurl_load(){ |
36 oLongUrlField = document.getElementById("longurl-field"); | 36 oLongUrlField = document.getElementById("longurl-field"); |
37 oTabBox = document.getElementById("tinyurl-tabs"); | 37 oTabBox = document.getElementById("tinyurl-tabs"); |
38 oSavedTree = document.getElementById("saved-tree"); | 38 oSavedTree = document.getElementById("saved-tree"); |
39 oSavedTreeItems = document.getElementById("saved-items"); | 39 oSavedTreeItems = document.getElementById("saved-items"); |
40 | 40 |
41 // Cancel button | 41 // Cancel button |
42 document.getElementById('tinyurlDialog').getButton('cancel').setAttribute("label", "Close"); | 42 document.getElementById('tinyurlDialog').getButton('cancel').setAttribute("label", "Close"); |
43 | 43 |
44 // Is RDF Present | 44 // Is RDF Present |
45 tinyurl_saved_rdfCreate(); | 45 tinyurl_saved_rdfCreate(); |
46 | 46 |
47 // Load Saved Data | 47 // Load Saved Data |
48 tinyurl_saved_read(); | 48 tinyurl_saved_read(); |
49 | 49 |
50 // Preload Field | 50 // Preload Field |
51 if(opener.gBrowser && opener.gBrowser.currentURI){ | 51 if(opener.gBrowser && opener.gBrowser.currentURI){ |
52 oLongUrlField.value = opener.gBrowser.currentURI.spec; | 52 oLongUrlField.value = opener.gBrowser.currentURI.spec; |
53 } | 53 } |
54 | 54 |
55 // Goto Save Tab | 55 // Goto Save Tab |
56 if(window.arguments.length > 0 && window.arguments[0] == "saved"){ | 56 if(window.arguments.length > 0 && window.arguments[0] == "saved"){ |
57 oTabBox.selectedTab = document.getElementById("tinyurl-saved-tab"); | 57 oTabBox.selectedTab = document.getElementById("tinyurl-saved-tab"); |
58 oTabBox.selectedPanel = document.getElementById("tinyurl-saved-tabpanel"); | 58 oTabBox.selectedPanel = document.getElementById("tinyurl-saved-tabpanel"); |
59 } | 59 } |
70 /** | 70 /** |
71 * Creat new Tiny Url from dialog | 71 * Creat new Tiny Url from dialog |
72 */ | 72 */ |
73 function tinyurl_createNew(){ | 73 function tinyurl_createNew(){ |
74 try{ | 74 try{ |
75 | 75 |
76 // If not in main tab | 76 // If not in main tab |
77 if(oTabBox.selectedTab.id != "tinyurl-create-tab"){ | 77 if(oTabBox.selectedTab.id != "tinyurl-create-tab"){ |
78 return; | 78 return; |
79 } | 79 } |
80 | 80 |
81 // Validate | 81 // Validate |
82 if(oLongUrlField == null || oLongUrlField.value == ""){ | 82 if(oLongUrlField == null || oLongUrlField.value == ""){ |
83 alert("You have not entered a long url"); | 83 alert("You have not entered a long url"); |
84 return; | 84 return; |
85 } | 85 } |
86 | 86 |
87 document.getElementById("tinyurl-field").value = "Loading..."; | 87 document.getElementById("tinyurl-field").value = "Loading..."; |
88 | 88 |
89 // Title | 89 // Title |
90 var sTitle = ""; | 90 var sTitle = ""; |
91 if(opener && oLongUrlField.value == opener.gBrowser.currentURI.spec){ | 91 if(opener && oLongUrlField.value == opener.gBrowser.currentURI.spec){ |
92 sTitle = opener.window._content.document.title; | 92 sTitle = opener.window._content.document.title; |
93 } | 93 } |
94 | 94 |
95 // Get TinyUrl | 95 // Get TinyUrl |
96 var tiny = new TinyUrl(oLongUrlField.value, sTitle); | 96 var tiny = new TinyUrl(oLongUrlField.value, sTitle); |
97 | 97 |
98 // Output | 98 // Output |
99 var loaded = { | 99 var loaded = { |
100 done : function(oTiny){ | 100 done : function(oTiny){ |
101 if(oTiny.tiny != null){ | 101 if(oTiny.tiny != null){ |
102 document.getElementById("tinyurl-field").value = oTiny.tiny; | 102 document.getElementById("tinyurl-field").value = oTiny.tiny; |
104 document.getElementById("save-button").setAttribute("oncommand", "tinyurl_saved_add('"+ oTiny.tiny +"', '"+ oTiny.original +"');"); | 104 document.getElementById("save-button").setAttribute("oncommand", "tinyurl_saved_add('"+ oTiny.tiny +"', '"+ oTiny.original +"');"); |
105 document.getElementById("save-button").disabled = false; | 105 document.getElementById("save-button").disabled = false; |
106 document.getElementById('tinyurlDialogMain').style.cursor = "default"; | 106 document.getElementById('tinyurlDialogMain').style.cursor = "default"; |
107 } | 107 } |
108 }, | 108 }, |
109 | 109 |
110 error : function(oTiny){ | 110 error : function(oTiny){ |
111 document.getElementById("tinyurl-field").value = ""; | 111 document.getElementById("tinyurl-field").value = ""; |
112 alert("An error occurred.\nIs your internet connection available?"); | 112 alert("An error occurred.\nIs your internet connection available?"); |
113 } | 113 } |
114 } | 114 } |
115 | 115 |
116 tiny.shrink(loaded); | 116 tiny.shrink(loaded); |
117 | 117 |
118 }catch(err){ alert("Error 120\nAn unknown error occurred\n"+ err); } | 118 }catch(err){ alert("Error 120\nAn unknown error occurred\n"+ err); } |
119 } | 119 } |
120 | 120 |
121 /** | 121 /** |
122 * Create TinyUrl from anchor on page | 122 * Create TinyUrl from anchor on page |
123 */ | 123 */ |
124 function tinyurl_createFromAnchor(){ | 124 function tinyurl_createFromAnchor(){ |
125 if(gContextMenu != null && (gContextMenu.getLinkURL || gContextMenu.linkURL)){ | 125 if(gContextMenu != null && (gContextMenu.getLinkURL || gContextMenu.linkURL)){ |
126 var url = (gContextMenu.getLinkURL) ? gContextMenu.getLinkURL() : gContextMenu.linkURL | 126 var url = (gContextMenu.getLinkURL) ? gContextMenu.getLinkURL() : gContextMenu.linkURL |
127 var tiny = new TinyUrl(url, null); | 127 var tiny = new TinyUrl(url, null); |
128 | 128 |
129 window.openDialog('chrome://tinyurl/content/create.xul','TinyUrlCreate','chrome, centerscreen, resizable', tiny); | 129 window.openDialog('chrome://tinyurl/content/create.xul','TinyUrlCreate','chrome, centerscreen, resizable', tiny); |
130 } | 130 } |
132 | 132 |
133 //Send text to tinyurl_copyText() | 133 //Send text to tinyurl_copyText() |
134 function tinyurl_copy(){ | 134 function tinyurl_copy(){ |
135 try{ | 135 try{ |
136 var oField = document.getElementById("tinyurl-field"); | 136 var oField = document.getElementById("tinyurl-field"); |
137 | 137 |
138 if(oField == null || oField.value == ""){ | 138 if(oField == null || oField.value == ""){ |
139 return; | 139 return; |
140 } | 140 } |
141 | 141 |
142 //Copy | 142 //Copy |
143 tinyurl_copyText(oField.value); | 143 tinyurl_copyText(oField.value); |
144 | 144 |
145 }catch(err){ alert("Error 140\nCouldn't copy to clipboard"); } | 145 }catch(err){ alert("Error 140\nCouldn't copy to clipboard"); } |
146 } | 146 } |
147 | 147 |
148 // Comand from key stroke | 148 // Comand from key stroke |
149 function tinyurl_copy_command(){ | 149 function tinyurl_copy_command(){ |
159 function tinyurl_copyText(str){ | 159 function tinyurl_copyText(str){ |
160 try{ | 160 try{ |
161 //Copy | 161 //Copy |
162 var oClipboard = Components.classes["@mozilla.org/widget/clipboardhelper;1"].getService(Components.interfaces.nsIClipboardHelper); | 162 var oClipboard = Components.classes["@mozilla.org/widget/clipboardhelper;1"].getService(Components.interfaces.nsIClipboardHelper); |
163 oClipboard.copyString(str); | 163 oClipboard.copyString(str); |
164 | 164 |
165 }catch(err){ throw err } | 165 }catch(err){ throw err } |
166 } | 166 } |
167 | 167 |
168 // Context Showing | 168 // Context Showing |
169 function tinyurl_contextShowing(event){ | 169 function tinyurl_contextShowing(event){ |
179 | 179 |
180 //Opens http://tinyurl.com | 180 //Opens http://tinyurl.com |
181 function tinyurl_gotoTinyUrl(){ | 181 function tinyurl_gotoTinyUrl(){ |
182 opener.focus(); | 182 opener.focus(); |
183 self.focus(); | 183 self.focus(); |
184 | 184 |
185 if(opener.gBrowser.addTab) | 185 if(opener.gBrowser.addTab) |
186 opener.gBrowser.selectedTab = opener.gBrowser.addTab("http://tinyurl.com"); | 186 opener.gBrowser.selectedTab = opener.gBrowser.addTab("http://tinyurl.com"); |
187 else | 187 else |
188 window.open("http://tinyurl.com"); | 188 window.open("http://tinyurl.com"); |
189 } | 189 } |
192 | 192 |
193 /** | 193 /** |
194 * @constructor | 194 * @constructor |
195 */ | 195 */ |
196 function TinyUrl(url, title){ | 196 function TinyUrl(url, title){ |
197 | 197 |
198 /** | 198 /** |
199 * Original url | 199 * Original url |
200 * @type string | 200 * @type string |
201 */ | 201 */ |
202 this.original = url; | 202 this.original = url; |
203 | 203 |
204 /** | 204 /** |
205 * Shunken url | 205 * Shunken url |
206 * @type string | 206 * @type string |
207 */ | 207 */ |
208 this.tiny = null; | 208 this.tiny = null; |
209 | 209 |
210 /** | 210 /** |
211 * Title of the page from the original url. | 211 * Title of the page from the original url. |
212 * Used for when the user selects to save the tiny url. | 212 * Used for when the user selects to save the tiny url. |
213 * @type string | 213 * @type string |
214 */ | 214 */ |
215 this.title = title; | 215 this.title = title; |
216 | 216 |
217 /** | 217 /** |
218 * @type XMLHttpRequest | 218 * @type XMLHttpRequest |
219 */ | 219 */ |
220 this.request = null; | 220 this.request = null; |
221 | 221 |
222 | 222 |
223 /** | 223 /** |
224 * Make the long url tiny | 224 * Make the long url tiny |
225 */ | 225 */ |
226 this.shrink = function(callback){ | 226 this.shrink = function(callback){ |
227 | 227 |
228 // No need to shrink | 228 // No need to shrink |
229 if(this.tiny != null){ | 229 if(this.tiny != null){ |
230 return; | 230 return; |
231 } | 231 } |
232 | 232 |
233 // Abort current request | 233 // Abort current request |
234 if(this.request != null){ | 234 if(this.request != null){ |
235 this.request.abort(); | 235 this.request.abort(); |
236 } | 236 } |
237 | 237 |
238 // Encode URL (can't use encodeURIComponent, because TinyUrl.com will choke) | 238 // Encode URL (can't use encodeURIComponent, because TinyUrl.com will choke) |
239 var url = this.original; | 239 var url = this.original; |
240 url = url.replace(/\+/g, "%2B"); | 240 url = url.replace(/\+/g, "%2B"); |
241 url = url.replace(/\?/g, '%3F'); | 241 url = url.replace(/\?/g, '%3F'); |
242 url = url.replace(/&/g, '%26'); | 242 url = url.replace(/&/g, '%26'); |
243 url = url.replace(/=/g, '%3D'); | 243 url = url.replace(/=/g, '%3D'); |
244 | 244 |
245 // Setup Request | 245 // Setup Request |
246 this.request = new XMLHttpRequest(); | 246 this.request = new XMLHttpRequest(); |
247 this.request.open("GET", "http://tinyurl.com/api-create.php?url="+ url, true); | 247 this.request.open("GET", "http://tinyurl.com/api-create.php?url="+ url, true); |
248 this.request.setRequestHeader("User-Agent", navigator.userAgent); | 248 this.request.setRequestHeader("User-Agent", navigator.userAgent); |
249 this.request.setRequestHeader("Accept", "text/xml,application/xml,application/xhtml+xml,text/html;q=0.9,text/plain;q=0.8,video/x-mng,image/png,image/jpeg,image/gif;q=0.2,*/*;q=0.1"); | 249 this.request.setRequestHeader("Accept", "text/xml,application/xml,application/xhtml+xml,text/html;q=0.9,text/plain;q=0.8,video/x-mng,image/png,image/jpeg,image/gif;q=0.2,*/*;q=0.1"); |
250 this.request.setRequestHeader("Accept-Language", navigator.language); | 250 this.request.setRequestHeader("Accept-Language", navigator.language); |
251 this.request.setRequestHeader("Accept-Charset", "ISO-8859-1,utf-8;q=0.7,*;q=0.7"); | 251 this.request.setRequestHeader("Accept-Charset", "ISO-8859-1,utf-8;q=0.7,*;q=0.7"); |
252 this.request.setRequestHeader("Referer", "http://tinyurl.com/"); | 252 this.request.setRequestHeader("Referer", "http://tinyurl.com/"); |
253 this.request.setRequestHeader("Content-Type", "application/x-www-form-urlencoded"); | 253 this.request.setRequestHeader("Content-Type", "application/x-www-form-urlencoded"); |
254 | 254 |
255 var obj = this; | 255 var obj = this; |
256 | 256 |
257 // Handle request | 257 // Handle request |
258 this.request.onload = { | 258 this.request.onload = { |
259 handleEvent : function(e){ | 259 handleEvent : function(e){ |
260 | 260 |
261 // If already aborted | 261 // If already aborted |
262 if(obj.request == null){ | 262 if(obj.request == null){ |
263 return; | 263 return; |
264 } | 264 } |
265 | 265 |
266 //Parse Response | 266 //Parse Response |
267 obj.tiny = null; | 267 obj.tiny = null; |
268 var url = obj.request.responseText; | 268 var url = obj.request.responseText; |
269 if(url.length < 200){ // must have returned more than the URL if more than 200 | 269 if(url.length < 200){ // must have returned more than the URL if more than 200 |
270 obj.tiny = url; | 270 obj.tiny = url; |
271 } | 271 } |
272 | 272 |
273 // Notify callback function | 273 // Notify callback function |
274 if(obj.tiny == null){ | 274 if(obj.tiny == null){ |
275 callback.error(obj); | 275 callback.error(obj); |
276 } | 276 } |
277 else{ | 277 else{ |
278 callback.done(obj); | 278 callback.done(obj); |
279 } | 279 } |
280 } | 280 } |
281 }; | 281 }; |
282 | 282 |
283 // Handle Error | 283 // Handle Error |
284 this.request.onerror = { | 284 this.request.onerror = { |
285 handleEvent : function(e){ | 285 handleEvent : function(e){ |
286 callback.error(obj); | 286 callback.error(obj); |
287 } | 287 } |
288 } | 288 } |
289 | 289 |
290 // Start Request | 290 // Start Request |
291 this.request.send(""); | 291 this.request.send(""); |
292 } | 292 } |
293 | 293 |
294 /** | 294 /** |
295 * Abort TinyUrl shrink request | 295 * Abort TinyUrl shrink request |
296 */ | 296 */ |
297 this.abort = function(){ | 297 this.abort = function(){ |
298 try{ | 298 try{ |
299 this.request.abort(); | 299 this.request.abort(); |
300 }catch(e){ } | 300 }catch(e){ } |
301 } | 301 } |
302 | 302 |
303 /** | 303 /** |
304 * Add a the full URL to an link with a TinyURL | 304 * Add a the full URL to an link with a TinyURL |
305 * @param {Anchor} anchor The anchor tag with a TinyURL | 305 * @param {Anchor} anchor The anchor tag with a TinyURL |
306 */ | 306 */ |
307 this.addPreview = function(anchor){ | 307 this.addPreview = function(anchor){ |
308 | 308 |
309 // Find where it redirects to | 309 // Find where it redirects to |
310 try{ | 310 try{ |
311 | 311 |
312 // Setup request objects | 312 // Setup request objects |
313 var io = Components.classes["@mozilla.org/network/io-service;1"].getService(Components.interfaces.nsIIOService); | 313 var io = Components.classes["@mozilla.org/network/io-service;1"].getService(Components.interfaces.nsIIOService); |
314 var uri = Components.classes["@mozilla.org/network/standard-url;1"].createInstance(Components.interfaces.nsIURI); | 314 var uri = Components.classes["@mozilla.org/network/standard-url;1"].createInstance(Components.interfaces.nsIURI); |
315 uri.spec = anchor.href; | 315 uri.spec = anchor.href; |
316 var request = io.newChannelFromURI(uri).QueryInterface(Components.interfaces.nsIHttpChannel); | 316 var request = io.newChannelFromURI(uri).QueryInterface(Components.interfaces.nsIHttpChannel); |
317 | 317 |
318 request.redirectionLimit = 0; | 318 request.redirectionLimit = 0; |
319 request.requestMethod = "HEAD"; | 319 request.requestMethod = "HEAD"; |
320 | 320 |
321 // Start request and find redirect location | 321 // Start request and find redirect location |
322 request.asyncOpen({ | 322 request.asyncOpen({ |
323 onStartRequest : function(request, context){ }, | 323 onStartRequest : function(request, context){ }, |
324 onStopRequest : function(request, context, statusCode){ | 324 onStopRequest : function(request, context, statusCode){ |
325 var location = request.getResponseHeader("Location"); | 325 var location = request.getResponseHeader("Location"); |
326 anchor.setAttribute("title", location); | 326 anchor.setAttribute("title", location); |
327 }, | 327 }, |
328 onDataAvailable : function(request, context, inputStream, offset, count){} | 328 onDataAvailable : function(request, context, inputStream, offset, count){} |
329 }, null); | 329 }, null); |
330 | 330 |
331 } catch(e){ | 331 } catch(e){ |
332 e = e; | 332 e = e; |
333 } | 333 } |
334 | 334 |
335 | 335 |
336 } | 336 } |
337 } | 337 } |