Skip to content

Instantly share code, notes, and snippets.

@kwijibo kwijibo/x
Created Apr 28, 2009

Embed
What would you like to do?
morph current document url
function absolutifyUrls(data, sourceUrl) {
var relRe = /:\/\//;
var domainRe = /.*?\/\/[^?/]*/;
var pathRe = /.*\//;
if (sourceUrl.length-sourceUrl.replace(/\//g,"").length==2) {
sourceUrl += "/";
}
if (typeof(data)=="string") {
data = jQuery("<div>"+data+"</div>");
} else if (typeof(data)=="object") {
data = jQuery('<div>').append(data);
}
jQuery("a",data).each(function correctUrls(){
var el = jQuery(this);
var href = el.attr("href");
if (!relRe.exec(href)) {
if (href[0] == "/") {
href = domainRe.exec(sourceUrl)+href;
}
else {
href = pathRe.exec(sourceUrl)+href;
}
el.attr("href", href);
}
});
return data.html();
};
/* This is a template command. */
CmdUtils.CreateCommand({
name: "morph",
//icon: "http://example.com/example.png",
homepage: "http://morph.talis.com/",
author: {name: "Keith Alexander", email: "keith.alexander@talis.com"},
license: "GPL",
description: "Morphs current document",
help: "How to use your command",
preview: function(pblock ) {
var baseURI = "http://morph.talis.com/";
jQuery.ajax(
{
"url": baseURI+"?data-uri[]=" + encodeURIComponent( CmdUtils.getDocument().location.href ),
"type" : 'GET',
"params" : {
"data-uri[]" : CmdUtils.getDocument().location.href ,
},
"success" : function(data){
data = absolutifyUrls(data, baseURI);
pblock.innerHTML = jQuery('.resources ol', data).html();
}
});
},
execute: function() {
var baseURI = "http://morph.talis.com/";
Utils.openUrlInBrowser(baseURI+"?data-uri[]=" + encodeURIComponent( CmdUtils.getDocument().location.href ));
}
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.