Add access to user agent for wxWebView
Allow setting a custom user agent for a webview. Also allow access to the current user agent.
This commit is contained in:
committed by
Tobias Taschner
parent
d1efcc374b
commit
97e469c255
@@ -187,6 +187,8 @@ public:
|
|||||||
virtual void Print() = 0;
|
virtual void Print() = 0;
|
||||||
virtual void RegisterHandler(wxSharedPtr<wxWebViewHandler> handler) = 0;
|
virtual void RegisterHandler(wxSharedPtr<wxWebViewHandler> handler) = 0;
|
||||||
virtual void Reload(wxWebViewReloadFlags flags = wxWEBVIEW_RELOAD_DEFAULT) = 0;
|
virtual void Reload(wxWebViewReloadFlags flags = wxWEBVIEW_RELOAD_DEFAULT) = 0;
|
||||||
|
virtual bool SetUserAgent(const wxString& userAgent) { wxUnusedVar(userAgent); return false; }
|
||||||
|
virtual wxString GetUserAgent() const;
|
||||||
|
|
||||||
// Script
|
// Script
|
||||||
virtual bool RunScript(const wxString& javascript, wxString* output = NULL) const = 0;
|
virtual bool RunScript(const wxString& javascript, wxString* output = NULL) const = 0;
|
||||||
|
@@ -889,6 +889,28 @@ public:
|
|||||||
*/
|
*/
|
||||||
virtual bool IsAccessToDevToolsEnabled() const;
|
virtual bool IsAccessToDevToolsEnabled() const;
|
||||||
|
|
||||||
|
/**
|
||||||
|
Specify a custom user agent string for the web view.
|
||||||
|
Returns @true the user agent could be set.
|
||||||
|
|
||||||
|
If your first request should already use the custom user agent
|
||||||
|
please use two step creation and call SetUserAgent() before Create().
|
||||||
|
|
||||||
|
@note This is not implemented for IE. For Edge SetUserAgent()
|
||||||
|
MUST be called before Create().
|
||||||
|
|
||||||
|
@since 3.1.5
|
||||||
|
*/
|
||||||
|
virtual bool SetUserAgent(const wxString& userAgent);
|
||||||
|
|
||||||
|
/**
|
||||||
|
Returns the current user agent string for the web view.
|
||||||
|
|
||||||
|
@since 3.1.5
|
||||||
|
*/
|
||||||
|
virtual wxString GetUserAgent() const;
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@name History
|
@name History
|
||||||
*/
|
*/
|
||||||
|
@@ -161,6 +161,7 @@ public:
|
|||||||
void OnRunScriptMessage(wxCommandEvent& evt);
|
void OnRunScriptMessage(wxCommandEvent& evt);
|
||||||
void OnRunScriptCustom(wxCommandEvent& evt);
|
void OnRunScriptCustom(wxCommandEvent& evt);
|
||||||
void OnAddUserScript(wxCommandEvent& evt);
|
void OnAddUserScript(wxCommandEvent& evt);
|
||||||
|
void OnSetCustomUserAgent(wxCommandEvent& evt);
|
||||||
void OnClearSelection(wxCommandEvent& evt);
|
void OnClearSelection(wxCommandEvent& evt);
|
||||||
void OnDeleteSelection(wxCommandEvent& evt);
|
void OnDeleteSelection(wxCommandEvent& evt);
|
||||||
void OnSelectAll(wxCommandEvent& evt);
|
void OnSelectAll(wxCommandEvent& evt);
|
||||||
@@ -393,9 +394,6 @@ WebFrame::WebFrame(const wxString& url) :
|
|||||||
#endif
|
#endif
|
||||||
// Create the webview
|
// Create the webview
|
||||||
m_browser = wxWebView::New();
|
m_browser = wxWebView::New();
|
||||||
// Log backend information
|
|
||||||
wxLogMessage("Backend: %s Version: %s", m_browser->GetClassInfo()->GetClassName(),
|
|
||||||
wxWebView::GetBackendVersionInfo().ToString());
|
|
||||||
#ifdef __WXMAC__
|
#ifdef __WXMAC__
|
||||||
// With WKWebView handlers need to be registered before creation
|
// With WKWebView handlers need to be registered before creation
|
||||||
m_browser->RegisterHandler(wxSharedPtr<wxWebViewHandler>(new wxWebViewArchiveHandler("wxfs")));
|
m_browser->RegisterHandler(wxSharedPtr<wxWebViewHandler>(new wxWebViewArchiveHandler("wxfs")));
|
||||||
@@ -404,6 +402,11 @@ WebFrame::WebFrame(const wxString& url) :
|
|||||||
m_browser->Create(this, wxID_ANY, url, wxDefaultPosition, wxDefaultSize);
|
m_browser->Create(this, wxID_ANY, url, wxDefaultPosition, wxDefaultSize);
|
||||||
topsizer->Add(m_browser, wxSizerFlags().Expand().Proportion(1));
|
topsizer->Add(m_browser, wxSizerFlags().Expand().Proportion(1));
|
||||||
|
|
||||||
|
// Log backend information
|
||||||
|
wxLogMessage("Backend: %s Version: %s", m_browser->GetClassInfo()->GetClassName(),
|
||||||
|
wxWebView::GetBackendVersionInfo().ToString());
|
||||||
|
wxLogMessage("User Agent: %s", m_browser->GetUserAgent());
|
||||||
|
|
||||||
#ifndef __WXMAC__
|
#ifndef __WXMAC__
|
||||||
//We register the wxfs:// protocol for testing purposes
|
//We register the wxfs:// protocol for testing purposes
|
||||||
m_browser->RegisterHandler(wxSharedPtr<wxWebViewHandler>(new wxWebViewArchiveHandler("wxfs")));
|
m_browser->RegisterHandler(wxSharedPtr<wxWebViewHandler>(new wxWebViewArchiveHandler("wxfs")));
|
||||||
@@ -493,6 +496,7 @@ WebFrame::WebFrame(const wxString& url) :
|
|||||||
m_script_custom = script_menu->Append(wxID_ANY, "Custom script");
|
m_script_custom = script_menu->Append(wxID_ANY, "Custom script");
|
||||||
m_tools_menu->AppendSubMenu(script_menu, _("Run Script"));
|
m_tools_menu->AppendSubMenu(script_menu, _("Run Script"));
|
||||||
wxMenuItem* addUserScript = m_tools_menu->Append(wxID_ANY, _("Add user script"));
|
wxMenuItem* addUserScript = m_tools_menu->Append(wxID_ANY, _("Add user script"));
|
||||||
|
wxMenuItem* setCustomUserAgent = m_tools_menu->Append(wxID_ANY, _("Set custom user agent"));
|
||||||
|
|
||||||
//Selection menu
|
//Selection menu
|
||||||
wxMenu* selection = new wxMenu();
|
wxMenu* selection = new wxMenu();
|
||||||
@@ -593,6 +597,7 @@ WebFrame::WebFrame(const wxString& url) :
|
|||||||
Bind(wxEVT_MENU, &WebFrame::OnRunScriptMessage, this, m_script_message->GetId());
|
Bind(wxEVT_MENU, &WebFrame::OnRunScriptMessage, this, m_script_message->GetId());
|
||||||
Bind(wxEVT_MENU, &WebFrame::OnRunScriptCustom, this, m_script_custom->GetId());
|
Bind(wxEVT_MENU, &WebFrame::OnRunScriptCustom, this, m_script_custom->GetId());
|
||||||
Bind(wxEVT_MENU, &WebFrame::OnAddUserScript, this, addUserScript->GetId());
|
Bind(wxEVT_MENU, &WebFrame::OnAddUserScript, this, addUserScript->GetId());
|
||||||
|
Bind(wxEVT_MENU, &WebFrame::OnSetCustomUserAgent, this, setCustomUserAgent->GetId());
|
||||||
Bind(wxEVT_MENU, &WebFrame::OnClearSelection, this, m_selection_clear->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::OnDeleteSelection, this, m_selection_delete->GetId());
|
||||||
Bind(wxEVT_MENU, &WebFrame::OnSelectAll, this, selectall->GetId());
|
Bind(wxEVT_MENU, &WebFrame::OnSelectAll, this, selectall->GetId());
|
||||||
@@ -1230,6 +1235,24 @@ void WebFrame::OnAddUserScript(wxCommandEvent & WXUNUSED(evt))
|
|||||||
wxLogError("Could not add user script");
|
wxLogError("Could not add user script");
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void WebFrame::OnSetCustomUserAgent(wxCommandEvent& WXUNUSED(evt))
|
||||||
|
{
|
||||||
|
wxString customUserAgent = "Mozilla/5.0 (iPhone; CPU iPhone OS 13_1_3 like Mac OS X) AppleWebKit/605.1.15 (KHTML, like Gecko) Version/13.0.1 Mobile/15E148 Safari/604.1";
|
||||||
|
wxTextEntryDialog dialog
|
||||||
|
(
|
||||||
|
this,
|
||||||
|
"Enter the custom user agent string you would like to use.",
|
||||||
|
wxGetTextFromUserPromptStr,
|
||||||
|
customUserAgent,
|
||||||
|
wxOK | wxCANCEL | wxCENTRE
|
||||||
|
);
|
||||||
|
if (dialog.ShowModal() != wxID_OK)
|
||||||
|
return;
|
||||||
|
|
||||||
|
if (!m_browser->SetUserAgent(customUserAgent))
|
||||||
|
wxLogError("Could not set custom user agent");
|
||||||
|
}
|
||||||
|
|
||||||
void WebFrame::OnClearSelection(wxCommandEvent& WXUNUSED(evt))
|
void WebFrame::OnClearSelection(wxCommandEvent& WXUNUSED(evt))
|
||||||
{
|
{
|
||||||
m_browser->ClearSelection();
|
m_browser->ClearSelection();
|
||||||
|
@@ -217,6 +217,13 @@ long wxWebView::Find(const wxString& text, int flags)
|
|||||||
return 1;
|
return 1;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
wxString wxWebView::GetUserAgent() const
|
||||||
|
{
|
||||||
|
wxString userAgent;
|
||||||
|
RunScript("navigator.userAgent", &userAgent);
|
||||||
|
return userAgent;
|
||||||
|
}
|
||||||
|
|
||||||
// static
|
// static
|
||||||
wxWebView* wxWebView::New(const wxString& backend)
|
wxWebView* wxWebView::New(const wxString& backend)
|
||||||
{
|
{
|
||||||
|
Reference in New Issue
Block a user