org.mozilla.javascript.Context.initSafeStandardObjects()方法的使用及代码示例

x33g5p2x  于2022-01-18 转载在 JavaScript  
字(4.9k)|赞(0)|评价(0)|浏览(133)

本文整理了Java中org.mozilla.javascript.Context.initSafeStandardObjects()方法的一些代码示例,展示了Context.initSafeStandardObjects()的具体用法。这些代码示例主要来源于Github/Stackoverflow/Maven等平台,是从一些精选项目中提取出来的代码,具有较强的参考意义,能在一定程度帮忙到你。Context.initSafeStandardObjects()方法的具体详情如下:
包路径:org.mozilla.javascript.Context
类名称:Context
方法名:initSafeStandardObjects

Context.initSafeStandardObjects介绍

[英]Initialize the standard objects, leaving out those that offer access directly to Java classes. This sets up "scope" to have access to all the standard JavaScript classes, but does not create global objects for any top-level Java packages. In addition, the "Packages," "JavaAdapter," and "JavaImporter" classes, and the "getClass" function, are not initialized. The result of this function is a scope that may be safely used in a "sandbox" environment where it is not desirable to give access to Java code from JavaScript. Creates instances of the standard objects and their constructors (Object, String, Number, Date, etc.), setting up 'scope' to act as a global object as in ECMA 15.1.

This method must be called to initialize a scope before scripts can be evaluated in that scope.

This method does not affect the Context it is called upon.
[中]初始化标准对象,忽略那些提供直接访问Java类的对象。这将设置“scope”以访问所有标准JavaScript类,但不会为任何顶级Java包创建全局对象。此外,“包”、“JavaAdapter”和“JavaImporter”类以及“getClass”函数都没有初始化。此函数的结果是一个作用域,可以在“沙盒”环境中安全使用,在该环境中,不希望从JavaScript访问Java代码。创建标准对象及其构造函数(对象、字符串、数字、日期等)的实例,将“范围”设置为ECMA15.1中的全局对象。
必须调用此方法来初始化作用域,然后才能在该作用域中计算脚本。
此方法不影响调用它的上下文。

代码示例

代码示例来源:origin: xtuhcy/gecco

@Override
@SuppressWarnings({ "unchecked" })
public void render(HttpRequest request, HttpResponse response, BeanMap beanMap, SpiderBean bean) {
  Context cx = Context.enter();
  ScriptableObject scope = cx.initSafeStandardObjects();
  String windowScript = "var window = {};var document = {};";
  cx.evaluateString(scope, windowScript, "window", 1, null);
  HtmlParser parser = new HtmlParser(request.getUrl(), response.getContent());
  for (Element ele : parser.$("script")) {
    String sc = ele.html();
    if (StringUtils.isNotEmpty(sc)) {
      try {
        cx.evaluateString(scope, sc, "", 1, null);
      } catch (Exception ex) {
        // ex.printStackTrace();
      }
    }
  }
  Map<String, Object> fieldMap = new HashMap<String, Object>();
  Set<Field> jsVarFields = ReflectionUtils.getAllFields(bean.getClass(), ReflectionUtils.withAnnotation(JSVar.class));
  for (Field jsVarField : jsVarFields) {
    Object value = injectJsVarField(request, beanMap, jsVarField, cx, scope);
    if(value != null) {
      fieldMap.put(jsVarField.getName(), value);
    }
  }
  beanMap.putAll(fieldMap);
  Context.exit();
}

代码示例来源:origin: geogebra/geogebra

/**
 * Initialize the standard objects, leaving out those that offer access directly
 * to Java classes. This sets up "scope" to have access to all the standard
 * JavaScript classes, but does not create global objects for any top-level
 * Java packages. In addition, the "Packages," "JavaAdapter," and
 * "JavaImporter" classes, and the "getClass" function, are not
 * initialized.
 *
 * The result of this function is a scope that may be safely used in a "sandbox"
 * environment where it is not desirable to give access to Java code from JavaScript.
 *
 * Creates instances of the standard objects and their constructors
 * (Object, String, Number, Date, etc.), setting up 'scope' to act
 * as a global object as in ECMA 15.1.<p>
 *
 * This method must be called to initialize a scope before scripts
 * can be evaluated in that scope.<p>
 *
 * This method does not affect the Context it is called upon.
 *
 * @return the initialized scope
 */
public final ScriptableObject initSafeStandardObjects()
{
  return initSafeStandardObjects(null, false);
}

代码示例来源:origin: geogebra/geogebra

/**
 * Initialize the standard objects, leaving out those that offer access directly
 * to Java classes. This sets up "scope" to have access to all the standard
 * JavaScript classes, but does not create global objects for any top-level
 * Java packages. In addition, the "Packages," "JavaAdapter," and
 * "JavaImporter" classes, and the "getClass" function, are not
 * initialized.
 *
 * The result of this function is a scope that may be safely used in a "sandbox"
 * environment where it is not desirable to give access to Java code from JavaScript.
 *
 * Creates instances of the standard objects and their constructors
 * (Object, String, Number, Date, etc.), setting up 'scope' to act
 * as a global object as in ECMA 15.1.<p>
 *
 * This method must be called to initialize a scope before scripts
 * can be evaluated in that scope.<p>
 *
 * This method does not affect the Context it is called upon.
 *
 * @param scope the scope to initialize, or null, in which case a new
 *        object will be created to serve as the scope
 * @return the initialized scope. The method returns the value of the scope
 *         argument if it is not null or newly allocated scope object which
 *         is an instance {@link ScriptableObject}.
 */
public final Scriptable initSafeStandardObjects(ScriptableObject scope)
{
  return initSafeStandardObjects(scope, false);
}

代码示例来源:origin: org.eclipse.ditto/ditto-services-connectivity-mapping

final Scriptable scope = cx.initSafeStandardObjects(); // that one disables "print, exit, quit", etc.
initLibraries(cx, scope);
return scope;

代码示例来源:origin: eclipse/ditto

final Scriptable scope = cx.initSafeStandardObjects(); // that one disables "print, exit, quit", etc.
initLibraries(cx, scope);
return scope;

相关文章

微信公众号

Context类方法