Skip to content

Instantly share code, notes, and snippets.

@niisar
Last active August 29, 2015 14:06
Show Gist options
  • Save niisar/67cd26a132106a67a771 to your computer and use it in GitHub Desktop.
Save niisar/67cd26a132106a67a771 to your computer and use it in GitHub Desktop.
Text to Javascript
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>Text to Javascript</title>
<script type="text/javascript">
var oneClick = false, oneMoreClick = false;
function convIt(OoB) {
//check if the text is supplided for conversion or not.
if (!OoB.toEnc.value) { window.alert('You have not supplied any code for me to convert.\nPut some HTML or JavaScript in the large text box.'); OoB.isEnc.value = 'Resulting code will be displayed here'; return; }
//check to variable is currect or not.
// \W = Find a non-word character, \w = Find a word character
if (OoB.oName.value.replace(/\W/, '') != OoB.oName.value || !OoB.oName.value) { window.alert('The variable name you have chosen is invalid.\nIt may only contain characters a-z, A-Z, 0-9 and _'); return; }
var tempVal = OoB.toEnc.value, tempNm = OoB.oName.value;
var fromAr = new Array(/\\/g, /'/g, /"/g, /\r\n/g, /[\r\n]/g, /\t/g, new RegExp('--' + '>', 'g'), new RegExp('<!' + '--', 'g'), /\//g), toAr = new Array('\\\\', '\\\'', '\\\"', '\\n', '\\n', '\\t', '--\'+\'>', '<!\'+\'--', '\\\/');
for (var x = 0; x < fromAr.length; x++) {
tempVal = tempVal.replace(fromAr[x], toAr[x]);
}
var quoteStyle = (OoB.whatQuote[0].checked ? '\'' : '\"');
tempVal = 'var ' + tempNm + ' = ' + quoteStyle + tempVal + quoteStyle + ';';
if (OoB.isHTML.checked) {
tempVal = tempVal.replace(/&/g, '&amp;').replace(/<{1}/g, '&lt;').replace(/>/g, '&gt;').replace(/"/g, '&quot;');
if (OoB.andSingle.checked) { tempVal = tempVal.replace(/'/g, '&#39;'); }
}
OoB.isEnc.value = tempVal; OoB.isEnc.focus(); OoB.isEnc.select();
}
</script>
</head>
<body>
<form method="post" action="" onsubmit="return false;">
<p>
<textarea class="almostfull" wrap="off" rows="12" cols="80" name="toEnc" onfocus="if(!oneClick) { this.value='';} oneClick = true;">Put in HTML and JavaScript code in here</textarea></p>
<fieldset>
<legend>Configuration</legend>
<p>
<label for="oName">
What do you want your variable to be called?</label>
<input class="box" type="text" id="oName" name="oName" size="15" value="myVariable"
onfocus="if(!oneMoreClick) { this.value='';} oneMoreClick = true;"></p>
<fieldset>
<legend>What type of quotes do you want to use:</legend>
<ul>
<li>
<input type="radio" id="whatQuote1" name="whatQuote" value="Yes" checked>
<label for="whatQuote2">
Single '</label></li>
<li>
<input type="radio" id="whatQuote2" name="whatQuote" value="Yes">
<label for="whatQuote2">
Double &quot;</label></li>
</ul>
</fieldset>
<fieldset>
<legend>HTML encoding:</legend>
<ul>
<li>
<input type="checkbox" id="isHTML" name="isHTML" value="Yes" onclick="if(document.getElementById){document.getElementById('goHide').style.display=(this.checked?'':'none');}">
<label for="isHTML">
Make this HTML safe (so it can be written inside HTML attributes)</label>
[<a href="javascript:alert('Scripts written within HTML attributes or displayed on the page as a code sample must have certain characters converted into HTML entities.\n\nThis includes: &lt; &gt; &quot; &amp;\n\nTick this box if you want this script to automatically convert these for you.\n\nThis will work for HTML attributes that use the standard &quot; quotes. If you want to use the less common \' quotes as your attribute delimiters, you will need to enable the option to replace all \' characters with the &amp;#39; entity.\n\nWARNING: some browsers can only hold upto 4KB in attributes!');">Info</a>]</li>
<li id="goHide">
<input type="checkbox" id="andSingle" name="andSingle" value="Yes">
<label for="andSingle">
Also convert ' to &amp;#39; (for use with HTML attributes that use ' as delimiters)</label></li>
</ul>
</fieldset>
</fieldset>
<p>
<input type="button" value="Convert" onclick="this.form.isEnc.value='Converting, please wait . . .';convIt(this.form);"></p>
<p>
<input class="almostfull" type="text" name="isEnc" size="70" value="Resulting code will be displayed here"
onfocus="this.select();"></p>
</form>
<script type="text/javascript">
if (document.getElementById) { document.getElementById('goHide').style.display = 'none'; }
</script>
</body>
</html>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment