1
mirror of https://invent.kde.org/network/falkon.git synced 2024-09-21 17:52:10 +02: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();
}
ClosedTabsManager::Tab ClosedTabsManager::takeLastClosedTab()
{
Tab tab;

View File

@ -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());

View File

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

View File

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