1
mirror of https://invent.kde.org/network/falkon.git synced 2024-11-11 09:32:12 +01:00

[WebView] Another approach to window.onbeforeunload handling

Don't try to load empty url just to check for beforeunload event.
Explicitly evaluate the window.onbeforeunload function instead.
This commit is contained in:
nowrep 2014-02-11 22:25:32 +01:00
parent dc6f71a1b8
commit dab7c82ae8
4 changed files with 17 additions and 8 deletions

View File

@ -47,7 +47,6 @@ bool ClosedTabsManager::isClosedTabAvailable()
return !m_closedTabs.isEmpty(); return !m_closedTabs.isEmpty();
} }
ClosedTabsManager::Tab ClosedTabsManager::takeLastClosedTab() ClosedTabsManager::Tab ClosedTabsManager::takeLastClosedTab()
{ {
Tab tab; Tab tab;

View File

@ -472,13 +472,9 @@ void TabWidget::closeTab(int index, bool force)
m_closedTabsManager->saveView(webTab, index); m_closedTabsManager->saveView(webTab, index);
// window.onbeforeunload handling // window.onbeforeunload handling
if (!webView->page()->mainFrame()->evaluateJavaScript("window.onbeforeunload===null").toBool()) { if (!webView->onBeforeUnload()) {
webView->load(QUrl()); m_closedTabsManager->takeLastClosedTab();
if (webView->url() != QUrl()) { return;
// We are not closing, let's remove the tab from history
m_closedTabsManager->takeLastClosedTab();
return;
}
} }
m_locationBars->removeWidget(webView->webTab()->locationBar()); m_locationBars->removeWidget(webView->webTab()->locationBar());

View File

@ -234,6 +234,17 @@ QWebElement WebView::activeElement() const
return page()->mainFrame()->hitTestContent(activeRect.center()).element(); return page()->mainFrame()->hitTestContent(activeRect.center()).element();
} }
bool WebView::onBeforeUnload()
{
const QString res = page()->mainFrame()->evaluateJavaScript("window.onbeforeunload(new Event(\"beforeunload\"))").toString();
if (!res.isEmpty()) {
return page()->javaScriptConfirm(page()->mainFrame(), res);
}
return true;
}
bool WebView::isUrlValid(const QUrl &url) bool WebView::isUrlValid(const QUrl &url)
{ {
// Valid url must have scheme and actually contains something (therefore scheme:// is invalid) // Valid url must have scheme and actually contains something (therefore scheme:// is invalid)

View File

@ -51,6 +51,9 @@ public:
bool hasRss() const; bool hasRss() const;
QWebElement activeElement() const; QWebElement activeElement() const;
// Executes window.onbeforeunload, returns true if view can be closed
bool onBeforeUnload();
void addNotification(QWidget* notif); void addNotification(QWidget* notif);
bool eventFilter(QObject* obj, QEvent* event); bool eventFilter(QObject* obj, QEvent* event);