1
Fork 0
mirror of https://github.com/RGBCube/serenity synced 2025-07-25 04:07:44 +00:00

LibWeb: Parse <script> elements and run any JavaScript found inside

This patch begins integrating LibJS into LibWeb. Document holds the
JS::Interpreter for now, and it is created on demand when you first
call Document::interpreter().

We also add a simple "alert()" function to the global object.
This commit is contained in:
Andreas Kling 2020-03-14 12:41:51 +01:00
parent f94099f796
commit 9c9d3f0904
11 changed files with 142 additions and 3 deletions

View file

@ -28,6 +28,9 @@
#include <AK/StringBuilder.h>
#include <LibCore/Timer.h>
#include <LibGUI/Application.h>
#include <LibGUI/MessageBox.h>
#include <LibJS/GlobalObject.h>
#include <LibJS/Interpreter.h>
#include <LibWeb/CSS/StyleResolver.h>
#include <LibWeb/DOM/Document.h>
#include <LibWeb/DOM/DocumentType.h>
@ -332,4 +335,19 @@ Color Document::visited_link_color() const
return frame()->html_view()->palette().visited_link();
}
JS::Interpreter& Document::interpreter()
{
if (!m_interpreter) {
m_interpreter = make<JS::Interpreter>();
m_interpreter->global_object().put_native_function("alert", [](JS::Interpreter&, Vector<JS::Value> arguments) -> JS::Value {
if (arguments.size() < 1)
return JS::js_undefined();
GUI::MessageBox::show(arguments[0].to_string(), "Alert", GUI::MessageBox::Type::Information);
return JS::js_undefined();
});
}
return *m_interpreter;
}
}