mirror of
https://github.com/SoftFever/OrcaSlicer.git
synced 2025-07-16 03:07:55 -06:00
Fix webview freezing issue
This commit is contained in:
parent
f3e4016795
commit
e97ab55e83
3 changed files with 36 additions and 17 deletions
|
@ -13,6 +13,7 @@
|
|||
#include <wx/textdlg.h>
|
||||
|
||||
#include <slic3r/GUI/Widgets/WebView.hpp>
|
||||
#include <wx/webview.h>
|
||||
|
||||
namespace pt = boost::property_tree;
|
||||
|
||||
|
@ -32,7 +33,8 @@ PrinterWebView::PrinterWebView(wxWindow *parent)
|
|||
return;
|
||||
}
|
||||
|
||||
Bind(wxEVT_WEBVIEW_ERROR, &PrinterWebView::OnError, this);
|
||||
m_browser->Bind(wxEVT_WEBVIEW_ERROR, &PrinterWebView::OnError, this);
|
||||
m_browser->Bind(wxEVT_WEBVIEW_LOADED, &PrinterWebView::OnLoaded, this);
|
||||
|
||||
SetSizer(topsizer);
|
||||
|
||||
|
@ -69,9 +71,10 @@ void PrinterWebView::load_url(wxString& url, wxString apikey)
|
|||
// this->Raise();
|
||||
if (m_browser == nullptr)
|
||||
return;
|
||||
m_apikey = apikey;
|
||||
m_apikey_sent = false;
|
||||
|
||||
m_browser->LoadURL(url);
|
||||
if(!apikey.IsEmpty())
|
||||
SendAPIKey(apikey);
|
||||
//m_browser->SetFocus();
|
||||
UpdateState();
|
||||
}
|
||||
|
@ -89,21 +92,27 @@ void PrinterWebView::OnClose(wxCloseEvent& evt)
|
|||
this->Hide();
|
||||
}
|
||||
|
||||
void PrinterWebView::SendAPIKey(wxString apikey)
|
||||
void PrinterWebView::SendAPIKey()
|
||||
{
|
||||
// After the page is fully loaded, run:
|
||||
if (m_apikey_sent || m_apikey.IsEmpty())
|
||||
return;
|
||||
m_apikey_sent = true;
|
||||
wxString script = wxString::Format(R"(
|
||||
// Example for overriding the global fetch method
|
||||
const originalFetch = window.fetch;
|
||||
window.fetch = function(input, init = {}) {
|
||||
init.headers = init.headers || {};
|
||||
init.headers['X-API-Key'] = '%s';
|
||||
return originalFetch(input, init);
|
||||
};
|
||||
// Check if window.fetch exists before overriding
|
||||
if (window.fetch) {
|
||||
const originalFetch = window.fetch;
|
||||
window.fetch = function(input, init = {}) {
|
||||
init.headers = init.headers || {};
|
||||
init.headers['X-API-Key'] = '%s';
|
||||
return originalFetch(input, init);
|
||||
};
|
||||
}
|
||||
)",
|
||||
apikey);
|
||||
m_apikey);
|
||||
m_browser->RemoveAllUserScripts();
|
||||
|
||||
m_browser->RunScript(script);
|
||||
m_browser->AddUserScript(script);
|
||||
m_browser->Reload();
|
||||
}
|
||||
|
||||
void PrinterWebView::OnError(wxWebViewEvent &evt)
|
||||
|
@ -138,7 +147,12 @@ void PrinterWebView::OnError(wxWebViewEvent &evt)
|
|||
BOOST_LOG_TRIVIAL(info) << __FUNCTION__<< boost::format(": error loading page %1% %2% %3% %4%") %evt.GetURL() %evt.GetTarget() %e %evt.GetString();
|
||||
}
|
||||
|
||||
|
||||
void PrinterWebView::OnLoaded(wxWebViewEvent &evt)
|
||||
{
|
||||
if (evt.GetURL().IsEmpty())
|
||||
return;
|
||||
SendAPIKey();
|
||||
}
|
||||
|
||||
} // GUI
|
||||
} // Slic3r
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue