LibWeb+WebContent: Setup the js console client earlier

This allows us to print messages in inline scripts. Also add an example
of this in the welcome page to test this.
This commit is contained in:
davidot 2022-09-21 17:12:00 +02:00 committed by Linus Groh
parent 446a10a1ac
commit 4912b22e3b
6 changed files with 15 additions and 3 deletions

View file

@ -41,6 +41,9 @@
column-width: 250px;
}
</style>
<script>
console.log('Hello from an inline script in the head of welcome.html');
</script>
<script src="welcome.js"></script>
</head>

View file

@ -1,3 +1,6 @@
document.addEventListener("DOMContentLoaded", function () {
console.log(
"Hello from DOMContentLoaded! There should be a message before this from an inline script"
);
document.getElementById("ua").innerHTML = navigator.userAgent;
});

View file

@ -398,6 +398,8 @@ void FrameLoader::resource_did_load()
document->set_content_type(resource()->mime_type());
browsing_context().set_active_document(document);
if (auto* page = browsing_context().page())
page->client().page_did_create_main_document();
if (!parse_document(*document, resource()->encoded_data())) {
load_error_page(url, "Failed to parse content.");

View file

@ -86,6 +86,7 @@ public:
virtual CSS::PreferredColorScheme preferred_color_scheme() const = 0;
virtual void page_did_change_title(String const&) { }
virtual void page_did_start_loading(const AK::URL&) { }
virtual void page_did_create_main_document() { }
virtual void page_did_finish_loading(const AK::URL&) { }
virtual void page_did_change_selection() { }
virtual void page_did_request_cursor_change(Gfx::StandardCursor) { }

View file

@ -187,11 +187,13 @@ void PageHost::page_did_start_loading(const URL& url)
m_client.async_did_start_loading(url);
}
void PageHost::page_did_create_main_document()
{
m_client.initialize_js_console({});
}
void PageHost::page_did_finish_loading(const URL& url)
{
// FIXME: This is called after the page has finished loading, which means any log messages
// that happen *while* it is loading (such as inline <script>s) will be lost.
m_client.initialize_js_console({});
m_client.async_did_finish_loading(url);
}

View file

@ -57,6 +57,7 @@ private:
virtual void page_did_request_context_menu(Gfx::IntPoint const&) override;
virtual void page_did_request_link_context_menu(Gfx::IntPoint const&, const URL&, String const& target, unsigned modifiers) override;
virtual void page_did_start_loading(const URL&) override;
virtual void page_did_create_main_document() override;
virtual void page_did_finish_loading(const URL&) override;
virtual void page_did_request_alert(String const&) override;
virtual bool page_did_request_confirm(String const&) override;