0

I have some code (whatever is inside the body of the jsToBeConverted function below) that I want to be able to convert it into text so that the user can copy it and then paste it inside a <script> element on their site in order to have it executed.

someElement.onclick = function ()
{
    var textForUserToCopy = (function ()
    {
      x = document.getElementById('someInput').value;

      var jsToBeConverted = function ()
      {
        var y = x + 6;

        var z = function (a)
        {
          return (a + 5);
        };

         window.d = z(y);
      };

      var jsToExecute = "(" + jsToBeConverted + ")();"

      return jsToExecute;
    })();
};

However this would generate the following text :

"(function ()
 { 
   var y = x + 6; 

   var z = function (a)
   {
     return (a + 5);
   };

   window.d = z(y);
 })();"

But this isn't what I want. If the above is evaluated x will obviously be undefined. What I want is for x to be replaced by its value at the time and then have the js converted into text. So if the value of x happened to be 123, this is what the generated text should look like :

"(function ()
 {
   var y = 6 + 6; 

   var z = function (a)
   {
     return (a + 5);
   };

   window.d = z(y);
 })();"

If the above string is evaluated - window.d would be 17

Is the there some standard function/process for generating/converting dynamic code into text?

Thank you

The Random Guy
  • 321
  • 2
  • 4
  • 10
  • Why do you want to do this? This seems like a textbook example of the [XY Problem](http://meta.stackexchange.com/q/66377/180276). – tckmn Feb 09 '14 at 19:52
  • I'm building an app that takes some user input and based on that generates some code for the user to paste inside a ` – The Random Guy Feb 09 '14 at 19:55
  • Will `x` always be a number, or can it be a different type? If so, what types could it be? – tckmn Feb 09 '14 at 19:56
  • In thsi case it can only be a number - but this is just an example. The actual code to convert might 10 times as long and the number of dynamic values can be equally large as well as their types. – The Random Guy Feb 09 '14 at 20:00
  • Well, JS doesn't have a generic "uneval" or "inspect," so you're going to have to manually handle each type... (see the answer) – tckmn Feb 09 '14 at 20:01
  • If you really want to do this, you have to generate an AST from the code, create some logic to look up `x` in the environment and replace it appropriately. Have a look at http://esprima.org/ and https://github.com/benjamn/recast. – Felix Kling Feb 09 '14 at 20:09

1 Answers1

0

You can simply concatenate.

e.g.:

"function (x) { return " + generated_value + " + x }";

For a more elegant looking solution, see string formatting: JavaScript equivalent to printf/string.format

Community
  • 1
  • 1
Brandon
  • 1,336
  • 3
  • 10
  • 38
  • thanks I'll take a look at sprintf - is this standard Js supported by all browsers?? – The Random Guy Feb 09 '14 at 20:01
  • I don't think so. You may have to write the function yourself, or use an already implemented solution. I like this one in particular: http://stackoverflow.com/a/4974284/828709 – Brandon Feb 09 '14 at 20:39