diff --git a/doc/help/settings.asciidoc b/doc/help/settings.asciidoc index 418783a31..f64c82f6d 100644 --- a/doc/help/settings.asciidoc +++ b/doc/help/settings.asciidoc @@ -125,7 +125,7 @@ |<>|Whether JavaScript can close tabs. |<>|Whether JavaScript can open new tabs without user interaction. |<>|Enables or disables JavaScript. -|<>|How to log JavaScript console messages. +|<>|Log levels to use for JavaScript console logging messages. |<>|Use the standard JavaScript modal dialog for `alert()` and `confirm()` |<>|Show javascript prompts. |<>|Whether locally loaded documents are allowed to access other local urls. @@ -1466,15 +1466,15 @@ Default: +pass:[true]+ [[content.javascript.log]] == content.javascript.log -How to log JavaScript console messages. +Log levels to use for JavaScript console logging messages. +On QtWebKit, the "unknown" setting is always used. -Valid values: +Default: - * +none+: Don't log messages. - * +debug+: Log messages with debug level. - * +info+: Log messages with info level. - -Default: +pass:[debug]+ +- +pass:[error]+: +pass:[debug]+ +- +pass:[info]+: +pass:[debug]+ +- +pass:[unknown]+: +pass:[debug]+ +- +pass:[warning]+: +pass:[debug]+ [[content.javascript.modal_dialog]] == content.javascript.modal_dialog diff --git a/qutebrowser/browser/shared.py b/qutebrowser/browser/shared.py index 0729bda5a..cfa1e98da 100644 --- a/qutebrowser/browser/shared.py +++ b/qutebrowser/browser/shared.py @@ -119,6 +119,22 @@ def javascript_alert(url, js_msg, abort_on): abort_on=abort_on) +def javascript_log_message(level, source, line, msg): + """Display a JavaScript log message.""" + logstring = "[{}:{}] {}".format(source, line, msg) + # Needs to line up with the values allowed for the + # content.javascript.log setting. + logmap = { + 'none': lambda arg: None, + 'debug': log.js.debug, + 'info': log.js.info, + 'warning': log.js.warning, + 'error': log.js.error, + } + logger = logmap[config.val.content.javascript.log[level.name]] + logger(logstring) + + def ignore_certificate_errors(url, errors, abort_on): """Display a certificate error question. diff --git a/qutebrowser/browser/webengine/webview.py b/qutebrowser/browser/webengine/webview.py index 60ea06914..56bd1eb5a 100644 --- a/qutebrowser/browser/webengine/webview.py +++ b/qutebrowser/browser/webengine/webview.py @@ -274,18 +274,12 @@ class WebEnginePage(QWebEnginePage): def javaScriptConsoleMessage(self, level, msg, line, source): """Log javascript messages to qutebrowser's log.""" - # FIXME:qtwebengine maybe unify this in the tab api somehow? - if config.val.content.javascript.log == 'none': - return - - level_to_logger = { - QWebEnginePage.InfoMessageLevel: log.js.info, - QWebEnginePage.WarningMessageLevel: log.js.warning, - QWebEnginePage.ErrorMessageLevel: log.js.error, + level_map = { + QWebEnginePage.InfoMessageLevel: usertypes.JsLogLevel.info, + QWebEnginePage.WarningMessageLevel: usertypes.JsLogLevel.warning, + QWebEnginePage.ErrorMessageLevel: usertypes.JsLogLevel.error, } - logstring = "[{}:{}] {}".format(source, line, msg) - logger = level_to_logger[level] - logger(logstring) + shared.javascript_log_message(level_map[level], source, line, msg) def acceptNavigationRequest(self, url: QUrl, diff --git a/qutebrowser/browser/webkit/webpage.py b/qutebrowser/browser/webkit/webpage.py index f985ab228..c20952593 100644 --- a/qutebrowser/browser/webkit/webpage.py +++ b/qutebrowser/browser/webkit/webpage.py @@ -446,14 +446,8 @@ class BrowserPage(QWebPage): def javaScriptConsoleMessage(self, msg, line, source): """Override javaScriptConsoleMessage to use debug log.""" - logstring = "[{}:{}] {}".format(source, line, msg) - logmap = { - 'debug': log.js.debug, - 'info': log.js.info, - 'none': lambda arg: None - } - logger = logmap[config.val.content.javascript.log] - logger(logstring) + shared.javascript_log_message(usertypes.JsLogLevel.unknown, + source, line, msg) def acceptNavigationRequest(self, _frame: QWebFrame, diff --git a/qutebrowser/config/configdata.yml b/qutebrowser/config/configdata.yml index 912774876..ac7a59fde 100644 --- a/qutebrowser/config/configdata.yml +++ b/qutebrowser/config/configdata.yml @@ -388,13 +388,26 @@ content.javascript.enabled: content.javascript.log: type: - name: String - valid_values: - - none: "Don't log messages." - - debug: Log messages with debug level. - - info: Log messages with info level. - default: debug - desc: How to log JavaScript console messages. + name: Dict + fixed_keys: ['unknown', 'info', 'warning', 'error'] + keytype: String + valtype: + name: String + valid_values: + - none: "Don't log messages." + - debug: Log messages with debug level. + - info: Log messages with info level. + - warning: Log messages with warning level. + - error: Log messages with error level. + default: + unknown: debug + info: debug + warning: debug + error: debug + desc: >- + Log levels to use for JavaScript console logging messages. + + On QtWebKit, the "unknown" setting is always used. content.javascript.modal_dialog: type: Bool diff --git a/qutebrowser/utils/usertypes.py b/qutebrowser/utils/usertypes.py index 31f2f79cb..d57566e57 100644 --- a/qutebrowser/utils/usertypes.py +++ b/qutebrowser/utils/usertypes.py @@ -254,6 +254,11 @@ Backend = enum('Backend', ['QtWebKit', 'QtWebEngine']) JsWorld = enum('JsWorld', ['main', 'application', 'user', 'jseval']) +# Log level of a JS message. This needs to match up with the keys allowed for +# the content.javascript.log setting. +JsLogLevel = enum('JsLogLevel', ['unknown', 'info', 'warning', 'error']) + + MessageLevel = enum('MessageLevel', ['error', 'warning', 'info']) diff --git a/tests/end2end/features/conftest.py b/tests/end2end/features/conftest.py index 70d25c2fe..602657541 100644 --- a/tests/end2end/features/conftest.py +++ b/tests/end2end/features/conftest.py @@ -467,7 +467,6 @@ def javascript_message_logged(quteproc, message): def javascript_message_not_logged(quteproc, message): """Make sure the given message was *not* logged via javascript.""" quteproc.ensure_not_logged(category='js', - function='javaScriptConsoleMessage', message='[*] {}'.format(message)) diff --git a/tests/end2end/features/hints.feature b/tests/end2end/features/hints.feature index fef261a08..889e9e03e 100644 --- a/tests/end2end/features/hints.feature +++ b/tests/end2end/features/hints.feature @@ -197,8 +197,7 @@ Feature: Using hints Then the error "No elements found." should be shown Scenario: Clicking input with existing text - When I set content.javascript.log to info - And I open data/hints/input.html + When I open data/hints/input.html And I run :click-element id qute-input-existing And I wait for "Entering mode KeyMode.insert *" in the log And I run :fake-key new diff --git a/tests/end2end/features/javascript.feature b/tests/end2end/features/javascript.feature index e443e191a..732efa15a 100644 --- a/tests/end2end/features/javascript.feature +++ b/tests/end2end/features/javascript.feature @@ -5,8 +5,7 @@ Feature: Javascript stuff Integration with javascript. Scenario: Using console.log - When I set content.javascript.log to debug - And I open data/javascript/consolelog.html + When I open data/javascript/consolelog.html Then the javascript message "console.log works!" should be logged Scenario: Opening/Closing a window via JS @@ -111,7 +110,6 @@ Feature: Javascript stuff Scenario: Checking visible/invisible window size When I run :tab-only - And I set content.javascript.log to info And I open data/javascript/windowsize.html in a new background tab And I wait for "[*/data/javascript/windowsize.html:*] loaded" in the log And I run :tab-next @@ -119,7 +117,6 @@ Feature: Javascript stuff Scenario: Checking visible/invisible window size with vertical tabbar When I run :tab-only - And I set content.javascript.log to info And I set tabs.position to left And I open data/javascript/windowsize.html in a new background tab And I wait for "[*/data/javascript/windowsize.html:*] loaded" in the log diff --git a/tests/end2end/features/keyinput.feature b/tests/end2end/features/keyinput.feature index 21642d165..ee5b667e8 100644 --- a/tests/end2end/features/keyinput.feature +++ b/tests/end2end/features/keyinput.feature @@ -20,7 +20,6 @@ Feature: Keyboard input Scenario: Forwarding all keys When I open data/keyinput/log.html - And I set content.javascript.log to info And I set input.forward_unbound_keys to all And I press the key "," And I press the key "" @@ -33,7 +32,6 @@ Feature: Keyboard input Scenario: Forwarding special keys When I open data/keyinput/log.html - And I set content.javascript.log to info And I set input.forward_unbound_keys to auto And I press the key "x" And I press the key "" @@ -46,7 +44,6 @@ Feature: Keyboard input Scenario: Forwarding no keys When I open data/keyinput/log.html - And I set content.javascript.log to info And I set input.forward_unbound_keys to none And I press the key "" # @@ -60,8 +57,7 @@ Feature: Keyboard input Then the error "Could not parse 'blub': Got unknown key." should be shown Scenario: :fake-key sending key to the website - When I set content.javascript.log to info - And I open data/keyinput/log.html + When I open data/keyinput/log.html And I run :fake-key x Then the javascript message "key press: 88" should be logged And the javascript message "key release: 88" should be logged @@ -78,15 +74,13 @@ Feature: Keyboard input Then the error "No focused webview!" should be shown Scenario: :fake-key sending special key to the website - When I set content.javascript.log to info - And I open data/keyinput/log.html + When I open data/keyinput/log.html And I run :fake-key Then the javascript message "key press: 27" should be logged And the javascript message "key release: 27" should be logged Scenario: :fake-key sending keychain to the website - When I set content.javascript.log to info - And I open data/keyinput/log.html + When I open data/keyinput/log.html And I run :fake-key xy Then the javascript message "key press: 88" should be logged And the javascript message "key release: 88" should be logged diff --git a/tests/end2end/features/misc.feature b/tests/end2end/features/misc.feature index 2fe81f5fb..ed626875c 100644 --- a/tests/end2end/features/misc.feature +++ b/tests/end2end/features/misc.feature @@ -50,20 +50,19 @@ Feature: Various utility commands. ## :jseval Scenario: :jseval - When I set content.javascript.log to info - And I run :jseval console.log("Hello from JS!"); + When I run :jseval console.log("Hello from JS!"); And I wait for the javascript message "Hello from JS!" Then the message "No output or error" should be shown Scenario: :jseval without logging - When I set content.javascript.log to none + When I set content.javascript.log to {"unknown": "none", "info": "none", "warning": "debug", "error": "debug"} And I run :jseval console.log("Hello from JS!"); + And I set content.javascript.log to {"unknown": "debug", "info": "debug", "warning": "debug", "error": "debug"} Then the message "No output or error" should be shown And "[:*] Hello from JS!" should not be logged Scenario: :jseval with --quiet - When I set content.javascript.log to info - And I run :jseval --quiet console.log("Hello from JS!"); + When I run :jseval --quiet console.log("Hello from JS!"); And I wait for the javascript message "Hello from JS!" Then "No output or error" should not be logged @@ -77,16 +76,14 @@ Feature: Various utility commands. @qtwebengine_skip Scenario: :jseval with --world on QtWebKit - When I set content.javascript.log to info - And I run :jseval --world=1 console.log("Hello from JS!"); + When I run :jseval --world=1 console.log("Hello from JS!"); And I wait for the javascript message "Hello from JS!" Then "Ignoring world ID 1" should be logged And "No output or error" should be logged @qtwebkit_skip Scenario: :jseval uses separate world without --world - When I set content.javascript.log to info - And I open data/misc/jseval.html + When I open data/misc/jseval.html And I run :jseval do_log() Then the javascript message "Hello from the page!" should not be logged And the javascript message "Uncaught ReferenceError: do_log is not defined" should be logged @@ -94,23 +91,20 @@ Feature: Various utility commands. @qtwebkit_skip Scenario: :jseval using the main world - When I set content.javascript.log to info - And I open data/misc/jseval.html + When I open data/misc/jseval.html And I run :jseval --world 0 do_log() Then the javascript message "Hello from the page!" should be logged And "No output or error" should be logged @qtwebkit_skip Scenario: :jseval using the main world as name - When I set content.javascript.log to info - And I open data/misc/jseval.html + When I open data/misc/jseval.html And I run :jseval --world main do_log() Then the javascript message "Hello from the page!" should be logged And "No output or error" should be logged Scenario: :jseval --file using a file that exists as js-code - When I set content.javascript.log to info - And I run :jseval --file (testdata)/misc/jseval_file.js + When I run :jseval --file (testdata)/misc/jseval_file.js Then the javascript message "Hello from JS!" should be logged And the javascript message "Hello again from JS!" should be logged And "No output or error" should be logged diff --git a/tests/end2end/features/prompts.feature b/tests/end2end/features/prompts.feature index def50df72..7f8ba291d 100644 --- a/tests/end2end/features/prompts.feature +++ b/tests/end2end/features/prompts.feature @@ -3,9 +3,6 @@ Feature: Prompts Various prompts (javascript, SSL errors, authentification, etc.) - Background: - Given I set content.javascript.log to debug - # Javascript Scenario: Javascript alert diff --git a/tests/end2end/features/scroll.feature b/tests/end2end/features/scroll.feature index 1ec3e5012..d5e339f1a 100644 --- a/tests/end2end/features/scroll.feature +++ b/tests/end2end/features/scroll.feature @@ -323,8 +323,7 @@ Feature: Scrolling Then data/hello2.txt should be loaded Scenario: Scrolling to anchor in background tab - When I set content.javascript.log to info - And I open about:blank + When I open about:blank And I run :tab-only And I open data/scroll/simple.html#anchor in a new background tab And I run :tab-next diff --git a/tests/end2end/features/yankpaste.feature b/tests/end2end/features/yankpaste.feature index faab80dc7..dc3024eb4 100644 --- a/tests/end2end/features/yankpaste.feature +++ b/tests/end2end/features/yankpaste.feature @@ -255,8 +255,7 @@ Feature: Yanking and pasting. #### :insert-text Scenario: Inserting text into an empty text field - When I set content.javascript.log to info - And I open data/paste_primary.html + When I open data/paste_primary.html And I run :click-element id qute-textarea And I wait for "Entering mode KeyMode.insert (reason: clicking input)" in the log And I run :insert-text Hello world @@ -264,8 +263,7 @@ Feature: Yanking and pasting. Then the javascript message "textarea contents: Hello world" should be logged Scenario: Inserting text into an empty text field with javascript disabled - When I set content.javascript.log to info - And I set content.javascript.enabled to false + When I set content.javascript.enabled to false And I open data/paste_primary.html And I run :click-element id qute-textarea And I wait for "Entering mode KeyMode.insert (reason: clicking input)" in the log @@ -278,8 +276,7 @@ Feature: Yanking and pasting. Then the javascript message "textarea contents: Hello world" should be logged Scenario: Inserting text into a text field at specific position - When I set content.javascript.log to info - And I open data/paste_primary.html + When I open data/paste_primary.html And I insert "one two three four" into the text field And I run :click-element id qute-textarea And I wait for "Entering mode KeyMode.insert (reason: clicking input)" in the log @@ -292,8 +289,7 @@ Feature: Yanking and pasting. Then the javascript message "textarea contents: onHello worlde two three four" should be logged Scenario: Inserting text into a text field with undo - When I set content.javascript.log to info - And I open data/paste_primary.html + When I open data/paste_primary.html And I run :click-element id qute-textarea And I wait for "Entering mode KeyMode.insert (reason: clicking input)" in the log # Paste and undo diff --git a/tests/end2end/fixtures/quteprocess.py b/tests/end2end/fixtures/quteprocess.py index 38246ca91..dd03b358e 100644 --- a/tests/end2end/fixtures/quteprocess.py +++ b/tests/end2end/fixtures/quteprocess.py @@ -405,7 +405,6 @@ class QuteProc(testprocess.Process): The LogLine. """ line = self.wait_for(category='js', - function='javaScriptConsoleMessage', message='[*] {}'.format(message)) line.expected = True return line @@ -445,7 +444,6 @@ class QuteProc(testprocess.Process): def _is_error_logline(self, msg): """Check if the given LogLine is some kind of error message.""" is_js_error = (msg.category == 'js' and - msg.function == 'javaScriptConsoleMessage' and testutils.pattern_match(pattern='[*] [FAIL] *', value=msg.message)) # Try to complain about the most common mistake when accidentally @@ -464,7 +462,6 @@ class QuteProc(testprocess.Process): for msg in self._data: if (msg.category == 'js' and - msg.function == 'javaScriptConsoleMessage' and testutils.pattern_match(pattern='[*] [SKIP] *', value=msg.message)): skip_texts.append(msg.message.partition(' [SKIP] ')[2])