Created
January 9, 2013 22:52
-
-
Save chrisallick/4497754 to your computer and use it in GitHub Desktop.
parse urls for serviceable content and adjust accordingly
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Services = function( _p ) { | |
self = this; | |
parent = _p; | |
this.services = { | |
"cl.ly": "cloudapp", | |
"www.dropbox.com": "dropbox" | |
} | |
this.events = { | |
cloudapp: function( url_obj ) { | |
path = url_obj.pathname; | |
// assumes '/' is first character and ther are no '/'s after... | |
path = path.replace(/\//g,''); | |
//path = path.split("/")[1]; | |
$.ajax({ | |
type: "GET", | |
url: "/services/cloudapp", | |
data: { drop: path }, | |
dataType: "json", | |
}).done(function( resp ) { | |
console.log( resp ); | |
if( resp && resp.result == "success" && resp.msg && resp.type ) { | |
if( resp.type == "image" ) { | |
var msg = trash.createMessage( "chat", resp.msg ); | |
trash.trashio.sendMessage( msg ); | |
} else if( resp.type == "bookmark" ) { | |
var msg = trash.createMessage( "chat", resp.msg ); | |
msg["anon"] = isanon; | |
trash.trashio.sendMessage( msg ); | |
$("#chat").scrollTop($("#chat")[0].scrollHeight); | |
} | |
} | |
}); | |
}, dropbox: function( url_obj ) { | |
url_obj.host = "dl.dropbox.com"; | |
var msg = trash.createMessage( "chat", url_obj.href ); | |
trash.trashio.sendMessage( msg ); | |
} | |
} | |
/* | |
return a url object | |
http://example.com:3000/pathname/?search=test#hash | |
parser.protocol; // => "http:" | |
parser.hostname; // => "example.com" | |
parser.port; // => "3000" | |
parser.pathname; // => "/pathname/" | |
parser.search; // => "?search=test" | |
parser.hash; // => "#hash" | |
parser.host; // => "example.com:3000" | |
*/ | |
this.urlObj = function( url ) { | |
var a = document.createElement('a'); | |
a.href = url; | |
return a; | |
} | |
this.isSupportedService = function( url ) { | |
var parts = url.split(" "); | |
if( parts && parts.length == 1 ) { | |
url_obj = self.urlObj( parts[0] ); | |
if( self.services[url_obj.hostname] && url_obj.hostname != "/" ) { | |
self.events[self.services[url_obj.hostname]](url_obj); | |
return true; | |
} | |
} | |
return false; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment