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