1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137
|
#include "js.h"
#include "cachedhttp.h"
#include "jsvm.h"
namespace {
Http &cachedHttp() {
static Http *h = [] {
CachedHttp *cachedHttp = new CachedHttp(Http::instance(), "js");
cachedHttp->setMaxSeconds(3600);
// Avoid expiring the cached js
cachedHttp->setMaxSize(0);
cachedHttp->getValidators().insert("application/javascript", [](const auto &reply) -> bool {
return !reply.body().isEmpty();
});
return cachedHttp;
}();
return *h;
}
} // namespace
JS &JS::instance() {
static thread_local JS i;
return i;
}
JS::JS(QObject *parent) : QObject(parent), engine(nullptr) {}
void JS::initialize(const QUrl &url) {
this->url = url;
initialize();
}
bool JS::checkError(const QJSValue &value) {
if (value.isError()) {
qWarning() << "Error" << value.toString();
qDebug() << value.property("stack").toString().splitRef('\n');
return true;
}
return false;
}
bool JS::isInitialized() {
if (ready) return true;
initialize();
return false;
}
JSResult &JS::callFunction(JSResult *result, const QString &name, const QJSValueList &args) {
if (!isInitialized()) {
qDebug() << "Not initialized";
QTimer::singleShot(1000, this,
[this, result, name, args] { callFunction(result, name, args); });
return *result;
}
auto function = engine->evaluate(name);
if (!function.isCallable()) {
qWarning() << function.toString() << " is not callable";
QTimer::singleShot(0, result, [result, function] { result->setError(function); });
return *result;
}
auto args2 = args;
args2.prepend(engine->newQObject(result));
qDebug() << "Calling" << function.toString();
auto v = function.call(args2);
if (checkError(v)) QTimer::singleShot(0, result, [result, v] { result->setError(v); });
return *result;
}
void JS::resetNAM() {
class MyCookieJar : public QNetworkCookieJar {
bool insertCookie(const QNetworkCookie &cookie) {
if (cookie.name().contains("CONSENT")) {
qDebug() << "Fixing CONSENT cookie" << cookie;
auto cookie2 = cookie;
cookie2.setValue(cookie.value().replace("PENDING", "YES"));
return QNetworkCookieJar::insertCookie(cookie2);
}
return QNetworkCookieJar::insertCookie(cookie);
}
};
auto nam = getEngine().networkAccessManager();
nam->clearAccessCache();
nam->setCookieJar(new MyCookieJar());
}
void JS::initialize() {
if (url.isEmpty()) {
qDebug() << "No js url set";
return;
}
if (initializing) return;
initializing = true;
qDebug() << "Initializing";
if (engine) engine->deleteLater();
engine = new QQmlEngine(this);
engine->setNetworkAccessManagerFactory(&namFactory);
engine->globalObject().setProperty("global", engine->globalObject());
QJSValue timer = engine->newQObject(new JSTimer(engine));
engine->globalObject().setProperty("setTimeoutQt", timer.property("setTimeout"));
QJSValue setTimeoutWrapperFunction =
engine->evaluate("function setTimeout(cb, delay) {"
"const args = Array.prototype.slice.call(arguments, 2);"
"return setTimeoutQt(cb, delay, args);"
"}");
checkError(setTimeoutWrapperFunction);
engine->globalObject().setProperty("clearTimeout", timer.property("clearTimeout"));
QJSValue vm = engine->newQObject(new JSVM(engine));
engine->globalObject().setProperty("runInContextQt", vm.property("runInContext"));
connect(cachedHttp().get(url), &HttpReply::finished, this, [this](auto &reply) {
if (!reply.isSuccessful()) {
emit initFailed("Cannot load JS");
qDebug() << "Cannot load JS";
initializing = false;
return;
}
auto value = engine->evaluate(reply.body());
if (!checkError(value)) {
qDebug() << "Initialized";
resetNAM();
ready = true;
emit initialized();
}
initializing = false;
});
}
|