diff --git a/Libraries/LibWeb/Bindings/WindowObject.cpp b/Libraries/LibWeb/Bindings/WindowObject.cpp index 7c321f86611..a078548d5d1 100644 --- a/Libraries/LibWeb/Bindings/WindowObject.cpp +++ b/Libraries/LibWeb/Bindings/WindowObject.cpp @@ -24,8 +24,11 @@ * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. */ +#include +#include #include #include +#include #include #include #include @@ -60,6 +63,8 @@ void WindowObject::initialize() define_native_function("setTimeout", set_timeout, 1); define_native_function("requestAnimationFrame", request_animation_frame, 1); define_native_function("cancelAnimationFrame", cancel_animation_frame, 1); + define_native_function("atob", atob, 1); + define_native_function("btoa", btoa, 1); define_property("navigator", heap().allocate(*this, *this), JS::Attribute::Enumerable | JS::Attribute::Configurable); define_property("location", heap().allocate(*this, *this), JS::Attribute::Enumerable | JS::Attribute::Configurable); @@ -205,6 +210,34 @@ JS_DEFINE_NATIVE_FUNCTION(WindowObject::cancel_animation_frame) return JS::js_undefined(); } +JS_DEFINE_NATIVE_FUNCTION(WindowObject::atob) +{ + auto* impl = impl_from(interpreter); + if (!impl) + return {}; + if (!interpreter.argument_count()) + return interpreter.throw_exception(JS::ErrorType::BadArgCountOne, "atob"); + auto string = interpreter.argument(0).to_string(interpreter); + if (interpreter.exception()) + return {}; + auto decoded = decode_base64(StringView(string)); + return JS::js_string(interpreter, String::copy(decoded)); +} + +JS_DEFINE_NATIVE_FUNCTION(WindowObject::btoa) +{ + auto* impl = impl_from(interpreter); + if (!impl) + return {}; + if (!interpreter.argument_count()) + return interpreter.throw_exception(JS::ErrorType::BadArgCountOne, "btoa"); + auto string = interpreter.argument(0).to_string(interpreter); + if (interpreter.exception()) + return {}; + auto encoded = encode_base64(StringView(string)); + return JS::js_string(interpreter, String::copy(encoded)); +} + JS_DEFINE_NATIVE_GETTER(WindowObject::document_getter) { auto* impl = impl_from(interpreter, global_object); diff --git a/Libraries/LibWeb/Bindings/WindowObject.h b/Libraries/LibWeb/Bindings/WindowObject.h index 3bc6797249e..731c2083d95 100644 --- a/Libraries/LibWeb/Bindings/WindowObject.h +++ b/Libraries/LibWeb/Bindings/WindowObject.h @@ -60,6 +60,8 @@ private: JS_DECLARE_NATIVE_FUNCTION(set_timeout); JS_DECLARE_NATIVE_FUNCTION(request_animation_frame); JS_DECLARE_NATIVE_FUNCTION(cancel_animation_frame); + JS_DECLARE_NATIVE_FUNCTION(atob); + JS_DECLARE_NATIVE_FUNCTION(btoa); NonnullRefPtr m_impl;