diff --git a/src/lib/app/mainapplication.cpp b/src/lib/app/mainapplication.cpp index 4287291ae..912590382 100644 --- a/src/lib/app/mainapplication.cpp +++ b/src/lib/app/mainapplication.cpp @@ -61,9 +61,10 @@ #include #include #include -#include #include +#include #include +#include #if defined(Q_OS_WIN) && !defined(Q_OS_OS2) #include "registerqappassociation.h" @@ -631,10 +632,8 @@ void MainApplication::startPrivateBrowsing(const QUrl &startUrl) void MainApplication::reloadUserStyleSheet() { -#if QTWEBENGINE_DISABLED - const QUrl userCss = userStyleSheet(Settings().value("Web-Browser-Settings/userStyleSheet", QString()).toString()); - QWebEngineSettings::globalSettings()->setUserStyleSheetUrl(userCss); -#endif + const QString userCssFile = Settings().value("Web-Browser-Settings/userStyleSheet", QString()).toString(); + setUserStyleSheet(userCssFile); } void MainApplication::restoreOverrideCursor() @@ -873,13 +872,10 @@ void MainApplication::loadSettings() setWheelScrollLines(settings.value("wheelScrollLines", wheelScrollLines()).toInt()); -#if QTWEBENGINE_DISABLED const QString userCss = settings.value("userStyleSheet", QString()).toString(); settings.endGroup(); - webSettings->setUserStyleSheetUrl(userStyleSheet(userCss)); -#endif - settings.endGroup(); + setUserStyleSheet(userCss); settings.beginGroup("Browser-Fonts"); webSettings->setFontFamily(QWebEngineSettings::StandardFont, settings.value("StandardFont", webSettings->fontFamily(QWebEngineSettings::StandardFont)).toString()); @@ -1072,7 +1068,7 @@ void MainApplication::checkDefaultWebBrowser() #endif } -QUrl MainApplication::userStyleSheet(const QString &filePath) const +void MainApplication::setUserStyleSheet(const QString &filePath) { QString userCss; @@ -1091,13 +1087,28 @@ QUrl MainApplication::userStyleSheet(const QString &filePath) const userCss += QString("::selection {background: %1; color: %2;} ").arg(highlightColor, highlightedTextColor); #endif - userCss += AdBlockManager::instance()->elementHidingRules(); + userCss += AdBlockManager::instance()->elementHidingRules().replace(QL1S("\""), QL1S("\\\"")); userCss += QzTools::readAllFileContents(filePath).remove(QLatin1Char('\n')); - const QString encodedStyle = userCss.toLatin1().toBase64(); - const QString dataString = QString("data:text/css;charset=utf-8;base64,%1").arg(encodedStyle); + QString source("(function(){var css = document.createElement(\"style\");" + "css.setAttribute(\"type\", \"text/css\");" + "css.appendChild(document.createTextNode(\"%1\"));" + "document.getElementsByTagName(\"head\")[0].appendChild(css);})()"); - return QUrl(dataString); + const QString name = QStringLiteral("_qupzilla_userstylesheet"); + + QWebEngineScript oldScript = m_webProfile->scripts().findScript(name); + if (!oldScript.isNull()) { + m_webProfile->scripts().remove(oldScript); + } + + QWebEngineScript script; + script.setName(name); + script.setInjectionPoint(QWebEngineScript::DocumentReady); + script.setWorldId(QWebEngineScript::ApplicationWorld); + script.setRunsOnSubFrames(true); + script.setSourceCode(source.arg(userCss)); + m_webProfile->scripts().insert(script); } #if defined(Q_OS_WIN) && !defined(Q_OS_OS2) diff --git a/src/lib/app/mainapplication.h b/src/lib/app/mainapplication.h index 108d1a35c..b817da1fa 100644 --- a/src/lib/app/mainapplication.h +++ b/src/lib/app/mainapplication.h @@ -142,7 +142,7 @@ private: void backupSavedSessions(); void checkDefaultWebBrowser(); - QUrl userStyleSheet(const QString &filePath) const; + void setUserStyleSheet(const QString &filePath); bool m_isPrivate; bool m_isPortable;