From d86ac98e2e63c91393c71fb5dcbf8e79951c7930 Mon Sep 17 00:00:00 2001 From: Simon Rettberg Date: Tue, 27 Oct 2020 13:21:50 +0100 Subject: Fix triggering "go back" on blocked URL On more recenct Qt versions, you can get stuck at the error page for disallowed URLs. Make the code that detects this situation more agressive. --- src/slxbrowser.cpp | 36 ++++++++++++++++++++++++++---------- src/slxbrowser.h | 1 + 2 files changed, 27 insertions(+), 10 deletions(-) (limited to 'src') diff --git a/src/slxbrowser.cpp b/src/slxbrowser.cpp index 9c7eb03..c4c2067 100644 --- a/src/slxbrowser.cpp +++ b/src/slxbrowser.cpp @@ -85,10 +85,28 @@ void SlxBrowser::loadStarted() _progress->show(); } +void SlxBrowser::maybeTriggerBack() +{ + auto elems = _browser->page()->mainFrame()->documentElement().findAll("*"); + if (elems.count() > 10) + return; + QStringList want{{ "HEAD", "TITLE", "H1", "HR", "A", "PRE" }}; + for (auto x : elems) { + if (x.tagName() != want.first()) + continue; + want.pop_front(); + if (want.isEmpty()) + break; + } + if (want.isEmpty()) { + // Was probably a JS redirect, go back before displaying the error + _browser->page()->triggerAction(QWebPage::Back); + } +} + void SlxBrowser::loadFinished(bool ok) { _progress->hide(); - qint64 now = QDateTime::currentMSecsSinceEpoch(); bool abortedDl = _browser->wasAbortedDownload(); if (!abortedDl && !ok) { if (_unsupportedUri) { @@ -97,20 +115,18 @@ void SlxBrowser::loadFinished(bool ok) "Diese Art Link wird nicht unterstützt.\n\n" "(z.B. Mail)")); } else if (_blockedSite) { - int numElems = _browser->page()->mainFrame()->documentElement().findAll("*").count(); - if (now - _lastPageLoad < 500 && numElems < 15) { - // Was probably a JS redirect, go back before displaying the error - _browser->page()->triggerAction(QWebPage::Back); - } - QTimer::singleShot(5, [=]() { + maybeTriggerBack(); + QTimer::singleShot(10, [=]() { + maybeTriggerBack(); QMessageBox::warning(this, QString::fromUtf8("Denied"), QString::fromUtf8("Target URL not allowed.\n\n" "Dieser Link führt auf eine nicht erlaubte Seite.")); }); } else { _browser->page()->mainFrame()->setHtml("