From 1146f8bf49eda7f23960e2efdd6dcefb6a98b552 Mon Sep 17 00:00:00 2001 From: Daniel Silverstone Date: Sun, 5 May 2019 15:38:09 +0100 Subject: Add browser_window_exec Signed-off-by: Daniel Silverstone --- desktop/browser.c | 21 +++++++++++++++++++++ include/netsurf/browser_window.h | 12 ++++++++++++ 2 files changed, 33 insertions(+) diff --git a/desktop/browser.c b/desktop/browser.c index 1c8aa95fa..d26abd043 100644 --- a/desktop/browser.c +++ b/desktop/browser.c @@ -3406,3 +3406,24 @@ int browser_get_dpi(void) { return FIXTOINT(nscss_screen_dpi); } + +/* exported interface documented in browser.h */ +bool browser_window_exec(struct browser_window *bw, const char *src, size_t srclen) +{ + assert(bw != NULL); + + if (!bw->current_content) { + NSLOG(netsurf, DEEPDEBUG, "Unable to exec, no content"); + return false; + } + + if (content_get_status(bw->current_content) != CONTENT_STATUS_DONE) { + NSLOG(netsurf, DEEPDEBUG, "Unable to exec, content not done"); + return false; + } + + /* Okay it should be safe, forward the request through to the content + * itself. Only HTML contents currently support executing code + */ + return content_exec(bw->current_content, src, srclen); +} diff --git a/include/netsurf/browser_window.h b/include/netsurf/browser_window.h index 439b0785d..77a263189 100644 --- a/include/netsurf/browser_window.h +++ b/include/netsurf/browser_window.h @@ -725,4 +725,16 @@ nserror browser_window_get_name(struct browser_window *bw, const char **name); */ nserror browser_window_set_name(struct browser_window *bw, const char *name); +/** + * Execute some JavaScript code in a browsing context. + * + * Runs the passed in JavaScript code in the browsing context. + * + * \param bw The browser window + * \param src The JavaScript source code + * \param srclen The length of the source code + * \return Whether the JS function was successfully injected into the content + */ +bool browser_window_exec(struct browser_window *bw, const char *src, size_t srclen); + #endif -- cgit v1.2.3