diff --git a/dencoder.js b/dencoder.js
index 1699363..aa6c689 100644
--- a/dencoder.js
+++ b/dencoder.js
@@ -1,15 +1,89 @@
(function($) {
+ var plugins = [
+ {
+ "id": "CHOOSE",
+ "name": "Please choose your conversion ...",
+ "convert": function(input) {
+ return "";
+ }
+ },
+ {
+ "id": "base64encode",
+ "name": "Base64 encode",
+ "convert": function(input) {
+ return btoa(input);
+ }
+ },
+ {
+ "id": "base64decode",
+ "name": "Base64 decode",
+ "convert": function(input) {
+ try {
+ return atob(input);
+ } catch (exception) {
+ return "Invalid base64 input string.";
+ }
+ }
+ },
+ {
+ "id": "encodeuri",
+ "name": "Encode URI",
+ "convert": function(input) {
+ return encodeURI(input).replace(/%5B/g, '[').replace(/%5D/g, ']');
+ }
+ },
+ {
+ "id": "decodeuri",
+ "name": "Decode URI",
+ "convert": function(input) {
+ return decodeURI(input)
+ }
+ },
+ {
+ "id": "encodeuricomponent",
+ "name": "Encode URI component",
+ "convert": function(input) {
+ return encodeURIComponent(input).replace(/[!'()*]/g, function(c) {
+ return '%' + c.charCodeAt(0).toString(16);
+ });
+ }
+ },
+ {
+ "id": "decodeuricomponent",
+ "name": "Decode URI component",
+ "convert": function(input) {
+ return decodeURIComponent(input)
+ }
+ }
+ ];
+ var optiontemplate = "";
var template = "
"
+ "
";
+ 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);
+ options += option;
+ }
+ template = template.replace(/\{options\}/g, options);
+
$(document).ready(function() {
var $new = $(template.replace(/\{index\}/g, "0").replace(/\{content\}/g, "Please enter your input here."));
$("body").append($new);
});
-
+
+ function getPluginById(id) {
+ for (i = 0; i < plugins.length; i++) {
+ if (plugins[i].id === id) {
+ return plugins[i];
+ }
+ }
+ return null;
+ }
+
function convert(select) {
var $select = $(select);
var selectid = $select.attr("id");
@@ -21,23 +95,15 @@
var input = $input.val();
var conversion = $select.find(":selected").attr("name");
var output;
+ var plugin;
if ($output.length == 0) {
appendNewOutput = true;
}
- switch (conversion) {
- case "CHOOSE":
- output = "";
- break;
- case "base64encode":
- output = btoa(input);
- break;
- case "base64decode":
- output = atob(input);
- break;
- default:
- alert("Unknown: " + conversion);
- output = "ERROR!";
- break;
+ plugin = getPluginById(conversion);
+ if (plugin !== null) {
+ output = plugin.convert(input);
+ } else {
+ output = "internal error. Sorry.";
}
if (appendNewOutput) {
$output = $(template.replace(/\{index\}/g, "" + outputIndex).replace(/\{content\}/g, output));