Add wxWebView::AddUserScript()

This allows to inject javascript code across multiple pages
This commit is contained in:
Tobias Taschner
2021-02-26 22:52:11 +01:00
committed by Tobias Taschner
parent f2bfa7d446
commit 6f7ac6a934
3 changed files with 74 additions and 0 deletions

View File

@@ -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<wxWebViewHandler> 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)
{

View File

@@ -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 webpages 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
*/

View File

@@ -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());
@@ -1200,6 +1203,24 @@ void WebFrame::OnRunScriptCustom(wxCommandEvent& WXUNUSED(evt))
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))
{
m_browser->ClearSelection();