diff --git a/src/lib/tools/closedtabsmanager.cpp b/src/lib/tools/closedtabsmanager.cpp index 9309e96e4..f3d7d682f 100644 --- a/src/lib/tools/closedtabsmanager.cpp +++ b/src/lib/tools/closedtabsmanager.cpp @@ -47,7 +47,6 @@ bool ClosedTabsManager::isClosedTabAvailable() return !m_closedTabs.isEmpty(); } - ClosedTabsManager::Tab ClosedTabsManager::takeLastClosedTab() { Tab tab; diff --git a/src/lib/webview/tabwidget.cpp b/src/lib/webview/tabwidget.cpp index 47560b967..34b2e9355 100644 --- a/src/lib/webview/tabwidget.cpp +++ b/src/lib/webview/tabwidget.cpp @@ -472,13 +472,9 @@ void TabWidget::closeTab(int index, bool force) m_closedTabsManager->saveView(webTab, index); // window.onbeforeunload handling - if (!webView->page()->mainFrame()->evaluateJavaScript("window.onbeforeunload===null").toBool()) { - webView->load(QUrl()); - if (webView->url() != QUrl()) { - // We are not closing, let's remove the tab from history - m_closedTabsManager->takeLastClosedTab(); - return; - } + if (!webView->onBeforeUnload()) { + m_closedTabsManager->takeLastClosedTab(); + return; } m_locationBars->removeWidget(webView->webTab()->locationBar()); diff --git a/src/lib/webview/webview.cpp b/src/lib/webview/webview.cpp index 073333087..6e3f9109a 100644 --- a/src/lib/webview/webview.cpp +++ b/src/lib/webview/webview.cpp @@ -234,6 +234,17 @@ QWebElement WebView::activeElement() const 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) { // Valid url must have scheme and actually contains something (therefore scheme:// is invalid) diff --git a/src/lib/webview/webview.h b/src/lib/webview/webview.h index 3b7124f0b..252a2a83a 100644 --- a/src/lib/webview/webview.h +++ b/src/lib/webview/webview.h @@ -51,6 +51,9 @@ public: bool hasRss() const; QWebElement activeElement() const; + // Executes window.onbeforeunload, returns true if view can be closed + bool onBeforeUnload(); + void addNotification(QWidget* notif); bool eventFilter(QObject* obj, QEvent* event);