multiple fixes for nodejs integration

This commit is contained in:
Martin Rotter 2022-03-26 10:30:14 +01:00
parent 02c183b2c7
commit 919f3bd26f
4 changed files with 53 additions and 15 deletions

View File

@ -6,23 +6,17 @@
#include "gui/webbrowser.h" #include "gui/webbrowser.h"
#include "miscellaneous/application.h" #include "miscellaneous/application.h"
#include "miscellaneous/skinfactory.h" #include "miscellaneous/skinfactory.h"
#include "network-web/adblock/adblockmanager.h"
#include "network-web/adblock/adblockrequestinfo.h"
#include "network-web/networkfactory.h" #include "network-web/networkfactory.h"
#include "network-web/webfactory.h"
#include <QAction> #include <QAction>
#include <QWheelEvent> #include <QWheelEvent>
LiteHtmlViewer::LiteHtmlViewer(QWidget* parent) : QLiteHtmlWidget(parent) { LiteHtmlViewer::LiteHtmlViewer(QWidget* parent) : QLiteHtmlWidget(parent) {
setResourceHandler([this](const QUrl& url) { setResourceHandler([this](const QUrl& url) {
QByteArray output; return handleResource(url);
NetworkFactory::performNetworkOperation(
url.toString(),
5000,
{},
output,
QNetworkAccessManager::Operation::GetOperation);
return output;
}); });
} }
@ -204,3 +198,31 @@ void LiteHtmlViewer::wheelEvent(QWheelEvent* event) {
QLiteHtmlWidget::wheelEvent(event); QLiteHtmlWidget::wheelEvent(event);
} }
QByteArray LiteHtmlViewer::handleResource(const QUrl& url) {
AdblockRequestInfo block_request(url);
if (url.path().endsWith(QSL("css"))) {
block_request.setResourceType(QSL("stylesheet"));
}
else {
block_request.setResourceType(QSL("image"));
}
if (qApp->web()->adBlock()->block(block_request).m_blocked) {
qWarningNN << LOGSEC_ADBLOCK << "Blocked request:" << QUOTE_W_SPACE_DOT(block_request.requestUrl().toString());
return {};
}
else {
QByteArray output;
NetworkFactory::performNetworkOperation(
url.toString(),
5000,
{},
output,
QNetworkAccessManager::Operation::GetOperation);
return output;
}
}

View File

@ -38,6 +38,9 @@ class LiteHtmlViewer : public QLiteHtmlWidget, public WebViewer {
protected: protected:
virtual void wheelEvent(QWheelEvent* event); virtual void wheelEvent(QWheelEvent* event);
private:
QByteArray handleResource(const QUrl& url);
}; };
#endif // LITEHTMLVIEWER_H #endif // LITEHTMLVIEWER_H

View File

@ -9,6 +9,7 @@
#include "miscellaneous/settings.h" #include "miscellaneous/settings.h"
#include <QDir> #include <QDir>
#include <QFile>
#include <QJsonArray> #include <QJsonArray>
#include <QJsonDocument> #include <QJsonDocument>
#include <QJsonObject> #include <QJsonObject>
@ -58,6 +59,15 @@ QString NodeJs::processedPackageFolder() const {
qCriticalNN << LOGSEC_NODEJS << "Failed to create package folder structure" << QUOTE_W_SPACE_DOT(path); qCriticalNN << LOGSEC_NODEJS << "Failed to create package folder structure" << QUOTE_W_SPACE_DOT(path);
} }
if (!QDir(path).exists(QSL("package.json"))) {
QFile fl(path + QDir::separator() + QSL("package.json"));
fl.open(QIODevice::OpenModeFlag::WriteOnly);
fl.write(QString("{}").toUtf8());
fl.flush();
fl.close();
}
return QDir::toNativeSeparators(path); return QDir::toNativeSeparators(path);
} }
@ -80,7 +90,9 @@ QString NodeJs::npmVersion(const QString& npm_exe) const {
NodeJs::PackageStatus NodeJs::packageStatus(const PackageMetadata& pkg) const { NodeJs::PackageStatus NodeJs::packageStatus(const PackageMetadata& pkg) const {
QString npm_ls = IOFactory::startProcessGetOutput(npmExecutable(), QString npm_ls = IOFactory::startProcessGetOutput(npmExecutable(),
{ QSL("ls"), QSL("--unicode"), QSL("--json"), QSL("--prefix"), { QSL("ls"), QSL("--unicode"), QSL("--json"), QSL("--prefix"),
processedPackageFolder() }); processedPackageFolder() },
{},
processedPackageFolder());
QJsonDocument json = QJsonDocument::fromJson(npm_ls.toUtf8()); QJsonDocument json = QJsonDocument::fromJson(npm_ls.toUtf8());
QJsonObject deps = json.object()["dependencies"].toObject(); QJsonObject deps = json.object()["dependencies"].toObject();
@ -180,10 +192,11 @@ void NodeJs::installPackages(const QList<PackageMetadata>& pkgs) {
to_install.prepend(QSL("--production")); to_install.prepend(QSL("--production"));
to_install.prepend(QSL("install")); to_install.prepend(QSL("install"));
to_install.append(QSL("--prefix"));
to_install.append(processedPackageFolder());
IOFactory::startProcess(proc, npmExecutable(), to_install); //to_install.append(QSL("--prefix"));
//to_install.append(processedPackageFolder());
IOFactory::startProcess(proc, npmExecutable(), to_install, {}, processedPackageFolder());
} }
catch (const ProcessException& ex) { catch (const ProcessException& ex) {
qCriticalNN << LOGSEC_NODEJS << "Packages" << QUOTE_W_SPACE(to_install) qCriticalNN << LOGSEC_NODEJS << "Packages" << QUOTE_W_SPACE(to_install)

View File

@ -103,6 +103,6 @@ void AdblockRequestInfo::initialize(const QUrl& url) {
#if defined(USE_WEBENGINE) #if defined(USE_WEBENGINE)
setResourceType(convertResourceType(QWebEngineUrlRequestInfo::ResourceType::ResourceTypeMainFrame)); setResourceType(convertResourceType(QWebEngineUrlRequestInfo::ResourceType::ResourceTypeMainFrame));
#else #else
setResourceType(QSL("image")); setResourceType(QSL("main_frame"));
#endif #endif
} }