mozilla

Compare Revisions

Components.utils.evalInSandbox

Change Revisions

Revision 101249:

Revision 101249 by patilkr on

Revision 377149:

Revision 377149 by evilpie on

Title:
Components.utils.evalInSandbox
Components.utils.evalInSandbox
Slug:
Components.utils.evalInSandbox
Components.utils.evalInSandbox
Tags:
JavaScript, Extensions, Add-ons, XPConnect, "Developing Mozilla", "XPCOM:Language Bindings"
"JavaScript", "Extensions", "Developing Mozilla", "Add-ons", "XPCOM:Language Bindings", "XPConnect"
Content:

Revision 101249
Revision 377149
n98var x = Components.utils.evalInSandbox("let x = 1;", sandbox, "1.n98var x = Components.utils.evalInSandbox("let x = 1;", sandbox, "1.
>8", "<a class=" external" href="http://foo.com/mycode.js" rel="fr>8", "<a class="external" href="http://foo.com/mycode.js" rel="fre
>eelink">http://foo.com/mycode.js</a>", 25);>elink">http://foo.com/mycode.js</a>", 25);
tt109    <div class="note">
109    <p>110      <p>
110      Using a method <code>importFunction()</code>, it's possible111        <strong>Note:</strong> Using the method <code>importFunct
> to securely execute a chrome function into the Sandbox Context.>ion()</code> is no longer necessary. See using <a href="/en-US/do
 >cs/Components.utils.Sandbox#Importing_functions_or_objects_into_t
 >he_Sandbox" title="/en-US/docs/Components.utils.Sandbox#Importing
 >_functions_or_objects_into_the_Sandbox">Importing functions or ob
 >jects into the Sandbox</a>.
111    </p>112      </p>
112    <p>
113      For example:
114    </p>113    </div>
115    <pre class="eval">
116function chromeFunction() {
117  return 10;
118}
119 
120// create new sandbox instance
121var sandbox = new Components.utils.Sandbox("<span class="nowiki">
>http://www.example.com/</span>"); 
122 
123// script to execute in the Sandbox
124var scriptText = "var x = 2 + chromeFunction(); x + 3";
125 
126// import chrome function into sandbox context
127sandbox.importFunction(chromeFunction);
128 
129// evaluate script into sandbox context
130var result = Components.utils.evalInSandbox(scriptText, sandbox);
131// result = 15, sandbox.x is 12
132</pre>

Back to History