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, error_repr: e.toString()}
            }
        })(%(script_text)s)
        """ % dict(script_text=escaped)
        res = qt2py(frame.evaluateJavaScript(wrapped))
        if res.get("error", False):
            raise JsError(res.get("error_repr", "unknown JS error"))
        return res.get("result", None)
Ejemplo n.º 2
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, error_repr: e.toString()}
            }
        })(%(script_text)s)
        """ % dict(script_text=escaped)
        res = qt2py(frame.evaluateJavaScript(wrapped))
        if res.get("error", False):
            raise JsError(res.get("error_repr", "unknown JS error"))
        return res.get("result", None)
Ejemplo n.º 3
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.º 4
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)[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.º 5
0
    def resume(self, value=None):
        if self._used_up:
            raise OneShotCallbackError("resume() called on a one shot"
                                       " callback that was already used up.")

        self.use_up()
        self._callback(qt2py(value))
Ejemplo n.º 6
0
    def resume(self, value=None):
        if self._used_up:
            raise OneShotCallbackError("resume() called on a one shot" \
                                       " callback that was already used up.")

        self._use_up()
        self._callback(qt2py(value))
Ejemplo n.º 7
0
 def runjs(self, js_source):
     """
     Run JS code in page context and return the result.
     Only string results are supported.
     """
     frame = self.web_page.mainFrame()
     res = frame.evaluateJavaScript(js_source)
     return qt2py(res)
Ejemplo n.º 8
0
 def runjs(self, js_source):
     """
     Run JS code in page context and return the result.
     Only string results are supported.
     """
     frame = self.web_page.mainFrame()
     res = frame.evaluateJavaScript(js_source)
     return qt2py(res)
Ejemplo n.º 9
0
    def resume(self, value=None):
        if self._used_up:
            self.logger.log(
                "warning: resume() called on a one shot callback "
                "that was already used up.",
                min_level=1)
            return

        self.use_up()
        self._callback(qt2py(value))
Ejemplo n.º 10
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)
Ejemplo n.º 11
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)
Ejemplo n.º 12
0
    def evaljs(self,
               js_source,
               handle_errors=True,
               result_protection=True,
               dom_elements=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.

        When `dom_elements` is True (default) top-level DOM elements will be
        saved in JS field of window object under `self._elements_storage.name`
        key. The result of evaluation will be object with `type` property and
        `id` property. In JS the original DOM element can accessed through
        ``window[self._elements_storage.name][id]``.
        """
        frame = self.web_page.mainFrame()
        eval_expr = u"eval({})".format(escape_js(js_source))

        if dom_elements:
            self._init_js_objects_storage()
            eval_expr = store_dom_elements(eval_expr,
                                           self._elements_storage.name)
        if result_protection:
            eval_expr = get_sanitized_result_js(eval_expr)

        if handle_errors:
            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)
                })

            result = res.get("result", None)
        else:
            result = qt2py(frame.evaluateJavaScript(eval_expr))

        return self._process_js_result(result, allow_dom=dom_elements)