def dispatch_event(self, event: str, bubbles: bool = False, cancelable: bool = False, composed: bool = False) -> None: self._check_vanished() log.webelem.debug("Firing event on {!r} via javascript.".format(self)) self._elem.evaluateJavaScript( "this.dispatchEvent(new Event({}, " "{{'bubbles': {}, 'cancelable': {}, 'composed': {}}}))".format( javascript.to_js(event), javascript.to_js(bubbles), javascript.to_js(cancelable), javascript.to_js(composed)))
def insert_text(self, text: str) -> None: self._check_vanished() if not self.is_editable(strict=True): raise webelem.Error("Element is not editable!") log.webelem.debug("Inserting text into element {!r}".format(self)) self._elem.evaluateJavaScript(""" var text = {}; var event = document.createEvent("TextEvent"); event.initTextEvent("textInput", true, true, null, text); this.dispatchEvent(event); """.format(javascript.to_js(text)))
def check_set(self, value, css_style="background-color", document_element="document.body"): """Check whether the css in ELEMENT is set to VALUE.""" self.js.run( "console.log({document});" "window.getComputedStyle({document}, null)" ".getPropertyValue({prop});".format( document=document_element, prop=javascript.to_js(css_style)), value)
def set_value(self, value: webelem.JsValueType) -> None: self._check_vanished() if self._tab.is_deleted(): raise webelem.OrphanedError("Tab containing element vanished") if self.is_content_editable(): log.webelem.debug("Filling {!r} via set_text.".format(self)) assert isinstance(value, str) self._elem.setPlainText(value) else: log.webelem.debug("Filling {!r} via javascript.".format(self)) value = javascript.to_js(value) self._elem.evaluateJavaScript("this.value={}".format(value))
def _generate_pdfjs_script(filename): """Generate the script that shows the pdf with pdf.js. Args: filename: The name of the file to open. """ url = QUrl('glimpse://pdfjs/file') url_query = QUrlQuery() url_query.addQueryItem('filename', filename) url.setQuery(url_query) return jinja.js_environment.from_string(""" document.addEventListener("DOMContentLoaded", function() { if (typeof window.PDFJS !== 'undefined') { // v1.x {% if disable_create_object_url %} window.PDFJS.disableCreateObjectURL = true; {% endif %} window.PDFJS.verbosity = window.PDFJS.VERBOSITY_LEVELS.info; } else { // v2.x const options = window.PDFViewerApplicationOptions; {% if disable_create_object_url %} options.set('disableCreateObjectURL', true); {% endif %} options.set('verbosity', pdfjsLib.VerbosityLevel.INFOS); } const viewer = window.PDFView || window.PDFViewerApplication; viewer.open({{ url }}); }); """).render( url=javascript.to_js(url.toString(QUrl.FullyEncoded)), # WORKAROUND for https://bugreports.qt.io/browse/QTBUG-70420 disable_create_object_url=( not qtutils.version_check('5.12') and not qtutils.version_check('5.7.1', exact=True, compiled=False) and objects.backend == usertypes.Backend.QtWebEngine))
def test_to_js(arg, expected): if expected is TypeError: with pytest.raises(TypeError): javascript.to_js(arg) else: assert javascript.to_js(arg) == expected