-
-
Save kwijibo/103265 to your computer and use it in GitHub Desktop.
morph current document url
This file contains hidden or 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
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: "[email protected]"}, | |
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