Ejemplo n.º 1
0
    def evaljs(self, js_source, handle_errors=True):
        """
        Run JS code in page context and return the result.

        If JavaScript exception or an syntax error happens
        and :param:`handle_errors` is True then Python JsError
        exception is raised.
        """
        frame = self.web_page.mainFrame()
        if not handle_errors:
            return qt2py(frame.evaluateJavaScript(js_source))

        escaped = json.dumps([js_source], ensure_ascii=False,
                             encoding='utf8')[1:-1]
        wrapped = """
        (function(script_text){
            try{
                return {error: false, result: eval(script_text)}
            }
            catch(e){
                return {
                    error: true,
                    errorType: e.name,
                    errorMessage: e.message,
                    errorRepr: e.toString(),
                }
            }
        })(%(script_text)s)
        """ % dict(script_text=escaped)
        res = qt2py(frame.evaluateJavaScript(wrapped))

        if not isinstance(res, dict):
            raise JsError({
                'type': "unknown",
                'js_error_message': res,
                'message': "unknown JS error: {!r}".format(res)
            })

        if res.get("error", False):
            err_message = res.get('errorMessage')
            err_type = res.get('errorType', '<custom JS error>')
            err_repr = res.get('errorRepr', "<unknown JS error>")
            if err_message is None:
                err_message = err_repr
            raise JsError({
                'type': 'js_error',
                'js_error_type': err_type,
                'js_error_message': err_message,
                'js_error': err_repr,
                'message': "JS error: {!r}".format(err_repr)
            })

        return res.get("result", None)
Ejemplo n.º 2
0
    def evaljs(self, js_source, handle_errors=True, result_protection=True):
        """
        Run JS code in page context and return the result.

        If JavaScript exception or an syntax error happens
        and `handle_errors` is True then Python JsError
        exception is raised.

        When `result_protection` is True (default) protection against
        badly written or malicious scripts is activated. Disable it
        when the script result is known to be good, i.e. it only
        contains objects/arrays/primitives without circular references.
        """
        frame = self.web_page.mainFrame()
        eval_expr = u"eval({})".format(escape_js(js_source))

        if result_protection:
            eval_expr = get_sanitized_result_js(eval_expr)

        if not handle_errors:
            return qt2py(frame.evaluateJavaScript(eval_expr))

        res = frame.evaluateJavaScript(get_process_errors_js(eval_expr))

        if not isinstance(res, dict):
            raise JsError({
                'type': ScriptError.UNKNOWN_ERROR,
                'js_error_message': res,
                'message': "unknown JS error: {!r}".format(res)
            })

        if res.get("error", False):
            err_message = res.get('errorMessage')
            err_type = res.get('errorType', '<custom JS error>')
            err_repr = res.get('errorRepr', '<unknown JS error>')
            if err_message is None:
                err_message = err_repr
            raise JsError({
                'type': ScriptError.JS_ERROR,
                'js_error_type': err_type,
                'js_error_message': err_message,
                'js_error': err_repr,
                'message': "JS error: {!r}".format(err_repr)
            })

        return res.get("result", None)