Mercurial > tinyurl_creator
comparison chrome/content/tinyurl.js @ 0:c14d52a3b2fe
initial import
author | Yoshiki Yazawa <yaz@honeyplanet.jp> |
---|---|
date | Mon, 11 Aug 2008 20:34:21 +0900 |
parents | |
children | bebb38edfc61 |
comparison
equal
deleted
inserted
replaced
-1:000000000000 | 0:c14d52a3b2fe |
---|---|
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); } | |
13 | |
14 // Update all links with preview title attribute | |
15 if(gBrowser && !tinyurl_loaded){ | |
16 | |
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); | |
27 | |
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"); | |
40 | |
41 // Cancel button | |
42 document.getElementById('tinyurlDialog').getButton('cancel').setAttribute("label", "Close"); | |
43 | |
44 // Is RDF Present | |
45 tinyurl_saved_rdfCreate(); | |
46 | |
47 // Load Saved Data | |
48 tinyurl_saved_read(); | |
49 | |
50 // Preload Field | |
51 if(opener.gBrowser && opener.gBrowser.currentURI){ | |
52 oLongUrlField.value = opener.gBrowser.currentURI.spec; | |
53 } | |
54 | |
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{ | |
75 | |
76 // If not in main tab | |
77 if(oTabBox.selectedTab.id != "tinyurl-create-tab"){ | |
78 return; | |
79 } | |
80 | |
81 // Validate | |
82 if(oLongUrlField == null || oLongUrlField.value == ""){ | |
83 alert("You have not entered a long url"); | |
84 return; | |
85 } | |
86 | |
87 document.getElementById("tinyurl-field").value = "Loading..."; | |
88 | |
89 // Title | |
90 var sTitle = ""; | |
91 if(opener && oLongUrlField.value == opener.gBrowser.currentURI.spec){ | |
92 sTitle = opener.window._content.document.title; | |
93 } | |
94 | |
95 // Get TinyUrl | |
96 var tiny = new TinyUrl(oLongUrlField.value, sTitle); | |
97 | |
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 }, | |
109 | |
110 error : function(oTiny){ | |
111 document.getElementById("tinyurl-field").value = ""; | |
112 alert("An error occurred.\nIs your internet connection available?"); | |
113 } | |
114 } | |
115 | |
116 tiny.shrink(loaded); | |
117 | |
118 }catch(err){ alert("Error 120\nAn unknown error occurred\n"+ err); } | |
119 } | |
120 | |
121 /** | |
122 * Create TinyUrl from anchor on page | |
123 */ | |
124 function tinyurl_createFromAnchor(){ | |
125 if(gContextMenu != null && (gContextMenu.getLinkURL || gContextMenu.linkURL)){ | |
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"); | |
137 | |
138 if(oField == null || oField.value == ""){ | |
139 return; | |
140 } | |
141 | |
142 //Copy | |
143 tinyurl_copyText(oField.value); | |
144 | |
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); | |
164 | |
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(); | |
184 | |
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){ | |
197 | |
198 /** | |
199 * Original url | |
200 * @type string | |
201 */ | |
202 this.original = url; | |
203 | |
204 /** | |
205 * Shunken url | |
206 * @type string | |
207 */ | |
208 this.tiny = null; | |
209 | |
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; | |
216 | |
217 /** | |
218 * @type XMLHttpRequest | |
219 */ | |
220 this.request = null; | |
221 | |
222 | |
223 /** | |
224 * Make the long url tiny | |
225 */ | |
226 this.shrink = function(callback){ | |
227 | |
228 // No need to shrink | |
229 if(this.tiny != null){ | |
230 return; | |
231 } | |
232 | |
233 // Abort current request | |
234 if(this.request != null){ | |
235 this.request.abort(); | |
236 } | |
237 | |
238 // Encode URL (can't use encodeURIComponent, because TinyUrl.com will choke) | |
239 var url = this.original; | |
240 url = url.replace(/\+/g, "%2B"); | |
241 url = url.replace(/\?/g, '%3F'); | |
242 url = url.replace(/&/g, '%26'); | |
243 url = url.replace(/=/g, '%3D'); | |
244 | |
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"); | |
254 | |
255 var obj = this; | |
256 | |
257 // Handle request | |
258 this.request.onload = { | |
259 handleEvent : function(e){ | |
260 | |
261 // If already aborted | |
262 if(obj.request == null){ | |
263 return; | |
264 } | |
265 | |
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 } | |
272 | |
273 // Notify callback function | |
274 if(obj.tiny == null){ | |
275 callback.error(obj); | |
276 } | |
277 else{ | |
278 callback.done(obj); | |
279 } | |
280 } | |
281 }; | |
282 | |
283 // Handle Error | |
284 this.request.onerror = { | |
285 handleEvent : function(e){ | |
286 callback.error(obj); | |
287 } | |
288 } | |
289 | |
290 // Start Request | |
291 this.request.send(""); | |
292 } | |
293 | |
294 /** | |
295 * Abort TinyUrl shrink request | |
296 */ | |
297 this.abort = function(){ | |
298 try{ | |
299 this.request.abort(); | |
300 }catch(e){ } | |
301 } | |
302 | |
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){ | |
308 | |
309 // Find where it redirects to | |
310 try{ | |
311 | |
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); | |
317 | |
318 request.redirectionLimit = 0; | |
319 request.requestMethod = "HEAD"; | |
320 | |
321 // Start request and find redirect location | |
322 request.asyncOpen({ | |
323 onStartRequest : function(request, context){ }, | |
324 onStopRequest : function(request, context, statusCode){ | |
325 var location = request.getResponseHeader("Location"); | |
326 anchor.setAttribute("title", location); | |
327 }, | |
328 onDataAvailable : function(request, context, inputStream, offset, count){} | |
329 }, null); | |
330 | |
331 } catch(e){ | |
332 e = e; | |
333 } | |
334 | |
335 | |
336 } | |
337 } |