From 6f7ac6a934887bd6efa142e4263744f6c09b6659 Mon Sep 17 00:00:00 2001 From: Tobias Taschner Date: Fri, 26 Feb 2021 22:52:11 +0100 Subject: [PATCH] Add wxWebView::AddUserScript() This allows to inject javascript code across multiple pages --- include/wx/webview.h | 13 ++++++++++++ interface/wx/webview.h | 40 +++++++++++++++++++++++++++++++++++++ samples/webview/webview.cpp | 21 +++++++++++++++++++ 3 files changed, 74 insertions(+) diff --git a/include/wx/webview.h b/include/wx/webview.h index aadba6bfc7..732b612455 100644 --- a/include/wx/webview.h +++ b/include/wx/webview.h @@ -86,6 +86,12 @@ enum wxWebViewNavigationActionFlags wxWEBVIEW_NAV_ACTION_OTHER }; +enum wxWebViewUserScriptInjectionTime +{ + wxWEBVIEW_INJECT_AT_DOCUMENT_START, + wxWEBVIEW_INJECT_AT_DOCUMENT_END +}; + //Base class for custom scheme handlers class WXDLLIMPEXP_WEBVIEW wxWebViewHandler { @@ -181,9 +187,16 @@ public: virtual void Print() = 0; virtual void RegisterHandler(wxSharedPtr handler) = 0; virtual void Reload(wxWebViewReloadFlags flags = wxWEBVIEW_RELOAD_DEFAULT) = 0; + + // Script virtual bool RunScript(const wxString& javascript, wxString* output = NULL) const = 0; virtual bool AddScriptMessageHandler(const wxString& WXUNUSED(name)) { return false; } virtual bool RemoveScriptMessageHandler(const wxString& WXUNUSED(name)) { return false; } + virtual bool AddUserScript(const wxString& WXUNUSED(javascript), + wxWebViewUserScriptInjectionTime WXUNUSED(injectionTime) = wxWEBVIEW_INJECT_AT_DOCUMENT_START) + { return false; } + virtual void RemoveAllUserScripts() {} + virtual void SetEditable(bool enable = true) = 0; void SetPage(const wxString& html, const wxString& baseUrl) { diff --git a/interface/wx/webview.h b/interface/wx/webview.h index 28682e90d6..d736a1173f 100644 --- a/interface/wx/webview.h +++ b/interface/wx/webview.h @@ -107,6 +107,19 @@ enum wxWebViewNavigationActionFlags wxWEBVIEW_NAV_ACTION_OTHER }; +/** + Specifies at which place of documents an user script will be inserted. + + @since 3.1.5 +*/ +enum wxWebViewUserScriptInjectionTime +{ + /** Insert the code of the user script at the beginning of loaded documents. */ + wxWEBVIEW_INJECT_AT_DOCUMENT_START, + /** Insert the code of the user script at the end of the loaded documents. */ + wxWEBVIEW_INJECT_AT_DOCUMENT_END +}; + /** Internet Explorer emulation modes for wxWebViewIE. @@ -737,6 +750,33 @@ public: */ virtual bool RemoveScriptMessageHandler(const wxString& name); + /** + Injects the specified script into the webpage’s content. + + @param javascript The java script code to add + @param injectionTime Specifies when the script could will be + executed. + @return Returns true if the script was added successfully + + @note Please not that this is unsupported by the IE backend and + the Edge (Chromium) backend does only support wxWEBVIEW_INJECT_AT_DOCUMENT_START. + + @see RemoveAllUserScripts() + + @since 3.1.5 + */ + virtual bool AddUserScript(const wxString& javascript, + wxWebViewUserScriptInjectionTime injectionTime = wxWEBVIEW_INJECT_AT_DOCUMENT_START); + + /** + Removes all user scripts from the web view. + + @see AddUserScript() + + @since 3.1.5 + */ + virtual void RemoveAllUserScripts(); + /** @name Clipboard */ diff --git a/samples/webview/webview.cpp b/samples/webview/webview.cpp index 0b39fb6186..02441633a7 100644 --- a/samples/webview/webview.cpp +++ b/samples/webview/webview.cpp @@ -159,6 +159,7 @@ public: void OnRunScriptArrayWithEmulationLevel(wxCommandEvent& evt); #endif void OnRunScriptCustom(wxCommandEvent& evt); + void OnAddUserScript(wxCommandEvent& evt); void OnClearSelection(wxCommandEvent& evt); void OnDeleteSelection(wxCommandEvent& evt); void OnSelectAll(wxCommandEvent& evt); @@ -488,6 +489,7 @@ WebFrame::WebFrame(const wxString& url) : #endif m_script_custom = script_menu->Append(wxID_ANY, "Custom script"); m_tools_menu->AppendSubMenu(script_menu, _("Run Script")); + wxMenuItem* addUserScript = m_tools_menu->Append(wxID_ANY, _("Add user script")); //Selection menu wxMenu* selection = new wxMenu(); @@ -586,6 +588,7 @@ WebFrame::WebFrame(const wxString& url) : } #endif Bind(wxEVT_MENU, &WebFrame::OnRunScriptCustom, this, m_script_custom->GetId()); + Bind(wxEVT_MENU, &WebFrame::OnAddUserScript, this, addUserScript->GetId()); Bind(wxEVT_MENU, &WebFrame::OnClearSelection, this, m_selection_clear->GetId()); Bind(wxEVT_MENU, &WebFrame::OnDeleteSelection, this, m_selection_delete->GetId()); Bind(wxEVT_MENU, &WebFrame::OnSelectAll, this, selectall->GetId()); @@ -1198,6 +1201,24 @@ void WebFrame::OnRunScriptCustom(wxCommandEvent& WXUNUSED(evt)) return; RunScript(dialog.GetValue()); +} + +void WebFrame::OnAddUserScript(wxCommandEvent & WXUNUSED(evt)) +{ + wxString userScript = "window.wx_test_var = 'wxWidgets webview sample';"; + wxTextEntryDialog dialog + ( + this, + "Enter the JavaScript code to run as the initialization script that runs before any script in the HTML document.", + wxGetTextFromUserPromptStr, + userScript, + wxOK | wxCANCEL | wxCENTRE | wxTE_MULTILINE + ); + if (dialog.ShowModal() != wxID_OK) + return; + + if (!m_browser->AddUserScript(dialog.GetValue())) + wxLogError("Could not add user script"); } void WebFrame::OnClearSelection(wxCommandEvent& WXUNUSED(evt))