Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

BugFix: Prevent crashes from tab switches due to Table Of Content #1246

Merged
merged 1 commit into from
Nov 14, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions resources/js/headerAnchor.js
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ function anchorHeaderElements(headers)
});
}

function getHeaders()
function getHeadersJSONStr()
{
const headerInfo = { url: window.location.href.replace(location.hash,""), headers: [] };

Expand All @@ -40,12 +40,12 @@ function getHeaders()
const headers = getDOMElementsPreorderDFS(document.body, isHeaderElement);
headerInfo.headers = anchorHeaderElements(headers);
}
return headerInfo;
return JSON.stringify(headerInfo);
}

new QWebChannel(qt.webChannelTransport, function(channel) {
var kiwixObj = channel.objects.kiwixChannelObj;
kiwixObj.sendHeaders(getHeaders());
kiwixObj.sendHeadersJSONStr(getHeadersJSONStr());
kiwixObj.navigationRequested.connect(function(url, anchor) {
if (window.location.href.replace(location.hash,"") == url)
document.getElementById(anchor).scrollIntoView();
Expand Down
4 changes: 2 additions & 2 deletions src/kiwixwebchannelobject.h
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,10 @@ class KiwixWebChannelObject : public QObject
public:
explicit KiwixWebChannelObject(QObject *parent = nullptr) : QObject(parent) {};

Q_INVOKABLE void sendHeaders(const QJsonObject& headers) { emit headersChanged(headers); };
Q_INVOKABLE void sendHeadersJSONStr(const QString& headersJSONStr) { emit headersChanged(headersJSONStr); };

signals:
void headersChanged(const QJsonObject& headers);
void headersChanged(const QString& headersJSONStr);
void navigationRequested(const QString& url, const QString& anchor);
};

Expand Down
5 changes: 4 additions & 1 deletion src/tableofcontentbar.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -85,8 +85,11 @@ void createSubTree(QTreeWidgetItem* parent, QString parentNo, QJsonArray& header

void TableOfContentBar::setupTree(const QJsonObject& headers)
{
const auto headerUrl = headers["url"].toString();
const auto webView = KiwixApp::instance()->getTabWidget()->currentWebView();
if (!webView)
return;

const auto headerUrl = headers["url"].toString();
const auto currentUrl = webView->url().url(QUrl::RemoveFragment);
if (headerUrl != currentUrl)
return;
Expand Down
4 changes: 2 additions & 2 deletions src/webview.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -221,10 +221,10 @@ void WebView::onCurrentTitleChanged()
emit headersChanged(m_headers);
}

void WebView::onHeadersReceived(const QJsonObject& headers)
void WebView::onHeadersReceived(const QString& headersJSONStr)
{
const auto tabbar = KiwixApp::instance()->getTabWidget();
m_headers = QJsonObject(headers);
m_headers = QJsonDocument::fromJson(headersJSONStr.toUtf8()).object();

if (tabbar->currentWebView() == this)
emit headersChanged(m_headers);
Expand Down
2 changes: 1 addition & 1 deletion src/webview.h
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ public slots:
private slots:
void gotoTriggeredHistoryItemAction();
void onCurrentTitleChanged();
void onHeadersReceived(const QJsonObject& headers);
void onHeadersReceived(const QString& headersJSONStr);
void onNavigationRequested(const QString& url, const QString& anchor);

private:
Expand Down
Loading