2015-03-25 19:28:33 +01:00
|
|
|
(function($) {
|
2015-03-25 19:54:07 +01:00
|
|
|
var plugins = [
|
|
|
|
{
|
|
|
|
"id": "CHOOSE",
|
|
|
|
"name": "Please choose your conversion ...",
|
2015-03-26 22:06:54 +01:00
|
|
|
"convert": function (input) {
|
2015-03-26 18:13:18 +01:00
|
|
|
return {
|
|
|
|
"status": "OK",
|
|
|
|
"content": ""
|
|
|
|
};
|
2015-03-25 19:54:07 +01:00
|
|
|
}
|
|
|
|
},
|
|
|
|
{
|
|
|
|
"id": "base64decode",
|
2015-03-26 22:25:01 +01:00
|
|
|
"name": "Decode Base64",
|
2015-03-26 22:06:54 +01:00
|
|
|
"convert": function (input) {
|
2015-03-25 19:54:07 +01:00
|
|
|
try {
|
2015-03-26 18:13:18 +01:00
|
|
|
return {
|
|
|
|
"status": "OK",
|
|
|
|
"content": atob(input)
|
|
|
|
};
|
2015-03-25 19:54:07 +01:00
|
|
|
} catch (exception) {
|
2015-03-26 18:13:18 +01:00
|
|
|
return {
|
|
|
|
"status": "ERROR",
|
|
|
|
"content": "Invalid base64 input string."
|
|
|
|
};
|
2015-03-25 19:54:07 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
},
|
|
|
|
{
|
|
|
|
"id": "decodeuri",
|
|
|
|
"name": "Decode URI",
|
2015-03-26 22:06:54 +01:00
|
|
|
"convert": function (input) {
|
2015-03-26 17:58:42 +01:00
|
|
|
try {
|
2015-03-26 18:13:18 +01:00
|
|
|
return {
|
|
|
|
"status": "OK",
|
|
|
|
"content": decodeURI(input)
|
|
|
|
};
|
2015-03-26 17:58:42 +01:00
|
|
|
} catch (exception) {
|
2015-03-26 18:13:18 +01:00
|
|
|
return {
|
|
|
|
"status": "ERROR",
|
|
|
|
"content": "Invalid URI input string."
|
|
|
|
};
|
2015-03-26 17:58:42 +01:00
|
|
|
}
|
2015-03-25 19:54:07 +01:00
|
|
|
}
|
|
|
|
},
|
|
|
|
{
|
|
|
|
"id": "decodeuricomponent",
|
|
|
|
"name": "Decode URI component",
|
2015-03-26 22:06:54 +01:00
|
|
|
"convert": function (input) {
|
2015-03-26 17:58:42 +01:00
|
|
|
try {
|
2015-03-26 18:13:18 +01:00
|
|
|
return {
|
|
|
|
"status": "OK",
|
|
|
|
"content": decodeURIComponent(input)
|
|
|
|
};
|
2015-03-26 17:58:42 +01:00
|
|
|
} catch (exception) {
|
2015-03-26 18:13:18 +01:00
|
|
|
return {
|
|
|
|
"status": "ERROR",
|
|
|
|
"content": "Invalid URI component input string."
|
|
|
|
};
|
2015-03-26 17:58:42 +01:00
|
|
|
}
|
2015-03-25 19:54:07 +01:00
|
|
|
}
|
2015-03-26 22:06:54 +01:00
|
|
|
},
|
|
|
|
{
|
|
|
|
"id": "decodehtmlentities",
|
|
|
|
"name": "Decode HTML entities",
|
|
|
|
"convert": function (input) {
|
|
|
|
try {
|
|
|
|
return {
|
|
|
|
"status": "OK",
|
|
|
|
"content": input
|
|
|
|
.replace(/\"\;/g, "\"")
|
|
|
|
.replace(/\>\;/g, ">")
|
|
|
|
.replace(/\<\;/g, "<")
|
|
|
|
.replace(/\&\;/g, "&")
|
|
|
|
};
|
|
|
|
} catch (exception) {
|
|
|
|
return {
|
|
|
|
"status": "ERROR",
|
|
|
|
"content": "Invalid HTML entity string."
|
|
|
|
};
|
|
|
|
}
|
|
|
|
}
|
2015-03-26 22:51:00 +01:00
|
|
|
},
|
2015-03-26 23:11:59 +01:00
|
|
|
{
|
|
|
|
"id": "decodequotedprintable",
|
|
|
|
"name": "Decode quoted printable",
|
|
|
|
"convert": function (input) {
|
|
|
|
try {
|
|
|
|
return {
|
2015-03-26 23:33:58 +01:00
|
|
|
"status": "OK",
|
|
|
|
"content": utf8.decode(quotedPrintable.decode(input))
|
2015-03-26 23:11:59 +01:00
|
|
|
};
|
|
|
|
} catch (exception) {
|
|
|
|
return {
|
|
|
|
"status": "ERROR",
|
|
|
|
"content": "Invalid quoted printable string."
|
|
|
|
};
|
|
|
|
}
|
2015-03-26 23:33:58 +01:00
|
|
|
}
|
2015-03-26 23:11:59 +01:00
|
|
|
},
|
2016-08-15 17:50:23 +02:00
|
|
|
{
|
|
|
|
"id": "hextodec",
|
|
|
|
"name": "Decode hex as decimal",
|
|
|
|
"convert": function (input) {
|
|
|
|
try {
|
|
|
|
return {
|
|
|
|
"status": "OK",
|
|
|
|
"content": parseInt(input, 16).toString(10)
|
|
|
|
};
|
|
|
|
} catch (exception) {
|
|
|
|
return {
|
|
|
|
"status": "ERROR",
|
|
|
|
"content": "Invalid number (integer) string."
|
|
|
|
};
|
|
|
|
}
|
|
|
|
}
|
|
|
|
},
|
|
|
|
{
|
|
|
|
"id": "bintodec",
|
|
|
|
"name": "Decode binary as decimal",
|
|
|
|
"convert": function (input) {
|
|
|
|
try {
|
|
|
|
return {
|
|
|
|
"status": "OK",
|
|
|
|
"content": parseInt(input, 2).toString(10)
|
|
|
|
};
|
|
|
|
} catch (exception) {
|
|
|
|
return {
|
|
|
|
"status": "ERROR",
|
|
|
|
"content": "Invalid number (integer) string."
|
|
|
|
};
|
|
|
|
}
|
|
|
|
}
|
|
|
|
},
|
2015-03-26 22:51:00 +01:00
|
|
|
{
|
|
|
|
"id": "base64encode",
|
|
|
|
"name": "Encode Base64",
|
|
|
|
"convert": function (input) {
|
|
|
|
return {
|
|
|
|
"status": "OK",
|
|
|
|
"content": btoa(input)
|
|
|
|
};
|
|
|
|
}
|
|
|
|
},
|
|
|
|
{
|
|
|
|
"id": "encodeuri",
|
|
|
|
"name": "Encode URI",
|
|
|
|
"convert": function (input) {
|
|
|
|
return {
|
|
|
|
"status": "OK",
|
|
|
|
"content": encodeURI(input).replace(/%5B/g, '[').replace(/%5D/g, ']')
|
|
|
|
};
|
|
|
|
}
|
|
|
|
},
|
|
|
|
{
|
|
|
|
"id": "encodeuricomponent",
|
|
|
|
"name": "Encode URI component",
|
|
|
|
"convert": function (input) {
|
|
|
|
return {
|
|
|
|
"status": "OK",
|
|
|
|
"content": encodeURIComponent(input).replace(/[!'()*]/g, function(c) {
|
|
|
|
return '%' + c.charCodeAt(0).toString(16);
|
|
|
|
})
|
|
|
|
};
|
|
|
|
}
|
|
|
|
},
|
|
|
|
{
|
|
|
|
"id": "encodehtmlentities",
|
|
|
|
"name": "Encode HTML entities",
|
|
|
|
"convert": function (input) {
|
|
|
|
return {
|
|
|
|
"status": "OK",
|
|
|
|
"content": input
|
|
|
|
.replace(/\&/g, "&")
|
|
|
|
.replace(/\</g, "<")
|
|
|
|
.replace(/\>/g, ">")
|
|
|
|
.replace(/\"/g, """)
|
|
|
|
};
|
|
|
|
}
|
2015-03-26 23:11:59 +01:00
|
|
|
},
|
|
|
|
{
|
|
|
|
"id": "encodequotedprintable",
|
|
|
|
"name": "Encode quoted printable",
|
|
|
|
"convert": function (input) {
|
|
|
|
return {
|
2015-03-26 23:33:58 +01:00
|
|
|
"status": "OK",
|
|
|
|
"content": quotedPrintable.encode(utf8.encode(input))
|
2015-03-26 23:11:59 +01:00
|
|
|
};
|
2015-03-26 23:33:58 +01:00
|
|
|
}
|
2016-08-15 17:50:23 +02:00
|
|
|
},
|
|
|
|
{
|
|
|
|
"id": "dectohex",
|
|
|
|
"name": "Encode decimal as hex",
|
|
|
|
"convert": function (input) {
|
|
|
|
try {
|
|
|
|
return {
|
|
|
|
"status": "OK",
|
|
|
|
"content": parseInt(input).toString(16)
|
|
|
|
};
|
|
|
|
} catch (exception) {
|
|
|
|
return {
|
|
|
|
"status": "ERROR",
|
|
|
|
"content": "Invalid number (integer) string."
|
|
|
|
};
|
|
|
|
}
|
|
|
|
}
|
|
|
|
},
|
|
|
|
{
|
|
|
|
"id": "dectobin",
|
|
|
|
"name": "Encode decimal as binary",
|
|
|
|
"convert": function (input) {
|
|
|
|
try {
|
|
|
|
return {
|
|
|
|
"status": "OK",
|
|
|
|
"content": parseInt(input).toString(2)
|
|
|
|
};
|
|
|
|
} catch (exception) {
|
|
|
|
return {
|
|
|
|
"status": "ERROR",
|
|
|
|
"content": "Invalid number (integer) string."
|
|
|
|
};
|
|
|
|
}
|
|
|
|
}
|
2015-03-25 19:54:07 +01:00
|
|
|
}
|
|
|
|
];
|
2015-03-25 20:20:40 +01:00
|
|
|
|
2015-03-25 19:54:07 +01:00
|
|
|
var optiontemplate = "<option name='{identifier}'>{name}</option>";
|
2015-03-26 17:26:17 +01:00
|
|
|
var template = "<div id='wrapper-{index}' class='wrapper'><textarea id='input-{index}' class='input' onchange='den.update(this);' placeholder='Please enter your input ...'>{content}</textarea>"
|
2015-03-25 19:28:33 +01:00
|
|
|
+ "<select id='type-{index}' class='conversion' onchange='den.convert(this);'>"
|
2015-03-25 19:54:07 +01:00
|
|
|
+ "{options}"
|
2015-03-25 19:28:33 +01:00
|
|
|
+ "</select></div>";
|
2015-03-25 19:54:07 +01:00
|
|
|
var options = "";
|
|
|
|
var i, plugin, option;
|
|
|
|
for (i = 0; i < plugins.length; i++) {
|
|
|
|
plugin = plugins[i];
|
|
|
|
option = optiontemplate.replace(/\{identifier\}/g, plugin.id).replace(/\{name\}/g, plugin.name);
|
2015-03-26 23:11:59 +01:00
|
|
|
if (plugin.disabled) {
|
|
|
|
option = $(option).attr("disabled", "disabled")[0].outerHTML;
|
|
|
|
}
|
2015-03-25 19:54:07 +01:00
|
|
|
options += option;
|
|
|
|
}
|
|
|
|
template = template.replace(/\{options\}/g, options);
|
|
|
|
|
2015-03-25 19:28:33 +01:00
|
|
|
$(document).ready(function() {
|
2015-03-26 17:26:17 +01:00
|
|
|
var $new = $(template.replace(/\{index\}/g, "0").replace(/\{content\}/g, ""));
|
2015-03-25 19:28:33 +01:00
|
|
|
$("body").append($new);
|
|
|
|
});
|
2015-03-25 19:54:07 +01:00
|
|
|
|
|
|
|
function getPluginById(id) {
|
|
|
|
for (i = 0; i < plugins.length; i++) {
|
|
|
|
if (plugins[i].id === id) {
|
|
|
|
return plugins[i];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
2015-03-25 19:28:33 +01:00
|
|
|
function convert(select) {
|
|
|
|
var $select = $(select);
|
|
|
|
var selectid = $select.attr("id");
|
|
|
|
var inputIndex = +selectid.split("-")[1];
|
|
|
|
var outputIndex = inputIndex + 1;
|
|
|
|
var $input = $("#input-" + inputIndex);
|
|
|
|
var $output = $("#input-" + outputIndex);
|
|
|
|
var appendNewOutput = false;
|
|
|
|
var input = $input.val();
|
|
|
|
var conversion = $select.find(":selected").attr("name");
|
2015-03-26 18:13:18 +01:00
|
|
|
var result;
|
|
|
|
var status;
|
2015-03-25 19:28:33 +01:00
|
|
|
var output;
|
2015-03-25 19:54:07 +01:00
|
|
|
var plugin;
|
2015-03-25 19:28:33 +01:00
|
|
|
if ($output.length == 0) {
|
|
|
|
appendNewOutput = true;
|
|
|
|
}
|
2015-03-25 19:54:07 +01:00
|
|
|
plugin = getPluginById(conversion);
|
|
|
|
if (plugin !== null) {
|
2015-03-26 18:13:18 +01:00
|
|
|
result = plugin.convert(input);
|
|
|
|
output = result.content;
|
|
|
|
status = result.status
|
2015-03-25 19:54:07 +01:00
|
|
|
} else {
|
2015-03-26 18:13:18 +01:00
|
|
|
output = "Internal error. Sorry.";
|
|
|
|
status = "ERROR";
|
2015-03-25 19:28:33 +01:00
|
|
|
}
|
|
|
|
if (appendNewOutput) {
|
2015-03-25 20:28:54 +01:00
|
|
|
if (output !== "") {
|
2015-03-26 22:06:54 +01:00
|
|
|
$output = $(template.replace(/\{index\}/g, "" + outputIndex).replace(/\{content\}/g, output.replace(/\&/g, "&")));
|
2015-03-26 18:13:18 +01:00
|
|
|
if (status === "ERROR") {
|
|
|
|
$output.find("textarea").addClass("error");
|
|
|
|
}
|
2015-03-25 20:28:54 +01:00
|
|
|
$("body").append($output);
|
|
|
|
}
|
2015-03-25 19:28:33 +01:00
|
|
|
} else {
|
2015-03-25 20:20:40 +01:00
|
|
|
$output.val(output);
|
2015-03-26 18:13:18 +01:00
|
|
|
if (status === "ERROR") {
|
|
|
|
$output.addClass("error");
|
|
|
|
} else {
|
|
|
|
$output.removeClass("error");
|
|
|
|
}
|
2015-03-25 20:20:40 +01:00
|
|
|
update($output);
|
2015-03-25 19:28:33 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-03-25 20:20:40 +01:00
|
|
|
function update(textarea) {
|
|
|
|
var $textarea = $(textarea);
|
|
|
|
var areaid = $textarea.attr("id");
|
|
|
|
var inputindex = +areaid.split("-")[1];
|
|
|
|
var $select = $("#type-" + inputindex);
|
|
|
|
var conversion = $select.find(":selected").attr("name");
|
|
|
|
var plugin = getPluginById(conversion);
|
|
|
|
if (plugin !== null) {
|
|
|
|
convert($select);
|
|
|
|
}
|
2015-03-25 19:28:33 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
var den = {};
|
|
|
|
den.convert = convert;
|
2015-03-25 20:20:40 +01:00
|
|
|
den.update = update;
|
2015-03-25 19:28:33 +01:00
|
|
|
window.den = window.den || den;
|
|
|
|
|
|
|
|
})(jQuery);
|