def javascript_alert(url, js_msg, abort_on, *, escape_msg=True): """Display a javascript alert.""" log.js.debug("alert: {}".format(js_msg)) if config.val.content.javascript.modal_dialog: raise CallSuper if not config.val.content.javascript.alert: return js_msg = html.escape(js_msg) if escape_msg else js_msg msg = 'From <b>{}</b>:<br/>{}'.format(html.escape(url.toDisplayString()), js_msg) urlstr = url.toString(QUrl.RemovePassword | QUrl.FullyEncoded) message.ask('Javascript alert', msg, mode=usertypes.PromptMode.alert, abort_on=abort_on, url=urlstr)
def feature_permission(url, option, msg, yes_action, no_action, abort_on, blocking=False): """Handle a feature permission request. Args: url: The URL the request was done for. option: An option name to check. msg: A string like "show notifications" yes_action: A callable to call if the request was approved no_action: A callable to call if the request was denied abort_on: A list of signals which interrupt the question. blocking: If True, ask a blocking question. Return: The Question object if a question was asked (and blocking=False), None otherwise. """ config_val = config.instance.get(option, url=url) if config_val == 'ask': if url.isValid(): urlstr = url.toString(QUrl.RemovePassword | QUrl.FullyEncoded) text = "Allow the website at <b>{}</b> to {}?".format( html.escape(url.toDisplayString()), msg) else: urlstr = None option = None # For message.ask/confirm_async text = "Allow the website to {}?".format(msg) if blocking: answer = message.ask(abort_on=abort_on, title='Permission request', text=text, url=urlstr, option=option, mode=usertypes.PromptMode.yesno) if answer: yes_action() else: no_action() return None else: return message.confirm_async( yes_action=yes_action, no_action=no_action, cancel_action=no_action, abort_on=abort_on, title='Permission request', text=text, url=urlstr, option=option) elif config_val: yes_action() return None else: no_action() return None
def ignore_certificate_errors(url, errors, abort_on): """Display a certificate error question. Args: url: The URL the errors happened in errors: A list of QSslErrors or QWebEngineCertificateErrors Return: True if the error should be ignored, False otherwise. """ ssl_strict = config.instance.get('content.ssl_strict', url=url) log.webview.debug("Certificate errors {!r}, strict {}".format( errors, ssl_strict)) for error in errors: assert error.is_overridable(), repr(error) if ssl_strict == 'ask': err_template = jinja.environment.from_string(""" Errors while loading <b>{{url.toDisplayString()}}</b>:<br/> <ul> {% for err in errors %} <li>{{err}}</li> {% endfor %} </ul> """.strip()) msg = err_template.render(url=url, errors=errors) urlstr = url.toString(QUrl.RemovePassword | QUrl.FullyEncoded) ignore = message.ask(title="Certificate errors - continue?", text=msg, mode=usertypes.PromptMode.yesno, default=False, abort_on=abort_on, url=urlstr) if ignore is None: # prompt aborted ignore = False return ignore elif ssl_strict is False: log.webview.debug("ssl_strict is False, only warning about errors") for err in errors: # FIXME we might want to use warn here (non-fatal error) # https://github.com/glimpsebrowser/glimpsebrowser/issues/114 message.error('Certificate error: {}'.format(err)) return True elif ssl_strict is True: return False else: raise ValueError("Invalid ssl_strict value {!r}".format(ssl_strict)) raise utils.Unreachable
def authentication_required(url, authenticator, abort_on): """Ask a prompt for an authentication question.""" realm = authenticator.realm() if realm: msg = '<b>{}</b> says:<br/>{}'.format( html.escape(url.toDisplayString()), html.escape(realm)) else: msg = '<b>{}</b> needs authentication'.format( html.escape(url.toDisplayString())) urlstr = url.toString(QUrl.RemovePassword | QUrl.FullyEncoded) answer = message.ask(title="Authentication required", text=msg, mode=usertypes.PromptMode.user_pwd, abort_on=abort_on, url=urlstr) if answer is not None: authenticator.setUser(answer.user) authenticator.setPassword(answer.password) return answer
def on_proxy_authentication_required(self, proxy, authenticator): """Called when a proxy needs authentication.""" proxy_id = ProxyId(proxy.type(), proxy.hostName(), proxy.port()) if proxy_id in _proxy_auth_cache: authinfo = _proxy_auth_cache[proxy_id] authenticator.setUser(authinfo.user) authenticator.setPassword(authinfo.password) else: msg = '<b>{}</b> says:<br/>{}'.format( html.escape(proxy.hostName()), html.escape(authenticator.realm())) abort_on = self._get_abort_signals() answer = message.ask(title="Proxy authentication required", text=msg, mode=usertypes.PromptMode.user_pwd, abort_on=abort_on) if answer is not None: authenticator.setUser(answer.user) authenticator.setPassword(answer.password) _proxy_auth_cache[proxy_id] = answer
def closeEvent(self, e): """Override closeEvent to display a confirmation if needed.""" if crashsignal.is_crashing: e.accept() return tab_count = self.tabbed_browser.widget.count() download_model = objreg.get('download-model', scope='window', window=self.win_id) download_count = download_model.running_downloads() quit_texts = [] # Ask if multiple-tabs are open if 'multiple-tabs' in config.val.confirm_quit and tab_count > 1: quit_texts.append("{} tabs are open.".format(tab_count)) # Ask if multiple downloads running if 'downloads' in config.val.confirm_quit and download_count > 0: quit_texts.append("{} {} running.".format( download_count, "download is" if download_count == 1 else "downloads are")) # Process all quit messages that user must confirm if quit_texts or 'always' in config.val.confirm_quit: msg = jinja.environment.from_string(""" <ul> {% for text in quit_texts %} <li>{{text}}</li> {% endfor %} </ul> """.strip()).render(quit_texts=quit_texts) confirmed = message.ask('Really quit?', msg, mode=usertypes.PromptMode.yesno, default=True) # Stop asking if the user cancels if not confirmed: log.destroy.debug("Cancelling closing of window {}".format( self.win_id)) e.ignore() return e.accept() self._do_close()
def javascript_prompt(url, js_msg, default, abort_on, *, escape_msg=True): """Display a javascript prompt.""" log.js.debug("prompt: {}".format(js_msg)) if config.val.content.javascript.modal_dialog: raise CallSuper if not config.val.content.javascript.prompt: return (False, "") js_msg = html.escape(js_msg) if escape_msg else js_msg msg = '<b>{}</b> asks:<br/>{}'.format(html.escape(url.toDisplayString()), js_msg) urlstr = url.toString(QUrl.RemovePassword | QUrl.FullyEncoded) answer = message.ask('Javascript prompt', msg, mode=usertypes.PromptMode.text, default=default, abort_on=abort_on, url=urlstr) if answer is None: return (False, "") else: return (True, answer)