Fix webview freezing issue

This commit is contained in:
SoftFever 2023-09-04 20:02:26 +08:00
parent f3e4016795
commit e97ab55e83
3 changed files with 36 additions and 17 deletions

View file

@ -19,6 +19,7 @@
#include "libslic3r/Print.hpp"
#include "libslic3r/Polygon.hpp"
#include "libslic3r/PrintConfig.hpp"
#include "libslic3r/SLAPrint.hpp"
#include "libslic3r/PresetBundle.hpp"
@ -3505,7 +3506,8 @@ void MainFrame::load_printer_url()
auto cfg = preset_bundle.printers.get_edited_preset().config;
wxString url = cfg.opt_string("print_host_webui").empty() ? cfg.opt_string("print_host") : cfg.opt_string("print_host_webui");
wxString apikey;
if (cfg.has("printhost_apikey"))
if (cfg.has("printhost_apikey") && (cfg.option<ConfigOptionEnum<PrintHostType>>("host_type")->value == htPrusaLink ||
cfg.option<ConfigOptionEnum<PrintHostType>>("host_type")->value == htPrusaConnect))
apikey = cfg.opt_string("printhost_apikey");
if (!url.empty()) {
if (!url.Lower().starts_with("http"))

View file

@ -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

View file

@ -40,11 +40,14 @@ public:
void UpdateState();
void OnClose(wxCloseEvent& evt);
void OnError(wxWebViewEvent& evt);
void OnLoaded(wxWebViewEvent& evt);
private:
void SendAPIKey(wxString apikey);
void SendAPIKey();
wxWebView* m_browser;
long m_zoomFactor;
wxString m_apikey;
bool m_apikey_sent;
// DECLARE_EVENT_TABLE()
};