diff --git a/src/lib/app/mainapplication.cpp b/src/lib/app/mainapplication.cpp index a6103116..5f726b8f 100644 --- a/src/lib/app/mainapplication.cpp +++ b/src/lib/app/mainapplication.cpp @@ -1,1242 +1,1250 @@ /* ============================================================ * Falkon - Qt web browser * Copyright (C) 2010-2018 David Rosca * * This program is free software: you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation, either version 3 of the License, or * (at your option) any later version. * * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * * You should have received a copy of the GNU General Public License * along with this program. If not, see . * ============================================================ */ #include "mainapplication.h" #include "history.h" #include "qztools.h" #include "updater.h" #include "autofill.h" #include "settings.h" #include "qzregexp.h" #include "autosaver.h" #include "datapaths.h" #include "tabwidget.h" #include "cookiejar.h" #include "bookmarks.h" #include "qzsettings.h" #include "proxystyle.h" #include "pluginproxy.h" #include "iconprovider.h" #include "browserwindow.h" #include "checkboxdialog.h" #include "networkmanager.h" #include "profilemanager.h" #include "restoremanager.h" #include "browsinglibrary.h" #include "downloadmanager.h" #include "clearprivatedata.h" #include "useragentmanager.h" #include "commandlineoptions.h" #include "searchenginesmanager.h" #include "desktopnotificationsfactory.h" #include "html5permissions/html5permissionsmanager.h" #include "scripts.h" #include "sessionmanager.h" #include "closedwindowsmanager.h" #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #ifdef Q_OS_WIN #include #include #include #endif #include #if defined(Q_OS_WIN) && !defined(Q_OS_OS2) #include "registerqappassociation.h" #endif MainApplication::MainApplication(int &argc, char** argv) : QtSingleApplication(argc, argv) , m_isPrivate(false) , m_isPortable(false) , m_isClosing(false) , m_isStartingAfterCrash(false) , m_history(0) , m_bookmarks(0) , m_autoFill(0) , m_cookieJar(0) , m_plugins(0) , m_browsingLibrary(0) , m_networkManager(0) , m_restoreManager(0) , m_sessionManager(0) , m_downloadManager(0) , m_userAgentManager(0) , m_searchEnginesManager(0) , m_closedWindowsManager(0) , m_html5PermissionsManager(0) , m_desktopNotifications(0) , m_webProfile(0) , m_autoSaver(0) #if defined(Q_OS_WIN) && !defined(Q_OS_OS2) , m_registerQAppAssociation(0) #endif { setAttribute(Qt::AA_UseHighDpiPixmaps); setAttribute(Qt::AA_DontCreateNativeWidgetSiblings); setApplicationName(QLatin1String("falkon")); setOrganizationDomain(QLatin1String("org.kde")); setWindowIcon(QIcon::fromTheme(QSL("qupzilla"), QIcon(QSL(":icons/exeicons/qupzilla-window.png")))); setDesktopFileName(QSL("org.kde.Falkon")); #ifdef GIT_REVISION setApplicationVersion(QSL("%1 (%2)").arg(Qz::VERSION, GIT_REVISION)); #else setApplicationVersion(Qz::VERSION); #endif // Set fallback icon theme (eg. on Windows/Mac) if (QIcon::fromTheme(QSL("view-refresh")).isNull()) { QIcon::setThemeSearchPaths(QStringList() << QL1S(":/breeze-fallback")); QIcon::setThemeName(QSL("breeze-fallback")); } // QSQLITE database plugin is required if (!QSqlDatabase::isDriverAvailable(QSL("QSQLITE"))) { QMessageBox::critical(0, QSL("Error"), QSL("Qt SQLite database plugin is not available. Please install it and restart the application.")); m_isClosing = true; return; } #ifdef Q_OS_WIN // Set default app font (needed for N'ko) int fontId = QFontDatabase::addApplicationFont(QSL("font.ttf")); if (fontId != -1) { const QStringList families = QFontDatabase::applicationFontFamilies(fontId); if (!families.empty()) setFont(QFont(families.at(0))); } #endif QUrl startUrl; QString startProfile; QStringList messages; bool noAddons = false; bool newInstance = false; if (argc > 1) { CommandLineOptions cmd; foreach (const CommandLineOptions::ActionPair &pair, cmd.getActions()) { switch (pair.action) { case Qz::CL_StartWithoutAddons: noAddons = true; break; case Qz::CL_StartWithProfile: startProfile = pair.text; break; case Qz::CL_StartPortable: m_isPortable = true; break; case Qz::CL_NewTab: messages.append(QLatin1String("ACTION:NewTab")); m_postLaunchActions.append(OpenNewTab); break; case Qz::CL_NewWindow: messages.append(QLatin1String("ACTION:NewWindow")); break; case Qz::CL_ToggleFullScreen: messages.append(QLatin1String("ACTION:ToggleFullScreen")); m_postLaunchActions.append(ToggleFullScreen); break; case Qz::CL_ShowDownloadManager: messages.append(QLatin1String("ACTION:ShowDownloadManager")); m_postLaunchActions.append(OpenDownloadManager); break; case Qz::CL_StartPrivateBrowsing: m_isPrivate = true; break; case Qz::CL_StartNewInstance: newInstance = true; break; case Qz::CL_OpenUrlInCurrentTab: startUrl = QUrl::fromUserInput(pair.text); messages.append("ACTION:OpenUrlInCurrentTab" + pair.text); break; case Qz::CL_OpenUrlInNewWindow: startUrl = QUrl::fromUserInput(pair.text); messages.append("ACTION:OpenUrlInNewWindow" + pair.text); break; case Qz::CL_OpenUrl: startUrl = QUrl::fromUserInput(pair.text); messages.append("URL:" + pair.text); break; case Qz::CL_ExitAction: m_isClosing = true; return; default: break; } } } if (isPortable()) { std::cout << "Falkon: Running in Portable Mode." << std::endl; DataPaths::setPortableVersion(); } // Don't start single application in private browsing if (!isPrivate()) { QString appId = QLatin1String("FalkonWebBrowser"); if (isPortable()) { appId.append(QLatin1String("Portable")); } if (newInstance) { if (startProfile.isEmpty() || startProfile == QLatin1String("default")) { std::cout << "New instance cannot be started with default profile!" << std::endl; } else { // Generate unique appId so it is possible to start more separate instances // of the same profile. It is dangerous to run more instances of the same profile, // but if the user wants it, we should allow it. appId.append(startProfile + QString::number(QDateTime::currentMSecsSinceEpoch())); } } setAppId(appId); } // If there is nothing to tell other instance, we need to at least wake it if (messages.isEmpty()) { messages.append(QLatin1String(" ")); } if (isRunning()) { m_isClosing = true; foreach (const QString &message, messages) { sendMessage(message); } return; } #ifdef Q_OS_MACOS setQuitOnLastWindowClosed(false); // disable tabbing issue#2261 extern void disableWindowTabbing(); disableWindowTabbing(); #else setQuitOnLastWindowClosed(true); #endif QSettings::setDefaultFormat(QSettings::IniFormat); QDesktopServices::setUrlHandler(QSL("http"), this, "addNewTab"); QDesktopServices::setUrlHandler(QSL("https"), this, "addNewTab"); QDesktopServices::setUrlHandler(QSL("ftp"), this, "addNewTab"); ProfileManager profileManager; profileManager.initConfigDir(); profileManager.initCurrentProfile(startProfile); Settings::createSettings(DataPaths::currentProfilePath() + QLatin1String("/settings.ini")); m_webProfile = isPrivate() ? new QWebEngineProfile(this) : QWebEngineProfile::defaultProfile(); connect(m_webProfile, &QWebEngineProfile::downloadRequested, this, &MainApplication::downloadRequested); m_networkManager = new NetworkManager(this); // Setup QWebChannel userscript QWebEngineScript script; script.setName(QSL("_falkon_webchannel")); script.setInjectionPoint(QWebEngineScript::DocumentCreation); - script.setWorldId(QWebEngineScript::MainWorld); + script.setWorldId(WebPage::SafeJsWorld); script.setRunsOnSubFrames(true); - script.setSourceCode(Scripts::setupWebChannel()); + script.setSourceCode(Scripts::setupWebChannel(script.worldId())); m_webProfile->scripts()->insert(script); + QWebEngineScript script2; + script2.setName(QSL("_qupzilla_webchannel2")); + script2.setInjectionPoint(QWebEngineScript::DocumentCreation); + script2.setWorldId(WebPage::UnsafeJsWorld); + script2.setRunsOnSubFrames(true); + script2.setSourceCode(Scripts::setupWebChannel(script2.worldId())); + m_webProfile->scripts()->insert(script2); + if (!isPrivate()) { m_sessionManager = new SessionManager(this); m_autoSaver = new AutoSaver(this); connect(m_autoSaver, SIGNAL(save()), m_sessionManager, SLOT(autoSaveLastSession())); Settings settings; settings.beginGroup(QSL("SessionRestore")); const bool wasRunning = settings.value(QSL("isRunning"), false).toBool(); const bool wasRestoring = settings.value(QSL("isRestoring"), false).toBool(); settings.setValue(QSL("isRunning"), true); settings.setValue(QSL("isRestoring"), wasRunning); settings.endGroup(); settings.sync(); m_isStartingAfterCrash = wasRunning && wasRestoring; if (wasRunning) { QTimer::singleShot(60 * 1000, this, [this]() { Settings().setValue(QSL("SessionRestore/isRestoring"), false); }); } // we have to ask about startup session before creating main window if (!m_isStartingAfterCrash && afterLaunch() == SelectSession) m_restoreManager = new RestoreManager(sessionManager()->askSessionFromUser()); } translateApp(); loadSettings(); m_plugins = new PluginProxy; m_autoFill = new AutoFill(this); if (!noAddons) m_plugins->loadPlugins(); BrowserWindow* window = createWindow(Qz::BW_FirstAppWindow, startUrl); connect(window, SIGNAL(startingCompleted()), this, SLOT(restoreOverrideCursor())); connect(this, SIGNAL(focusChanged(QWidget*,QWidget*)), this, SLOT(onFocusChanged())); if (!isPrivate()) { #ifndef DISABLE_CHECK_UPDATES Settings settings; bool checkUpdates = settings.value("Web-Browser-Settings/CheckUpdates", true).toBool(); if (checkUpdates) { new Updater(window); } #endif sessionManager()->backupSavedSessions(); if (m_isStartingAfterCrash || afterLaunch() == RestoreSession) { m_restoreManager = new RestoreManager(sessionManager()->lastActiveSessionPath()); if (!m_restoreManager->isValid()) { destroyRestoreManager(); } } if (!m_isStartingAfterCrash && m_restoreManager) { restoreSession(window, m_restoreManager->restoreData()); } } QTimer::singleShot(0, this, SLOT(postLaunch())); } MainApplication::~MainApplication() { IconProvider::instance()->saveIconsToDatabase(); // Wait for all QtConcurrent jobs to finish QThreadPool::globalInstance()->waitForDone(); // Delete all classes that are saving data in destructor delete m_bookmarks; delete m_cookieJar; delete m_plugins; Settings::syncSettings(); } bool MainApplication::isClosing() const { return m_isClosing; } bool MainApplication::isPrivate() const { return m_isPrivate; } bool MainApplication::isPortable() const { #ifdef PORTABLE_BUILD return true; #else return m_isPortable; #endif } bool MainApplication::isStartingAfterCrash() const { return m_isStartingAfterCrash; } int MainApplication::windowCount() const { return m_windows.count(); } QList MainApplication::windows() const { return m_windows; } BrowserWindow* MainApplication::getWindow() const { if (m_lastActiveWindow) { return m_lastActiveWindow.data(); } return m_windows.isEmpty() ? 0 : m_windows.at(0); } BrowserWindow* MainApplication::createWindow(Qz::BrowserWindowType type, const QUrl &startUrl) { if (windowCount() == 0 && type != Qz::BW_MacFirstWindow) { type = Qz::BW_FirstAppWindow; } BrowserWindow* window = new BrowserWindow(type, startUrl); connect(window, SIGNAL(destroyed(QObject*)), this, SLOT(windowDestroyed(QObject*))); m_windows.prepend(window); return window; } MainApplication::AfterLaunch MainApplication::afterLaunch() const { return static_cast(Settings().value(QSL("Web-URL-Settings/afterLaunch"), RestoreSession).toInt()); } void MainApplication::openSession(BrowserWindow* window, RestoreData &restoreData) { setOverrideCursor(Qt::BusyCursor); if (!window) window = createWindow(Qz::BW_OtherRestoredWindow); if (window->tabCount() != 0) { // This can only happen when recovering crashed session! // Don't restore tabs in current window as user already opened some new tabs. createWindow(Qz::BW_OtherRestoredWindow)->restoreWindow(restoreData.windows.takeAt(0)); } else { window->restoreWindow(restoreData.windows.takeAt(0)); } foreach (const BrowserWindow::SavedWindow &data, restoreData.windows) { BrowserWindow* window = createWindow(Qz::BW_OtherRestoredWindow); window->restoreWindow(data); } m_closedWindowsManager->restoreState(restoreData.closedWindows); restoreOverrideCursor(); } bool MainApplication::restoreSession(BrowserWindow* window, RestoreData restoreData) { if (m_isPrivate || !restoreData.isValid()) { return false; } openSession(window, restoreData); m_restoreManager->clearRestoreData(); destroyRestoreManager(); return true; } void MainApplication::destroyRestoreManager() { if (m_restoreManager && m_restoreManager->isValid()) { return; } delete m_restoreManager; m_restoreManager = 0; } void MainApplication::reloadSettings() { loadSettings(); emit settingsReloaded(); } QString MainApplication::styleName() const { return m_proxyStyle ? m_proxyStyle->name() : QString(); } void MainApplication::setProxyStyle(ProxyStyle *style) { m_proxyStyle = style; setStyle(style); } QString MainApplication::currentLanguageFile() const { return m_languageFile; } QString MainApplication::currentLanguage() const { QString lang = m_languageFile; if (lang.isEmpty()) { return "en_US"; } return lang.left(lang.length() - 3); } History* MainApplication::history() { if (!m_history) { m_history = new History(this); } return m_history; } Bookmarks* MainApplication::bookmarks() { if (!m_bookmarks) { m_bookmarks = new Bookmarks(this); } return m_bookmarks; } AutoFill* MainApplication::autoFill() { return m_autoFill; } CookieJar* MainApplication::cookieJar() { if (!m_cookieJar) { m_cookieJar = new CookieJar(this); } return m_cookieJar; } PluginProxy* MainApplication::plugins() { return m_plugins; } BrowsingLibrary* MainApplication::browsingLibrary() { if (!m_browsingLibrary) { m_browsingLibrary = new BrowsingLibrary(getWindow()); } return m_browsingLibrary; } NetworkManager *MainApplication::networkManager() { return m_networkManager; } RestoreManager* MainApplication::restoreManager() { return m_restoreManager; } SessionManager* MainApplication::sessionManager() { return m_sessionManager; } DownloadManager* MainApplication::downloadManager() { if (!m_downloadManager) { m_downloadManager = new DownloadManager(); } return m_downloadManager; } UserAgentManager* MainApplication::userAgentManager() { if (!m_userAgentManager) { m_userAgentManager = new UserAgentManager(this); } return m_userAgentManager; } SearchEnginesManager* MainApplication::searchEnginesManager() { if (!m_searchEnginesManager) { m_searchEnginesManager = new SearchEnginesManager(this); } return m_searchEnginesManager; } ClosedWindowsManager* MainApplication::closedWindowsManager() { if (!m_closedWindowsManager) { m_closedWindowsManager = new ClosedWindowsManager(this); } return m_closedWindowsManager; } HTML5PermissionsManager* MainApplication::html5PermissionsManager() { if (!m_html5PermissionsManager) { m_html5PermissionsManager = new HTML5PermissionsManager(this); } return m_html5PermissionsManager; } DesktopNotificationsFactory* MainApplication::desktopNotifications() { if (!m_desktopNotifications) { m_desktopNotifications = new DesktopNotificationsFactory(this); } return m_desktopNotifications; } QWebEngineProfile *MainApplication::webProfile() const { return m_webProfile; } QWebEngineSettings *MainApplication::webSettings() const { return m_webProfile->settings(); } // static MainApplication* MainApplication::instance() { return static_cast(QCoreApplication::instance()); } void MainApplication::addNewTab(const QUrl &url) { BrowserWindow* window = getWindow(); if (window) { window->tabWidget()->addView(url, url.isEmpty() ? Qz::NT_SelectedNewEmptyTab : Qz::NT_SelectedTabAtTheEnd); } } void MainApplication::startPrivateBrowsing(const QUrl &startUrl) { QUrl url = startUrl; if (QAction* act = qobject_cast(sender())) { url = act->data().toUrl(); } QStringList args; args.append(QSL("--private-browsing")); args.append(QSL("--profile=") + ProfileManager::currentProfile()); if (!url.isEmpty()) { args << url.toEncoded(); } if (!QProcess::startDetached(applicationFilePath(), args)) { qWarning() << "MainApplication: Cannot start new browser process for private browsing!" << applicationFilePath() << args; } } void MainApplication::reloadUserStyleSheet() { const QString userCssFile = Settings().value("Web-Browser-Settings/userStyleSheet", QString()).toString(); setUserStyleSheet(userCssFile); } void MainApplication::restoreOverrideCursor() { QApplication::restoreOverrideCursor(); } void MainApplication::changeOccurred() { if (m_autoSaver) m_autoSaver->changeOccurred(); } void MainApplication::quitApplication() { if (m_downloadManager && !m_downloadManager->canClose()) { m_downloadManager->show(); return; } if (m_sessionManager && m_windows.count() > 0) { m_sessionManager->autoSaveLastSession(); } m_isClosing = true; for (BrowserWindow *window : qAsConst(m_windows)) { window->close(); } // Saving settings in saveSettings() slot called from quit() so // everything gets saved also when quitting application in other // way than clicking Quit action in File menu or closing last window // eg. on Mac (#157) if (!isPrivate()) { removeLockFile(); } quit(); } void MainApplication::postLaunch() { if (m_postLaunchActions.contains(OpenDownloadManager)) { downloadManager()->show(); } if (m_postLaunchActions.contains(OpenNewTab)) { getWindow()->tabWidget()->addView(QUrl(), Qz::NT_SelectedNewEmptyTab); } if (m_postLaunchActions.contains(ToggleFullScreen)) { getWindow()->toggleFullScreen(); } QSettings::setPath(QSettings::IniFormat, QSettings::UserScope, DataPaths::currentProfilePath()); connect(this, SIGNAL(messageReceived(QString)), this, SLOT(messageReceived(QString))); connect(this, SIGNAL(aboutToQuit()), this, SLOT(saveSettings())); createJumpList(); initPulseSupport(); QTimer::singleShot(5000, this, &MainApplication::runDeferredPostLaunchActions); } QByteArray MainApplication::saveState() const { RestoreData restoreData; restoreData.windows.reserve(m_windows.count()); for (BrowserWindow *window : qAsConst(m_windows)) { restoreData.windows.append(BrowserWindow::SavedWindow(window)); } if (m_restoreManager && m_restoreManager->isValid()) { QDataStream stream(&restoreData.crashedSession, QIODevice::WriteOnly); stream << m_restoreManager->restoreData(); } restoreData.closedWindows = m_closedWindowsManager->saveState(); QByteArray data; QDataStream stream(&data, QIODevice::WriteOnly); stream << Qz::sessionVersion; stream << restoreData; return data; } void MainApplication::saveSettings() { if (isPrivate()) { return; } m_isClosing = true; Settings settings; settings.beginGroup("SessionRestore"); settings.setValue("isRunning", false); settings.setValue("isRestoring", false); settings.endGroup(); settings.beginGroup("Web-Browser-Settings"); bool deleteCache = settings.value("deleteCacheOnClose", false).toBool(); bool deleteHistory = settings.value("deleteHistoryOnClose", false).toBool(); bool deleteHtml5Storage = settings.value("deleteHTML5StorageOnClose", false).toBool(); settings.endGroup(); settings.beginGroup("Cookie-Settings"); bool deleteCookies = settings.value("deleteCookiesOnClose", false).toBool(); settings.endGroup(); if (deleteHistory) { m_history->clearHistory(); } if (deleteHtml5Storage) { ClearPrivateData::clearLocalStorage(); } if (deleteCookies) { m_cookieJar->deleteAllCookies(); } if (deleteCache) { QzTools::removeDir(mApp->webProfile()->cachePath()); } m_searchEnginesManager->saveSettings(); m_plugins->shutdown(); m_networkManager->shutdown(); qzSettings->saveSettings(); QFile::remove(DataPaths::currentProfilePath() + QLatin1String("/WebpageIcons.db")); sessionManager()->saveSettings(); } void MainApplication::messageReceived(const QString &message) { QWidget* actWin = getWindow(); QUrl actUrl; if (message.startsWith(QLatin1String("URL:"))) { const QUrl url = QUrl::fromUserInput(message.mid(4)); addNewTab(url); actWin = getWindow(); } else if (message.startsWith(QLatin1String("ACTION:"))) { const QString text = message.mid(7); if (text == QLatin1String("NewTab")) { addNewTab(); } else if (text == QLatin1String("NewWindow")) { actWin = createWindow(Qz::BW_NewWindow); } else if (text == QLatin1String("ShowDownloadManager")) { downloadManager()->show(); actWin = downloadManager(); } else if (text == QLatin1String("ToggleFullScreen") && actWin) { BrowserWindow* qz = static_cast(actWin); qz->toggleFullScreen(); } else if (text.startsWith(QLatin1String("OpenUrlInCurrentTab"))) { actUrl = QUrl::fromUserInput(text.mid(19)); } else if (text.startsWith(QLatin1String("OpenUrlInNewWindow"))) { createWindow(Qz::BW_NewWindow, QUrl::fromUserInput(text.mid(18))); return; } } else { // User attempted to start another instance, let's open a new window actWin = createWindow(Qz::BW_NewWindow); } if (!actWin) { if (!isClosing()) { // It can only occur if download manager window was still opened createWindow(Qz::BW_NewWindow, actUrl); } return; } actWin->setWindowState(actWin->windowState() & ~Qt::WindowMinimized); actWin->raise(); actWin->activateWindow(); actWin->setFocus(); BrowserWindow* win = qobject_cast(actWin); if (win && !actUrl.isEmpty()) { win->loadAddress(actUrl); } } void MainApplication::windowDestroyed(QObject* window) { // qobject_cast doesn't work because QObject::destroyed is emitted from destructor Q_ASSERT(static_cast(window)); Q_ASSERT(m_windows.contains(static_cast(window))); m_windows.removeOne(static_cast(window)); } void MainApplication::onFocusChanged() { BrowserWindow* activeBrowserWindow = qobject_cast(activeWindow()); if (activeBrowserWindow) { m_lastActiveWindow = activeBrowserWindow; emit activeWindowChanged(m_lastActiveWindow); } } void MainApplication::runDeferredPostLaunchActions() { checkDefaultWebBrowser(); checkOptimizeDatabase(); } void MainApplication::downloadRequested(QWebEngineDownloadItem *download) { downloadManager()->download(download); } void MainApplication::loadSettings() { Settings settings; settings.beginGroup("Themes"); QString activeTheme = settings.value("activeTheme", DEFAULT_THEME_NAME).toString(); settings.endGroup(); loadTheme(activeTheme); QWebEngineSettings* webSettings = m_webProfile->settings(); // Web browsing settings settings.beginGroup("Web-Browser-Settings"); webSettings->setAttribute(QWebEngineSettings::LocalStorageEnabled, settings.value("HTML5StorageEnabled", true).toBool()); webSettings->setAttribute(QWebEngineSettings::PluginsEnabled, settings.value("allowPlugins", true).toBool()); webSettings->setAttribute(QWebEngineSettings::JavascriptEnabled, settings.value("allowJavaScript", true).toBool()); webSettings->setAttribute(QWebEngineSettings::JavascriptCanOpenWindows, settings.value("allowJavaScriptOpenWindow", false).toBool()); webSettings->setAttribute(QWebEngineSettings::JavascriptCanAccessClipboard, settings.value("allowJavaScriptAccessClipboard", true).toBool()); webSettings->setAttribute(QWebEngineSettings::LinksIncludedInFocusChain, settings.value("IncludeLinkInFocusChain", false).toBool()); webSettings->setAttribute(QWebEngineSettings::XSSAuditingEnabled, settings.value("XSSAuditing", false).toBool()); webSettings->setAttribute(QWebEngineSettings::PrintElementBackgrounds, settings.value("PrintElementBackground", true).toBool()); webSettings->setAttribute(QWebEngineSettings::SpatialNavigationEnabled, settings.value("SpatialNavigation", false).toBool()); webSettings->setAttribute(QWebEngineSettings::ScrollAnimatorEnabled, settings.value("AnimateScrolling", true).toBool()); webSettings->setAttribute(QWebEngineSettings::HyperlinkAuditingEnabled, false); webSettings->setAttribute(QWebEngineSettings::FullScreenSupportEnabled, true); webSettings->setAttribute(QWebEngineSettings::LocalContentCanAccessRemoteUrls, true); webSettings->setAttribute(QWebEngineSettings::FocusOnNavigationEnabled, false); webSettings->setDefaultTextEncoding(settings.value("DefaultEncoding", webSettings->defaultTextEncoding()).toString()); setWheelScrollLines(settings.value("wheelScrollLines", wheelScrollLines()).toInt()); const QString userCss = settings.value("userStyleSheet", QString()).toString(); settings.endGroup(); setUserStyleSheet(userCss); settings.beginGroup("Browser-Fonts"); webSettings->setFontFamily(QWebEngineSettings::StandardFont, settings.value("StandardFont", webSettings->fontFamily(QWebEngineSettings::StandardFont)).toString()); webSettings->setFontFamily(QWebEngineSettings::CursiveFont, settings.value("CursiveFont", webSettings->fontFamily(QWebEngineSettings::CursiveFont)).toString()); webSettings->setFontFamily(QWebEngineSettings::FantasyFont, settings.value("FantasyFont", webSettings->fontFamily(QWebEngineSettings::FantasyFont)).toString()); webSettings->setFontFamily(QWebEngineSettings::FixedFont, settings.value("FixedFont", webSettings->fontFamily(QWebEngineSettings::FixedFont)).toString()); webSettings->setFontFamily(QWebEngineSettings::SansSerifFont, settings.value("SansSerifFont", webSettings->fontFamily(QWebEngineSettings::SansSerifFont)).toString()); webSettings->setFontFamily(QWebEngineSettings::SerifFont, settings.value("SerifFont", webSettings->fontFamily(QWebEngineSettings::SerifFont)).toString()); webSettings->setFontSize(QWebEngineSettings::DefaultFontSize, settings.value("DefaultFontSize", 15).toInt()); webSettings->setFontSize(QWebEngineSettings::DefaultFixedFontSize, settings.value("FixedFontSize", 14).toInt()); webSettings->setFontSize(QWebEngineSettings::MinimumFontSize, settings.value("MinimumFontSize", 3).toInt()); webSettings->setFontSize(QWebEngineSettings::MinimumLogicalFontSize, settings.value("MinimumLogicalFontSize", 5).toInt()); settings.endGroup(); QWebEngineProfile* profile = QWebEngineProfile::defaultProfile(); profile->setPersistentCookiesPolicy(QWebEngineProfile::AllowPersistentCookies); profile->setPersistentStoragePath(DataPaths::currentProfilePath()); QString defaultPath = DataPaths::path(DataPaths::Cache); if (!defaultPath.startsWith(DataPaths::currentProfilePath())) defaultPath.append(QLatin1Char('/') + ProfileManager::currentProfile()); const QString &cachePath = settings.value("Web-Browser-Settings/CachePath", defaultPath).toString(); profile->setCachePath(cachePath); const bool allowCache = settings.value(QSL("Web-Browser-Settings/AllowLocalCache"), true).toBool(); profile->setHttpCacheType(allowCache ? QWebEngineProfile::DiskHttpCache : QWebEngineProfile::MemoryHttpCache); const int cacheSize = settings.value(QSL("Web-Browser-Settings/LocalCacheSize"), 50).toInt() * 1000 * 1000; profile->setHttpCacheMaximumSize(cacheSize); settings.beginGroup(QSL("SpellCheck")); profile->setSpellCheckEnabled(settings.value(QSL("Enabled"), false).toBool()); profile->setSpellCheckLanguages(settings.value(QSL("Languages")).toStringList()); settings.endGroup(); if (isPrivate()) { webSettings->setAttribute(QWebEngineSettings::LocalStorageEnabled, false); history()->setSaving(false); } if (m_downloadManager) { m_downloadManager->loadSettings(); } qzSettings->loadSettings(); networkManager()->loadSettings(); userAgentManager()->loadSettings(); } void MainApplication::loadTheme(const QString &name) { QString activeThemePath; const QStringList themePaths = DataPaths::allPaths(DataPaths::Themes); foreach (const QString &path, themePaths) { const QString theme = QString("%1/%2").arg(path, name); if (QFile::exists(theme + QLatin1String("/main.css"))) { activeThemePath = theme; break; } } if (activeThemePath.isEmpty()) { qWarning() << "Cannot load theme " << name; activeThemePath = QString("%1/%2").arg(DataPaths::path(DataPaths::Themes), DEFAULT_THEME_NAME); } QString qss = QzTools::readAllFileContents(activeThemePath + QLatin1String("/main.css")); #if defined(Q_OS_MACOS) qss.append(QzTools::readAllFileContents(activeThemePath + QLatin1String("/mac.css"))); #elif defined(Q_OS_UNIX) qss.append(QzTools::readAllFileContents(activeThemePath + QLatin1String("/linux.css"))); #elif defined(Q_OS_WIN) || defined(Q_OS_OS2) qss.append(QzTools::readAllFileContents(activeThemePath + QLatin1String("/windows.css"))); #endif if (isRightToLeft()) { qss.append(QzTools::readAllFileContents(activeThemePath + QLatin1String("/rtl.css"))); } qss.append(QzTools::readAllFileContents(DataPaths::currentProfilePath() + QL1S("/userChrome.css"))); QString relativePath = QDir::current().relativeFilePath(activeThemePath); qss.replace(QzRegExp(QSL("url\\s*\\(\\s*([^\\*:\\);]+)\\s*\\)"), Qt::CaseSensitive), QString("url(%1/\\1)").arg(relativePath)); setStyleSheet(qss); } void MainApplication::translateApp() { QString file = Settings().value(QSL("Language/language"), QLocale::system().name()).toString(); // It can only be "C" locale, for which we will use default English language if (file.size() < 2) file.clear(); if (!file.isEmpty() && !file.endsWith(QL1S(".qm"))) file.append(QL1S(".qm")); // Either we load default language (with empty file), or we attempt to load xx.qm (xx_yy.qm) Q_ASSERT(file.isEmpty() || file.size() >= 5); QString translationPath = DataPaths::path(DataPaths::Translations); if (!file.isEmpty()) { const QStringList translationsPaths = DataPaths::allPaths(DataPaths::Translations); foreach (const QString &path, translationsPaths) { // If "xx_yy" translation doesn't exists, try to use "xx*" translation // It can only happen when language is chosen from system locale if (!QFile(QString("%1/%2").arg(path, file)).exists()) { QDir dir(path); QString lang = file.left(2) + QL1S("*.qm"); const QStringList translations = dir.entryList(QStringList(lang)); // If no translation can be found, default English will be used file = translations.isEmpty() ? QString() : translations.at(0); } if (!file.isEmpty() && QFile(QString("%1/%2").arg(path, file)).exists()) { translationPath = path; break; } } } // Load application translation QTranslator* app = new QTranslator(this); app->load(file, translationPath); // Load Qt translation (first try to load from Qt path) QTranslator* sys = new QTranslator(this); sys->load(QL1S("qt_") + file, QLibraryInfo::location(QLibraryInfo::TranslationsPath)); // If there is no translation in Qt path for specified language, try to load it from our path if (sys->isEmpty()) { sys->load(QL1S("qt_") + file, translationPath); } m_languageFile = file; installTranslator(app); installTranslator(sys); } void MainApplication::checkDefaultWebBrowser() { if (isPortable()) { return; } #if defined(Q_OS_WIN) && !defined(Q_OS_OS2) Settings settings; bool checkNow = settings.value("Web-Browser-Settings/CheckDefaultBrowser", DEFAULT_CHECK_DEFAULTBROWSER).toBool(); if (!checkNow) { return; } bool checkAgain = true; if (!associationManager()->isDefaultForAllCapabilities()) { CheckBoxDialog dialog(QMessageBox::Yes | QMessageBox::No, getWindow()); dialog.setDefaultButton(QMessageBox::Yes); dialog.setText(tr("Falkon is not currently your default browser. Would you like to make it your default browser?")); dialog.setCheckBoxText(tr("Always perform this check when starting Falkon.")); dialog.setDefaultCheckState(Qt::Checked); dialog.setWindowTitle(tr("Default Browser")); dialog.setIcon(QMessageBox::Warning); if (dialog.exec() == QMessageBox::Yes) { if (!mApp->associationManager()->showNativeDefaultAppSettingsUi()) mApp->associationManager()->registerAllAssociation(); } checkAgain = dialog.isChecked(); } settings.setValue("Web-Browser-Settings/CheckDefaultBrowser", checkAgain); #endif } void MainApplication::checkOptimizeDatabase() { Settings settings; settings.beginGroup(QSL("Browser")); const int numberOfRuns = settings.value(QSL("RunsWithoutOptimizeDb"), 0).toInt(); settings.setValue(QSL("RunsWithoutOptimizeDb"), numberOfRuns + 1); if (numberOfRuns > 20) { std::cout << "Optimizing database..." << std::endl; IconProvider::instance()->clearOldIconsInDatabase(); settings.setValue(QSL("RunsWithoutOptimizeDb"), 0); } settings.endGroup(); } void MainApplication::setUserStyleSheet(const QString &filePath) { QString userCss; #if !defined(Q_OS_UNIX) && !defined(Q_OS_MACOS) // Don't grey out selection on losing focus (to prevent graying out found text) QString highlightColor; QString highlightedTextColor; #ifdef Q_OS_MACOS highlightColor = QLatin1String("#b6d6fc"); highlightedTextColor = QLatin1String("#000"); #else QPalette pal = style()->standardPalette(); highlightColor = pal.color(QPalette::Highlight).name(); highlightedTextColor = pal.color(QPalette::HighlightedText).name(); #endif userCss += QString("::selection {background: %1; color: %2;} ").arg(highlightColor, highlightedTextColor); #endif userCss += QzTools::readAllFileContents(filePath).remove(QLatin1Char('\n')); const QString name = QStringLiteral("_falkon_userstylesheet"); QWebEngineScript oldScript = m_webProfile->scripts()->findScript(name); if (!oldScript.isNull()) { m_webProfile->scripts()->remove(oldScript); } if (userCss.isEmpty()) return; QWebEngineScript script; script.setName(name); script.setInjectionPoint(QWebEngineScript::DocumentReady); script.setWorldId(WebPage::SafeJsWorld); script.setRunsOnSubFrames(true); script.setSourceCode(Scripts::setCss(userCss)); m_webProfile->scripts()->insert(script); } void MainApplication::createJumpList() { #ifdef Q_OS_WIN QWinJumpList *jumpList = new QWinJumpList(this); jumpList->clear(); // Frequent QWinJumpListCategory *frequent = jumpList->frequent(); frequent->setVisible(true); const QVector mostList = m_history->mostVisited(7); for (const HistoryEntry &entry : mostList) { frequent->addLink(IconProvider::iconForUrl(entry.url), entry.title, applicationFilePath(), QStringList{entry.url.toEncoded()}); } // Tasks QWinJumpListCategory *tasks = jumpList->tasks(); tasks->setVisible(true); tasks->addLink(IconProvider::newTabIcon(), tr("Open new tab"), applicationFilePath(), {QSL("--new-tab")}); tasks->addLink(IconProvider::newWindowIcon(), tr("Open new window"), applicationFilePath(), {QSL("--new-window")}); tasks->addLink(IconProvider::privateBrowsingIcon(), tr("Open new private window"), applicationFilePath(), {QSL("--private-browsing")}); #endif } void MainApplication::initPulseSupport() { qputenv("PULSE_PROP_OVERRIDE_application.name", "Falkon"); qputenv("PULSE_PROP_OVERRIDE_application.icon_name", "qupzilla"); qputenv("PULSE_PROP_OVERRIDE_media.icon_name", "qupzilla"); } #if defined(Q_OS_WIN) && !defined(Q_OS_OS2) RegisterQAppAssociation* MainApplication::associationManager() { if (!m_registerQAppAssociation) { QString desc = tr("Falkon is a new, fast and secure open-source WWW browser. Falkon is licensed under GPL version 3 or (at your option) any later version. It is based on WebKit core and Qt Framework."); QString fileIconPath = QApplication::applicationFilePath() + ",1"; QString appIconPath = QApplication::applicationFilePath() + ",0"; m_registerQAppAssociation = new RegisterQAppAssociation("Falkon", QApplication::applicationFilePath(), appIconPath, desc, this); m_registerQAppAssociation->addCapability(".html", "Falkon.HTML", "HTML File", fileIconPath, RegisterQAppAssociation::FileAssociation); m_registerQAppAssociation->addCapability(".htm", "Falkon.HTM", "HTM File", fileIconPath, RegisterQAppAssociation::FileAssociation); m_registerQAppAssociation->addCapability("http", "Falkon.HTTP", "URL:HyperText Transfer Protocol", appIconPath, RegisterQAppAssociation::UrlAssociation); m_registerQAppAssociation->addCapability("https", "Falkon.HTTPS", "URL:HyperText Transfer Protocol with Privacy", appIconPath, RegisterQAppAssociation::UrlAssociation); } return m_registerQAppAssociation; } #endif #ifdef Q_OS_MACOS #include bool MainApplication::event(QEvent* e) { switch (e->type()) { case QEvent::FileOpen: { QFileOpenEvent *ev = static_cast(e); if (!ev->url().isEmpty()) { addNewTab(ev->url()); } else if (!ev->file().isEmpty()) { addNewTab(QUrl::fromLocalFile(ev->file())); } else { return false; } return true; } case QEvent::ApplicationActivate: if (!activeWindow() && m_windows.isEmpty()) createWindow(Qz::BW_NewWindow); break; default: break; } return QtSingleApplication::event(e); } #endif diff --git a/src/lib/autofill/autofill.cpp b/src/lib/autofill/autofill.cpp index 51a733af..32e8eb5d 100644 --- a/src/lib/autofill/autofill.cpp +++ b/src/lib/autofill/autofill.cpp @@ -1,356 +1,357 @@ /* ============================================================ * Falkon - Qt web browser -* Copyright (C) 2010-2017 David Rosca +* Copyright (C) 2010-2018 David Rosca * * This program is free software: you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation, either version 3 of the License, or * (at your option) any later version. * * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * * You should have received a copy of the GNU General Public License * along with this program. If not, see . * ============================================================ */ #include "autofill.h" #include "browserwindow.h" #include "webpage.h" #include "sqldatabase.h" #include "tabbedwebview.h" #include "popupwebview.h" #include "mainapplication.h" #include "autofillnotification.h" #include "settings.h" #include "passwordmanager.h" #include "qztools.h" #include "scripts.h" +#include "webpage.h" #include #include #include #include #include AutoFill::AutoFill(QObject* parent) : QObject(parent) , m_manager(new PasswordManager(this)) , m_isStoring(false) { loadSettings(); // Setup AutoFill userscript QWebEngineScript script; script.setName(QSL("_falkon_autofill")); script.setInjectionPoint(QWebEngineScript::DocumentReady); - script.setWorldId(QWebEngineScript::MainWorld); + script.setWorldId(WebPage::SafeJsWorld); script.setRunsOnSubFrames(true); script.setSourceCode(Scripts::setupFormObserver()); mApp->webProfile()->scripts()->insert(script); } PasswordManager* AutoFill::passwordManager() const { return m_manager; } void AutoFill::loadSettings() { Settings settings; settings.beginGroup("Web-Browser-Settings"); m_isStoring = settings.value("SavePasswordsOnSites", true).toBool(); settings.endGroup(); } bool AutoFill::isStored(const QUrl &url) { if (!isStoringEnabled(url)) { return false; } return !m_manager->getEntries(url).isEmpty(); } bool AutoFill::isStoringEnabled(const QUrl &url) { if (!m_isStoring) { return false; } QString server = url.host(); if (server.isEmpty()) { server = url.toString(); } QSqlQuery query(SqlDatabase::instance()->database()); query.prepare("SELECT count(id) FROM autofill_exceptions WHERE server=?"); query.addBindValue(server); query.exec(); if (!query.next()) { return false; } return query.value(0).toInt() <= 0; } void AutoFill::blockStoringforUrl(const QUrl &url) { QString server = url.host(); if (server.isEmpty()) { server = url.toString(); } QSqlQuery query(SqlDatabase::instance()->database()); query.prepare("INSERT INTO autofill_exceptions (server) VALUES (?)"); query.addBindValue(server); query.exec(); } QVector AutoFill::getFormData(const QUrl &url) { return m_manager->getEntries(url); } QVector AutoFill::getAllFormData() { return m_manager->getAllEntries(); } void AutoFill::updateLastUsed(PasswordEntry &data) { m_manager->updateLastUsed(data); } // HTTP Authorization void AutoFill::addEntry(const QUrl &url, const QString &name, const QString &pass) { PasswordEntry entry; entry.host = PasswordManager::createHost(url); entry.username = name; entry.password = pass; m_manager->addEntry(entry); } // WEB Form void AutoFill::addEntry(const QUrl &url, const PageFormData &formData) { PasswordEntry entry; entry.host = PasswordManager::createHost(url); entry.username = formData.username; entry.password = formData.password; entry.data = formData.postData; m_manager->addEntry(entry); } // HTTP Authorization void AutoFill::updateEntry(const QUrl &url, const QString &name, const QString &pass) { PasswordEntry entry; entry.host = PasswordManager::createHost(url); entry.username = name; entry.password = pass; m_manager->updateEntry(entry); } // WEB Form bool AutoFill::updateEntry(const PasswordEntry &entry) { return m_manager->updateEntry(entry); } void AutoFill::removeEntry(const PasswordEntry &entry) { m_manager->removeEntry(entry); } void AutoFill::removeAllEntries() { m_manager->removeAllEntries(); } void AutoFill::saveForm(WebPage *page, const QUrl &frameUrl, const PageFormData &formData) { // Don't save in private browsing if (mApp->isPrivate() || !page) return; if (!isStoringEnabled(frameUrl)) return; PasswordEntry updateData; if (isStored(frameUrl)) { const QVector &list = getFormData(frameUrl); foreach (const PasswordEntry &data, list) { if (data.username == formData.username) { updateData = data; updateLastUsed(updateData); if (data.password == formData.password) { updateData.password.clear(); return; } updateData.username = formData.username; updateData.password = formData.password; updateData.data = formData.postData; break; } } } if (m_lastNotification && m_lastNotificationPage == page) { m_lastNotification->close(); } AutoFillNotification* aWidget = new AutoFillNotification(frameUrl, formData, updateData); page->view()->addNotification(aWidget); m_lastNotification = aWidget; m_lastNotificationPage = page; } // Returns all saved passwords on this page QVector AutoFill::completePage(WebPage *page, const QUrl &frameUrl) { QVector list; if (!page || !isStored(frameUrl)) return list; list = getFormData(frameUrl); if (!list.isEmpty()) { const PasswordEntry entry = list.at(0); page->runJavaScript(Scripts::completeFormData(entry.data), WebPage::SafeJsWorld); } return list; } QByteArray AutoFill::exportPasswords() { QByteArray output; QXmlStreamWriter stream(&output); stream.setCodec("UTF-8"); stream.setAutoFormatting(true); stream.writeStartDocument(); stream.writeStartElement("passwords"); stream.writeAttribute("version", "1.0"); QVector entries = m_manager->getAllEntries(); foreach (const PasswordEntry &entry, entries) { stream.writeStartElement("entry"); stream.writeTextElement("server", entry.host); stream.writeTextElement("username", entry.username); stream.writeTextElement("password", entry.password); stream.writeTextElement("data", entry.data); stream.writeEndElement(); } QSqlQuery query(SqlDatabase::instance()->database()); query.prepare("SELECT server FROM autofill_exceptions"); query.exec(); while (query.next()) { stream.writeStartElement("exception"); stream.writeTextElement("server", query.value(0).toString()); stream.writeEndElement(); } stream.writeEndElement(); stream.writeEndDocument(); return output; } bool AutoFill::importPasswords(const QByteArray &data) { QSqlDatabase db = QSqlDatabase::database(); db.transaction(); QXmlStreamReader xml(data); while (!xml.atEnd()) { xml.readNext(); if (xml.isStartElement()) { if (xml.name() == QLatin1String("entry")) { PasswordEntry entry; while (xml.readNext()) { if (xml.name() == QLatin1String("server")) { entry.host = xml.readElementText(); } else if (xml.name() == QLatin1String("username")) { entry.username = xml.readElementText(); } else if (xml.name() == QLatin1String("password")) { entry.password = xml.readElementText(); } else if (xml.name() == QLatin1String("data")) { entry.data = xml.readElementText().toUtf8(); } if (xml.isEndElement() && xml.name() == QLatin1String("entry")) { break; } } if (entry.isValid()) { bool containsEntry = false; foreach (const PasswordEntry &e, m_manager->getEntries(QUrl(entry.host))) { if (e.username == entry.username) { containsEntry = true; break; } } if (!containsEntry) { m_manager->addEntry(entry); } } } else if (xml.name() == QLatin1String("exception")) { QString server; while (xml.readNext()) { if (xml.name() == QLatin1String("server")) { server = xml.readElementText(); } if (xml.isEndElement() && xml.name() == QLatin1String("exception")) { break; } } if (!server.isEmpty()) { QSqlQuery query(SqlDatabase::instance()->database()); query.prepare("SELECT id FROM autofill_exceptions WHERE server=?"); query.addBindValue(server); query.exec(); if (!query.next()) { query.prepare("INSERT INTO autofill_exceptions (server) VALUES (?)"); query.addBindValue(server); query.exec(); } } } } } db.commit(); return !xml.hasError(); } diff --git a/src/lib/navigation/websearchbar.cpp b/src/lib/navigation/websearchbar.cpp index 55b9c1aa..ccb58801 100644 --- a/src/lib/navigation/websearchbar.cpp +++ b/src/lib/navigation/websearchbar.cpp @@ -1,337 +1,337 @@ /* ============================================================ * Falkon - Qt web browser -* Copyright (C) 2010-2017 David Rosca +* Copyright (C) 2010-2018 David Rosca * * This program is free software: you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation, either version 3 of the License, or * (at your option) any later version. * * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * * You should have received a copy of the GNU General Public License * along with this program. If not, see . * ============================================================ */ #include "websearchbar.h" #include "browserwindow.h" #include "mainapplication.h" #include "tabbedwebview.h" #include "webpage.h" #include "settings.h" #include "qzsettings.h" #include "tabwidget.h" #include "clickablelabel.h" #include "buttonwithmenu.h" #include "searchenginesmanager.h" #include "searchenginesdialog.h" #include "networkmanager.h" #include "iconprovider.h" #include "scripts.h" #include #include #include #include #include #include #include #include WebSearchBar_Button::WebSearchBar_Button(QWidget* parent) : ClickableLabel(parent) { setObjectName("websearchbar-searchbutton"); setCursor(QCursor(Qt::PointingHandCursor)); setFocusPolicy(Qt::ClickFocus); } void WebSearchBar_Button::contextMenuEvent(QContextMenuEvent* event) { event->accept(); } WebSearchBar::WebSearchBar(BrowserWindow* window) : LineEdit(window) , m_window(window) , m_reloadingEngines(false) { setObjectName("websearchbar"); setDragEnabled(true); m_buttonSearch = new WebSearchBar_Button(this); m_boxSearchType = new ButtonWithMenu(this); m_boxSearchType->setObjectName("websearchbar-searchprovider-comobobox"); // RTL Support // If we don't add 'm_boxSearchType' by following code, then we should use suitable padding-left value // but then, when typing RTL text the layout dynamically changed and within RTL layout direction // padding-left is equivalent to padding-right and vice versa, and because style sheet is // not changed dynamically this create padding problems. addWidget(m_boxSearchType, LineEdit::LeftSide); addWidget(m_buttonSearch, LineEdit::RightSide); connect(m_buttonSearch, SIGNAL(clicked(QPoint)), this, SLOT(search())); connect(m_buttonSearch, SIGNAL(middleClicked(QPoint)), this, SLOT(searchInNewTab())); connect(m_boxSearchType, SIGNAL(activeItemChanged(ButtonWithMenu::Item)), this, SLOT(searchChanged(ButtonWithMenu::Item))); setWidgetSpacing(0); m_searchManager = mApp->searchEnginesManager(); connect(m_boxSearchType->menu(), SIGNAL(aboutToShow()), this, SLOT(aboutToShowMenu())); m_completer = new QCompleter(this); m_completer->setCompletionMode(QCompleter::UnfilteredPopupCompletion); m_completerModel = new QStringListModel(this); m_completer->setModel(m_completerModel); m_completer->popup()->setMinimumHeight(90); setCompleter(m_completer); connect(m_completer->popup(), &QAbstractItemView::activated, this, &WebSearchBar::search); m_openSearchEngine = new OpenSearchEngine(this); m_openSearchEngine->setNetworkAccessManager(mApp->networkManager()); connect(m_openSearchEngine, SIGNAL(suggestions(QStringList)), this, SLOT(addSuggestions(QStringList))); connect(this, SIGNAL(textEdited(QString)), m_openSearchEngine, SLOT(requestSuggestions(QString))); editAction(PasteAndGo)->setText(tr("Paste And &Search")); editAction(PasteAndGo)->setIcon(QIcon::fromTheme(QSL("edit-paste"))); connect(editAction(PasteAndGo), SIGNAL(triggered()), this, SLOT(pasteAndGo())); QTimer::singleShot(0, this, SLOT(setupEngines())); } void WebSearchBar::aboutToShowMenu() { QMenu* menu = m_boxSearchType->menu(); menu->addSeparator(); m_window->weView()->page()->runJavaScript(Scripts::getOpenSearchLinks(), WebPage::SafeJsWorld, [this, menu](const QVariant &res) { const QVariantList &list = res.toList(); Q_FOREACH (const QVariant &val, list) { const QVariantMap &link = val.toMap(); QUrl url = m_window->weView()->url().resolved(link.value(QSL("url")).toUrl()); QString title = link.value(QSL("title")).toString(); if (url.isEmpty()) continue; if (title.isEmpty()) title = m_window->weView()->title(); menu->addAction(m_window->weView()->icon(), tr("Add %1 ...").arg(title), this, SLOT(addEngineFromAction()))->setData(url); } menu->addSeparator(); menu->addAction(IconProvider::settingsIcon(), tr("Manage Search Engines"), this, SLOT(openSearchEnginesDialog())); }); } void WebSearchBar::addSuggestions(const QStringList &list) { if (qzSettings->showWSBSearchSuggestions) { QStringList list_ = list.mid(0, 6); m_completerModel->setStringList(list_); m_completer->complete(); } } void WebSearchBar::openSearchEnginesDialog() { if (!m_searchDialog) m_searchDialog = new SearchEnginesDialog(this); m_searchDialog->open(); m_searchDialog->raise(); m_searchDialog->activateWindow(); } void WebSearchBar::enableSearchSuggestions(bool enable) { Settings settings; settings.beginGroup("SearchEngines"); settings.setValue("showSuggestions", enable); settings.endGroup(); qzSettings->showWSBSearchSuggestions = enable; m_completerModel->setStringList(QStringList()); } void WebSearchBar::setupEngines() { disconnect(m_searchManager, SIGNAL(enginesChanged()), this, SLOT(setupEngines())); m_reloadingEngines = true; QString activeEngine = m_searchManager->startingEngineName(); if (m_boxSearchType->allItems().count() != 0) { activeEngine = m_activeEngine.name; } m_boxSearchType->clearItems(); foreach (const SearchEngine &en, m_searchManager->allEngines()) { ButtonWithMenu::Item item; item.icon = en.icon; item.text = en.name; QVariant v; v.setValue(en); item.userData = v; m_boxSearchType->addItem(item); if (item.text == activeEngine) { m_boxSearchType->setCurrentItem(item, false); } } searchChanged(m_boxSearchType->currentItem()); connect(m_searchManager, SIGNAL(enginesChanged()), this, SLOT(setupEngines())); m_reloadingEngines = false; } void WebSearchBar::searchChanged(const ButtonWithMenu::Item &item) { setPlaceholderText(item.text); m_completerModel->setStringList(QStringList()); m_activeEngine = item.userData.value(); m_openSearchEngine->setSuggestionsUrl(m_activeEngine.suggestionsUrl); m_openSearchEngine->setSuggestionsParameters(m_activeEngine.suggestionsParameters); m_searchManager->setActiveEngine(m_activeEngine); if (qzSettings->searchOnEngineChange && !m_reloadingEngines && !text().isEmpty()) { search(); } } void WebSearchBar::instantSearchChanged(bool enable) { Settings settings; settings.beginGroup("SearchEngines"); settings.setValue("SearchOnEngineChange", enable); settings.endGroup(); qzSettings->searchOnEngineChange = enable; } void WebSearchBar::search() { m_window->weView()->setFocus(); m_window->weView()->load(m_searchManager->searchResult(m_activeEngine, text())); } void WebSearchBar::searchInNewTab() { int index = m_window->tabWidget()->addView(QUrl()); m_window->weView(index)->setFocus(); m_window->weView(index)->load(m_searchManager->searchResult(m_activeEngine, text())); } void WebSearchBar::addEngineFromAction() { if (QAction* action = qobject_cast(sender())) { m_searchManager->addEngine(action->data().toUrl()); } } void WebSearchBar::pasteAndGo() { clear(); paste(); search(); } void WebSearchBar::contextMenuEvent(QContextMenuEvent* event) { Q_UNUSED(event) QMenu* menu = createContextMenu(); menu->setAttribute(Qt::WA_DeleteOnClose); menu->addSeparator(); QAction* act = menu->addAction(tr("Show suggestions")); act->setCheckable(true); act->setChecked(qzSettings->showWSBSearchSuggestions); connect(act, SIGNAL(triggered(bool)), this, SLOT(enableSearchSuggestions(bool))); QAction* instantSearch = menu->addAction(tr("Search when engine changed")); instantSearch->setCheckable(true); instantSearch->setChecked(qzSettings->searchOnEngineChange); connect(instantSearch, SIGNAL(triggered(bool)), this, SLOT(instantSearchChanged(bool))); // Prevent choosing first option with double rightclick QPoint pos = event->globalPos(); pos.setY(pos.y() + 1); menu->popup(pos); } void WebSearchBar::focusOutEvent(QFocusEvent* e) { if (text().isEmpty()) { QString search = m_boxSearchType->currentItem().text; setPlaceholderText(search); } LineEdit::focusOutEvent(e); } void WebSearchBar::dropEvent(QDropEvent* event) { if (event->mimeData()->hasText()) { QString dropText = event->mimeData()->text(); setText(dropText); search(); QFocusEvent event(QFocusEvent::FocusOut); LineEdit::focusOutEvent(&event); return; } LineEdit::dropEvent(event); } void WebSearchBar::keyPressEvent(QKeyEvent* event) { switch (event->key()) { case Qt::Key_V: if (event->modifiers() == (Qt::ControlModifier | Qt::ShiftModifier)) { pasteAndGo(); event->accept(); return; } break; case Qt::Key_Return: case Qt::Key_Enter: if (event->modifiers() == Qt::AltModifier) { searchInNewTab(); } else { search(); } break; case Qt::Key_Up: if (event->modifiers() == Qt::ControlModifier) { m_boxSearchType->selectPreviousItem(); } break; case Qt::Key_Down: if (event->modifiers() == Qt::ControlModifier) { m_boxSearchType->selectNextItem(); } break; default: break; } LineEdit::keyPressEvent(event); } diff --git a/src/lib/tools/scripts.cpp b/src/lib/tools/scripts.cpp index e260b1c8..fab318e3 100644 --- a/src/lib/tools/scripts.cpp +++ b/src/lib/tools/scripts.cpp @@ -1,300 +1,310 @@ /* ============================================================ * Falkon - Qt web browser * Copyright (C) 2015-2018 David Rosca * * This program is free software: you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation, either version 3 of the License, or * (at your option) any later version. * * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * * You should have received a copy of the GNU General Public License * along with this program. If not, see . * ============================================================ */ #include "scripts.h" #include "qztools.h" +#include "webpage.h" #include -QString Scripts::setupWebChannel() +QString Scripts::setupWebChannel(quint32 worldId) { - QString source = QL1S("(function() {" - "%1" + QString source = QL1S("// ==UserScript==\n" + "// %1\n" + "// ==/UserScript==\n\n" + "(function() {" + "%2" "" "function registerExternal(e) {" " window.external = e;" " if (window.external) {" " var event = document.createEvent('Event');" " event.initEvent('_falkon_external_created', true, true);" " window._falkon_external = true;" " document.dispatchEvent(event);" " }" "}" "" "if (self !== top) {" " if (top._falkon_external)" " registerExternal(top.external);" " else" " top.document.addEventListener('_falkon_external_created', function() {" " registerExternal(top.external);" " });" " return;" "}" "" "function registerWebChannel() {" " try {" " new QWebChannel(qt.webChannelTransport, function(channel) {" " var external = channel.objects.qz_object;" " external.extra = {};" " for (var key in channel.objects) {" " if (key != 'qz_object' && key.startsWith('qz_')) {" " external.extra[key.substr(3)] = channel.objects[key];" " }" " }" " registerExternal(external);" " });" " } catch (e) {" " setTimeout(registerWebChannel, 100);" " }" "}" "registerWebChannel();" "" "})()"); - return source.arg(QzTools::readAllFileContents(QSL(":/qtwebchannel/qwebchannel.js"))); + QString match; + if (worldId == WebPage::SafeJsWorld) { + match = QSL("@exclude qupzilla:*"); + } else { + match = QSL("@include qupzilla:*"); + } + return source.arg(match, QzTools::readAllFileContents(QSL(":/qtwebchannel/qwebchannel.js"))); } QString Scripts::setupFormObserver() { QString source = QL1S("(function() {" "function findUsername(inputs) {" " var usernameNames = ['user', 'name', 'login'];" " for (var i = 0; i < usernameNames.length; ++i) {" " for (var j = 0; j < inputs.length; ++j)" " if (inputs[j].type == 'text' && inputs[j].value.length && inputs[j].name.indexOf(usernameNames[i]) != -1)" " return inputs[j].value;" " }" " for (var i = 0; i < inputs.length; ++i)" " if (inputs[i].type == 'text' && inputs[i].value.length)" " return inputs[i].value;" " for (var i = 0; i < inputs.length; ++i)" " if (inputs[i].type == 'email' && inputs[i].value.length)" " return inputs[i].value;" " return '';" "}" "" "function registerForm(form) {" " form.addEventListener('submit', function() {" " var form = this;" " var data = '';" " var password = '';" " var inputs = form.getElementsByTagName('input');" " for (var i = 0; i < inputs.length; ++i) {" " var input = inputs[i];" " var type = input.type.toLowerCase();" " if (type != 'text' && type != 'password' && type != 'email')" " continue;" " if (!password && type == 'password')" " password = input.value;" " data += encodeURIComponent(input.name);" " data += '=';" " data += encodeURIComponent(input.value);" " data += '&';" " }" " if (!password)" " return;" " data = data.substring(0, data.length - 1);" " var url = window.location.href;" " var username = findUsername(inputs);" " external.autoFill.formSubmitted(url, username, password, data);" " }, true);" "}" "" "if (!document.documentElement) return;" "" "for (var i = 0; i < document.forms.length; ++i)" " registerForm(document.forms[i]);" "" "var observer = new MutationObserver(function(mutations) {" " for (var i = 0; i < mutations.length; ++i)" " for (var j = 0; j < mutations[i].addedNodes.length; ++j)" " if (mutations[i].addedNodes[j].tagName == 'FORM')" " registerForm(mutations[i].addedNodes[j]);" "});" "observer.observe(document.documentElement, { childList: true, subtree: true });" "" "})()"); return source; } QString Scripts::setCss(const QString &css) { QString source = QL1S("(function() {" "var head = document.getElementsByTagName('head')[0];" "if (!head) return;" "var css = document.createElement('style');" "css.setAttribute('type', 'text/css');" "css.appendChild(document.createTextNode('%1'));" "head.appendChild(css);" "})()"); QString style = css; style.replace(QL1S("'"), QL1S("\\'")); style.replace(QL1S("\n"), QL1S("\\n")); return source.arg(style); } QString Scripts::sendPostData(const QUrl &url, const QByteArray &data) { QString source = QL1S("(function() {" "var form = document.createElement('form');" "form.setAttribute('method', 'POST');" "form.setAttribute('action', '%1');" "var val;" "%2" "form.submit();" "})()"); QString valueSource = QL1S("val = document.createElement('input');" "val.setAttribute('type', 'hidden');" "val.setAttribute('name', '%1');" "val.setAttribute('value', '%2');" "form.appendChild(val);"); QString values; QUrlQuery query(data); const auto &queryItems = query.queryItems(QUrl::FullyDecoded); for (int i = 0; i < queryItems.size(); ++i) { const auto &pair = queryItems[i]; QString value = pair.first; QString key = pair.second; value.replace(QL1S("'"), QL1S("\\'")); key.replace(QL1S("'"), QL1S("\\'")); values.append(valueSource.arg(value, key)); } return source.arg(url.toString(), values); } QString Scripts::completeFormData(const QByteArray &data) { QString source = QL1S("(function() {" "var data = '%1'.split('&');" "var inputs = document.getElementsByTagName('input');" "" "for (var i = 0; i < data.length; ++i) {" " var pair = data[i].split('=');" " if (pair.length != 2)" " continue;" " var key = decodeURIComponent(pair[0]);" " var val = decodeURIComponent(pair[1]);" " for (var j = 0; j < inputs.length; ++j) {" " var input = inputs[j];" " var type = input.type.toLowerCase();" " if (type != 'text' && type != 'password' && type != 'email')" " continue;" " if (input.name == key)" " input.value = val;" " }" "}" "" "})()"); QString d = data; d.replace(QL1S("'"), QL1S("\\'")); return source.arg(d); } QString Scripts::getOpenSearchLinks() { QString source = QL1S("(function() {" "var out = [];" "var links = document.getElementsByTagName('link');" "for (var i = 0; i < links.length; ++i) {" " var e = links[i];" " if (e.type == 'application/opensearchdescription+xml') {" " out.push({" " url: e.href," " title: e.title" " });" " }" "}" "return out;" "})()"); return source; } QString Scripts::getAllImages() { QString source = QL1S("(function() {" "var out = [];" "var imgs = document.getElementsByTagName('img');" "for (var i = 0; i < imgs.length; ++i) {" " var e = imgs[i];" " out.push({" " src: e.src," " alt: e.alt" " });" "}" "return out;" "})()"); return source; } QString Scripts::getAllMetaAttributes() { QString source = QL1S("(function() {" "var out = [];" "var meta = document.getElementsByTagName('meta');" "for (var i = 0; i < meta.length; ++i) {" " var e = meta[i];" " out.push({" " name: e.getAttribute('name')," " content: e.getAttribute('content')," " httpequiv: e.getAttribute('http-equiv')" " });" "}" "return out;" "})()"); return source; } QString Scripts::getFormData(const QPointF &pos) { QString source = QL1S("(function() {" "var e = document.elementFromPoint(%1, %2);" "if (!e || e.tagName != 'INPUT')" " return;" "var fe = e.parentElement;" "while (fe) {" " if (fe.tagName == 'FORM')" " break;" " fe = fe.parentElement;" "}" "if (!fe)" " return;" "var res = {" " method: fe.method.toLowerCase()," " action: fe.action," " inputName: e.name," " inputs: []," "};" "for (var i = 0; i < fe.length; ++i) {" " var input = fe.elements[i];" " res.inputs.push([input.name, input.value]);" "}" "return res;" "})()"); return source.arg(pos.x()).arg(pos.y()); } diff --git a/src/lib/tools/scripts.h b/src/lib/tools/scripts.h index 04f6e46d..4cf799c3 100644 --- a/src/lib/tools/scripts.h +++ b/src/lib/tools/scripts.h @@ -1,43 +1,43 @@ /* ============================================================ * Falkon - Qt web browser -* Copyright (C) 2015-2016 David Rosca +* Copyright (C) 2015-2018 David Rosca * * This program is free software: you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation, either version 3 of the License, or * (at your option) any later version. * * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * * You should have received a copy of the GNU General Public License * along with this program. If not, see . * ============================================================ */ #ifndef SCRIPTS_H #define SCRIPTS_H #include #include "qzcommon.h" class QWebEngineView; class FALKON_EXPORT Scripts { public: - static QString setupWebChannel(); + static QString setupWebChannel(quint32 worldId); static QString setupFormObserver(); static QString setCss(const QString &css); static QString sendPostData(const QUrl &url, const QByteArray &data); static QString completeFormData(const QByteArray &data); static QString getOpenSearchLinks(); static QString getAllImages(); static QString getAllMetaAttributes(); static QString getFormData(const QPointF &pos); }; #endif // SCRIPTS_H diff --git a/src/lib/webengine/webpage.cpp b/src/lib/webengine/webpage.cpp index 544b13b9..fa252e43 100644 --- a/src/lib/webengine/webpage.cpp +++ b/src/lib/webengine/webpage.cpp @@ -1,635 +1,649 @@ /* ============================================================ * Falkon - Qt web browser * Copyright (C) 2010-2018 David Rosca * * This program is free software: you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation, either version 3 of the License, or * (at your option) any later version. * * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * * You should have received a copy of the GNU General Public License * along with this program. If not, see . * ============================================================ */ #include "webpage.h" #include "tabbedwebview.h" #include "browserwindow.h" #include "pluginproxy.h" #include "downloadmanager.h" #include "mainapplication.h" #include "checkboxdialog.h" #include "widget.h" #include "qztools.h" #include "speeddial.h" #include "autofill.h" #include "popupwebview.h" #include "popupwindow.h" #include "iconprovider.h" #include "qzsettings.h" #include "useragentmanager.h" #include "delayedfilewatcher.h" #include "searchenginesmanager.h" #include "html5permissions/html5permissionsmanager.h" #include "javascript/externaljsobject.h" #include "tabwidget.h" #include "networkmanager.h" #include "webhittestresult.h" #include "ui_jsconfirm.h" #include "ui_jsalert.h" #include "ui_jsprompt.h" #include #include #include #include #include #include #include #include #include #include #include #include QString WebPage::s_lastUploadLocation = QDir::homePath(); QUrl WebPage::s_lastUnsupportedUrl; QTime WebPage::s_lastUnsupportedUrlTime; static const bool kEnableJsOutput = qEnvironmentVariableIsSet("QUPZILLA_ENABLE_JS_OUTPUT"); static const bool kEnableJsNonBlockDialogs = qEnvironmentVariableIsSet("QUPZILLA_ENABLE_JS_NONBLOCK_DIALOGS"); WebPage::WebPage(QObject* parent) : QWebEnginePage(mApp->webProfile(), parent) , m_fileWatcher(0) , m_runningLoop(0) , m_loadProgress(-1) , m_blockAlerts(false) , m_secureStatus(false) { - QWebChannel *channel = new QWebChannel(this); - ExternalJsObject::setupWebChannel(channel, this); - setWebChannel(channel); + setupWebChannelForUrl(QUrl()); connect(this, &QWebEnginePage::loadProgress, this, &WebPage::progress); connect(this, &QWebEnginePage::loadFinished, this, &WebPage::finished); connect(this, &QWebEnginePage::urlChanged, this, &WebPage::urlChanged); connect(this, &QWebEnginePage::featurePermissionRequested, this, &WebPage::featurePermissionRequested); connect(this, &QWebEnginePage::windowCloseRequested, this, &WebPage::windowCloseRequested); connect(this, &QWebEnginePage::fullScreenRequested, this, &WebPage::fullScreenRequested); connect(this, &QWebEnginePage::renderProcessTerminated, this, &WebPage::renderProcessTerminated); connect(this, &QWebEnginePage::authenticationRequired, this, [this](const QUrl &url, QAuthenticator *auth) { mApp->networkManager()->authentication(url, auth, view()); }); connect(this, &QWebEnginePage::proxyAuthenticationRequired, this, [this](const QUrl &, QAuthenticator *auth, const QString &proxyHost) { mApp->networkManager()->proxyAuthentication(proxyHost, auth, view()); }); // Workaround QWebEnginePage not scrolling to anchors when opened in background tab m_contentsResizedConnection = connect(this, &QWebEnginePage::contentsSizeChanged, this, [this]() { const QString fragment = url().fragment(); if (!fragment.isEmpty()) { const QString src = QSL("var els = document.querySelectorAll(\"[name='%1']\"); if (els.length) els[0].scrollIntoView();"); runJavaScript(src.arg(fragment)); } disconnect(m_contentsResizedConnection); }); } WebPage::~WebPage() { mApp->plugins()->emitWebPageDeleted(this); if (m_runningLoop) { m_runningLoop->exit(1); m_runningLoop = 0; } } WebView *WebPage::view() const { return static_cast(QWebEnginePage::view()); } bool WebPage::execPrintPage(QPrinter *printer, int timeout) { QPointer loop = new QEventLoop; bool result = false; QTimer::singleShot(timeout, loop.data(), &QEventLoop::quit); print(printer, [loop, &result](bool res) { if (loop && loop->isRunning()) { result = res; loop->quit(); } }); loop->exec(); delete loop; return result; } QVariant WebPage::execJavaScript(const QString &scriptSource, quint32 worldId, int timeout) { QPointer loop = new QEventLoop; QVariant result; QTimer::singleShot(timeout, loop.data(), &QEventLoop::quit); runJavaScript(scriptSource, worldId, [loop, &result](const QVariant &res) { if (loop && loop->isRunning()) { result = res; loop->quit(); } }); loop->exec(QEventLoop::ExcludeUserInputEvents); delete loop; return result; } QPointF WebPage::mapToViewport(const QPointF &pos) const { return QPointF(pos.x() / zoomFactor(), pos.y() / zoomFactor()); } WebHitTestResult WebPage::hitTestContent(const QPoint &pos) const { return WebHitTestResult(this, pos); } void WebPage::scroll(int x, int y) { - runJavaScript(QSL("window.scrollTo(window.scrollX + %1, window.scrollY + %2)").arg(x).arg(y), WebPage::SafeJsWorld); + runJavaScript(QSL("window.scrollTo(window.scrollX + %1, window.scrollY + %2)").arg(x).arg(y), SafeJsWorld); } void WebPage::setScrollPosition(const QPointF &pos) { const QPointF v = mapToViewport(pos.toPoint()); - runJavaScript(QSL("window.scrollTo(%1, %2)").arg(v.x()).arg(v.y()), WebPage::SafeJsWorld); + runJavaScript(QSL("window.scrollTo(%1, %2)").arg(v.x()).arg(v.y()), SafeJsWorld); } bool WebPage::isRunningLoop() { return m_runningLoop; } bool WebPage::isLoading() const { return m_loadProgress < 100; } void WebPage::urlChanged(const QUrl &url) { Q_UNUSED(url) if (isLoading()) { m_blockAlerts = false; } } void WebPage::progress(int prog) { m_loadProgress = prog; bool secStatus = url().scheme() == QL1S("https"); if (secStatus != m_secureStatus) { m_secureStatus = secStatus; emit privacyChanged(secStatus); } } void WebPage::finished() { progress(100); // File scheme watcher if (url().scheme() == QLatin1String("file")) { QFileInfo info(url().toLocalFile()); if (info.isFile()) { if (!m_fileWatcher) { m_fileWatcher = new DelayedFileWatcher(this); connect(m_fileWatcher, SIGNAL(delayedFileChanged(QString)), this, SLOT(watchedFileChanged(QString))); } const QString filePath = url().toLocalFile(); if (QFile::exists(filePath) && !m_fileWatcher->files().contains(filePath)) { m_fileWatcher->addPath(filePath); } } } else if (m_fileWatcher && !m_fileWatcher->files().isEmpty()) { m_fileWatcher->removePaths(m_fileWatcher->files()); } // AutoFill m_passwordEntries = mApp->autoFill()->completePage(this, url()); } void WebPage::watchedFileChanged(const QString &file) { if (url().toLocalFile() == file) { triggerAction(QWebEnginePage::Reload); } } void WebPage::handleUnknownProtocol(const QUrl &url) { const QString protocol = url.scheme(); if (protocol == QLatin1String("mailto")) { desktopServicesOpen(url); return; } if (qzSettings->blockedProtocols.contains(protocol)) { qDebug() << "WebPage::handleUnknownProtocol Protocol" << protocol << "is blocked!"; return; } if (qzSettings->autoOpenProtocols.contains(protocol)) { desktopServicesOpen(url); return; } CheckBoxDialog dialog(QMessageBox::Yes | QMessageBox::No, view()); dialog.setDefaultButton(QMessageBox::Yes); const QString wrappedUrl = QzTools::alignTextToWidth(url.toString(), "
", dialog.fontMetrics(), 450); const QString text = tr("Falkon cannot handle %1: links. The requested link " "is
  • %2
Do you want Falkon to try " "open this link in system application?").arg(protocol, wrappedUrl); dialog.setText(text); dialog.setCheckBoxText(tr("Remember my choice for this protocol")); dialog.setWindowTitle(tr("External Protocol Request")); dialog.setIcon(QMessageBox::Question); switch (dialog.exec()) { case QMessageBox::Yes: if (dialog.isChecked()) { qzSettings->autoOpenProtocols.append(protocol); qzSettings->saveSettings(); } QDesktopServices::openUrl(url); break; case QMessageBox::No: if (dialog.isChecked()) { qzSettings->blockedProtocols.append(protocol); qzSettings->saveSettings(); } break; default: break; } } void WebPage::desktopServicesOpen(const QUrl &url) { // Open same url only once in 2 secs const int sameUrlTimeout = 2 * 1000; if (s_lastUnsupportedUrl != url || s_lastUnsupportedUrlTime.isNull() || s_lastUnsupportedUrlTime.elapsed() > sameUrlTimeout) { s_lastUnsupportedUrl = url; s_lastUnsupportedUrlTime.restart(); QDesktopServices::openUrl(url); } else { qWarning() << "WebPage::desktopServicesOpen Url" << url << "has already been opened!\n" "Ignoring it to prevent infinite loop!"; } } void WebPage::windowCloseRequested() { if (!view()) return; view()->closeView(); } void WebPage::fullScreenRequested(QWebEngineFullScreenRequest fullScreenRequest) { view()->requestFullScreen(fullScreenRequest.toggleOn()); const bool accepted = fullScreenRequest.toggleOn() == view()->isFullScreen(); if (accepted) fullScreenRequest.accept(); else fullScreenRequest.reject(); } void WebPage::featurePermissionRequested(const QUrl &origin, const QWebEnginePage::Feature &feature) { if (feature == MouseLock && view()->isFullScreen()) setFeaturePermission(origin, feature, PermissionGrantedByUser); else mApp->html5PermissionsManager()->requestPermissions(this, origin, feature); } void WebPage::renderProcessTerminated(QWebEnginePage::RenderProcessTerminationStatus terminationStatus, int exitCode) { Q_UNUSED(exitCode) if (terminationStatus == NormalTerminationStatus) return; QTimer::singleShot(0, this, [this]() { QString page = QzTools::readAllFileContents(":html/tabcrash.html"); page.replace(QL1S("%IMAGE%"), QzTools::pixmapToDataUrl(IconProvider::standardIcon(QStyle::SP_MessageBoxWarning).pixmap(45)).toString()); page.replace(QL1S("%TITLE%"), tr("Failed loading page")); page.replace(QL1S("%HEADING%"), tr("Failed loading page")); page.replace(QL1S("%LI-1%"), tr("Something went wrong while loading this page.")); page.replace(QL1S("%LI-2%"), tr("Try reloading the page or closing some tabs to make more memory available.")); page.replace(QL1S("%RELOAD-PAGE%"), tr("Reload page")); page = QzTools::applyDirectionToPage(page); load(url()); // Workaround for QtWebEngine crash setHtml(page.toUtf8(), url()); }); } +void WebPage::setupWebChannelForUrl(const QUrl &url) +{ + QWebChannel *channel = webChannel(); + if (!channel) { + channel = new QWebChannel(this); + ExternalJsObject::setupWebChannel(channel, this); + } + if (url.scheme() == QL1S("qupzilla")) { + setWebChannel(channel, UnsafeJsWorld); + } else { + setWebChannel(channel, SafeJsWorld); + } +} + bool WebPage::acceptNavigationRequest(const QUrl &url, QWebEnginePage::NavigationType type, bool isMainFrame) { if (!mApp->plugins()->acceptNavigationRequest(this, url, type, isMainFrame)) return false; const bool result = QWebEnginePage::acceptNavigationRequest(url, type, isMainFrame); if (result && isMainFrame) { const bool isWeb = url.scheme() == QL1S("http") || url.scheme() == QL1S("https"); const bool globalJsEnabled = mApp->webSettings()->testAttribute(QWebEngineSettings::JavascriptEnabled); settings()->setAttribute(QWebEngineSettings::JavascriptEnabled, isWeb ? globalJsEnabled : true); + + setupWebChannelForUrl(url); } return result; } bool WebPage::certificateError(const QWebEngineCertificateError &error) { return mApp->networkManager()->certificateError(error, view()); } QStringList WebPage::chooseFiles(QWebEnginePage::FileSelectionMode mode, const QStringList &oldFiles, const QStringList &acceptedMimeTypes) { Q_UNUSED(acceptedMimeTypes); QStringList files; QString suggestedFileName = s_lastUploadLocation; if (!oldFiles.isEmpty()) suggestedFileName = oldFiles.at(0); switch (mode) { case FileSelectOpen: files = QStringList(QzTools::getOpenFileName("WebPage-ChooseFile", view(), tr("Choose file..."), suggestedFileName)); break; case FileSelectOpenMultiple: files = QzTools::getOpenFileNames("WebPage-ChooseFile", view(), tr("Choose files..."), suggestedFileName); break; default: files = QWebEnginePage::chooseFiles(mode, oldFiles, acceptedMimeTypes); break; } if (!files.isEmpty()) s_lastUploadLocation = files.at(0); return files; } bool WebPage::hasMultipleUsernames() const { return m_passwordEntries.count() > 1; } QVector WebPage::autoFillData() const { return m_passwordEntries; } bool WebPage::javaScriptPrompt(const QUrl &securityOrigin, const QString &msg, const QString &defaultValue, QString* result) { if (!kEnableJsNonBlockDialogs) { return QWebEnginePage::javaScriptPrompt(securityOrigin, msg, defaultValue, result); } if (m_runningLoop) { return false; } ResizableFrame* widget = new ResizableFrame(view()->overlayWidget()); widget->setObjectName("jsFrame"); Ui_jsPrompt* ui = new Ui_jsPrompt(); ui->setupUi(widget); ui->message->setText(msg); ui->lineEdit->setText(defaultValue); ui->lineEdit->setFocus(); widget->resize(view()->size()); widget->show(); connect(view(), SIGNAL(viewportResized(QSize)), widget, SLOT(slotResize(QSize))); connect(ui->lineEdit, SIGNAL(returnPressed()), ui->buttonBox->button(QDialogButtonBox::Ok), SLOT(animateClick())); QEventLoop eLoop; m_runningLoop = &eLoop; connect(ui->buttonBox, SIGNAL(clicked(QAbstractButton*)), &eLoop, SLOT(quit())); if (eLoop.exec() == 1) { return result; } m_runningLoop = 0; QString x = ui->lineEdit->text(); bool _result = ui->buttonBox->clickedButtonRole() == QDialogButtonBox::AcceptRole; *result = x; delete widget; view()->setFocus(); return _result; } bool WebPage::javaScriptConfirm(const QUrl &securityOrigin, const QString &msg) { if (!kEnableJsNonBlockDialogs) { return QWebEnginePage::javaScriptConfirm(securityOrigin, msg); } if (m_runningLoop) { return false; } ResizableFrame* widget = new ResizableFrame(view()->overlayWidget()); widget->setObjectName("jsFrame"); Ui_jsConfirm* ui = new Ui_jsConfirm(); ui->setupUi(widget); ui->message->setText(msg); ui->buttonBox->button(QDialogButtonBox::Ok)->setFocus(); widget->resize(view()->size()); widget->show(); connect(view(), SIGNAL(viewportResized(QSize)), widget, SLOT(slotResize(QSize))); QEventLoop eLoop; m_runningLoop = &eLoop; connect(ui->buttonBox, SIGNAL(clicked(QAbstractButton*)), &eLoop, SLOT(quit())); if (eLoop.exec() == 1) { return false; } m_runningLoop = 0; bool result = ui->buttonBox->clickedButtonRole() == QDialogButtonBox::AcceptRole; delete widget; view()->setFocus(); return result; } void WebPage::javaScriptAlert(const QUrl &securityOrigin, const QString &msg) { Q_UNUSED(securityOrigin) if (m_blockAlerts || m_runningLoop) { return; } if (!kEnableJsNonBlockDialogs) { QString title = tr("JavaScript alert"); if (!url().host().isEmpty()) { title.append(QString(" - %1").arg(url().host())); } CheckBoxDialog dialog(QMessageBox::Ok, view()); dialog.setDefaultButton(QMessageBox::Ok); dialog.setWindowTitle(title); dialog.setText(msg); dialog.setCheckBoxText(tr("Prevent this page from creating additional dialogs")); dialog.setIcon(QMessageBox::Information); dialog.exec(); m_blockAlerts = dialog.isChecked(); return; } ResizableFrame* widget = new ResizableFrame(view()->overlayWidget()); widget->setObjectName("jsFrame"); Ui_jsAlert* ui = new Ui_jsAlert(); ui->setupUi(widget); ui->message->setText(msg); ui->buttonBox->button(QDialogButtonBox::Ok)->setFocus(); widget->resize(view()->size()); widget->show(); connect(view(), SIGNAL(viewportResized(QSize)), widget, SLOT(slotResize(QSize))); QEventLoop eLoop; m_runningLoop = &eLoop; connect(ui->buttonBox, SIGNAL(clicked(QAbstractButton*)), &eLoop, SLOT(quit())); if (eLoop.exec() == 1) { return; } m_runningLoop = 0; m_blockAlerts = ui->preventAlerts->isChecked(); delete widget; view()->setFocus(); } void WebPage::javaScriptConsoleMessage(JavaScriptConsoleMessageLevel level, const QString &message, int lineNumber, const QString &sourceID) { if (!kEnableJsOutput) { return; } switch (level) { case InfoMessageLevel: std::cout << "[I] "; break; case WarningMessageLevel: std::cout << "[W] "; break; case ErrorMessageLevel: std::cout << "[E] "; break; } std::cout << qPrintable(sourceID) << ":" << lineNumber << " " << qPrintable(message); } QWebEnginePage* WebPage::createWindow(QWebEnginePage::WebWindowType type) { TabbedWebView *tView = qobject_cast(view()); BrowserWindow *window = tView ? tView->browserWindow() : mApp->getWindow(); auto createTab = [=](Qz::NewTabPositionFlags pos) { int index = window->tabWidget()->addView(QUrl(), pos); TabbedWebView* view = window->weView(index); view->setPage(new WebPage); // Workaround focus issue when creating tab if (pos.testFlag(Qz::NT_SelectedTab)) { QPointer pview = view; QTimer::singleShot(0, this, [pview]() { if (pview && pview->webTab()->isCurrentTab()) { pview->setFocus(); } }); } return view->page(); }; switch (type) { case QWebEnginePage::WebBrowserWindow: { BrowserWindow *window = mApp->createWindow(Qz::BW_NewWindow); WebPage *page = new WebPage; window->setStartPage(page); return page; } case QWebEnginePage::WebDialog: if (!qzSettings->openPopupsInTabs) { PopupWebView* view = new PopupWebView; view->setPage(new WebPage); PopupWindow* popup = new PopupWindow(view); popup->show(); window->addDeleteOnCloseWidget(popup); return view->page(); } // else fallthrough case QWebEnginePage::WebBrowserTab: return createTab(Qz::NT_CleanSelectedTab); case QWebEnginePage::WebBrowserBackgroundTab: return createTab(Qz::NT_CleanNotSelectedTab); default: break; } return Q_NULLPTR; } diff --git a/src/lib/webengine/webpage.h b/src/lib/webengine/webpage.h index 3ba97368..007c5f0f 100644 --- a/src/lib/webengine/webpage.h +++ b/src/lib/webengine/webpage.h @@ -1,111 +1,113 @@ /* ============================================================ * Falkon - Qt web browser * Copyright (C) 2010-2018 David Rosca * * This program is free software: you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation, either version 3 of the License, or * (at your option) any later version. * * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * * You should have received a copy of the GNU General Public License * along with this program. If not, see . * ============================================================ */ #ifndef WEBPAGE_H #define WEBPAGE_H #include #include #include #include #include "qzcommon.h" #include "passwordmanager.h" class QEventLoop; class QWebEngineDownloadItem; class WebView; class WebHitTestResult; class DelayedFileWatcher; class FALKON_EXPORT WebPage : public QWebEnginePage { Q_OBJECT public: enum JsWorld { + UnsafeJsWorld = QWebEngineScript::MainWorld, SafeJsWorld = QWebEngineScript::ApplicationWorld }; explicit WebPage(QObject* parent = 0); ~WebPage(); WebView *view() const; bool execPrintPage(QPrinter *printer, int timeout = 1000); - QVariant execJavaScript(const QString &scriptSource, quint32 worldId = QWebEngineScript::MainWorld, int timeout = 500); + QVariant execJavaScript(const QString &scriptSource, quint32 worldId = UnsafeJsWorld, int timeout = 500); QPointF mapToViewport(const QPointF &pos) const; WebHitTestResult hitTestContent(const QPoint &pos) const; void scroll(int x, int y); void setScrollPosition(const QPointF &pos); bool javaScriptPrompt(const QUrl &securityOrigin, const QString &msg, const QString &defaultValue, QString* result) Q_DECL_OVERRIDE; bool javaScriptConfirm(const QUrl &securityOrigin, const QString &msg) Q_DECL_OVERRIDE; void javaScriptAlert(const QUrl &securityOrigin, const QString &msg) Q_DECL_OVERRIDE; void javaScriptConsoleMessage(JavaScriptConsoleMessageLevel level, const QString &message, int lineNumber, const QString &sourceID) override; bool hasMultipleUsernames() const; QVector autoFillData() const; bool isRunningLoop(); bool isLoading() const; signals: void privacyChanged(bool status); protected slots: void progress(int prog); void finished(); private slots: void urlChanged(const QUrl &url); void watchedFileChanged(const QString &file); void windowCloseRequested(); void fullScreenRequested(QWebEngineFullScreenRequest fullScreenRequest); void featurePermissionRequested(const QUrl &origin, const QWebEnginePage::Feature &feature); void renderProcessTerminated(RenderProcessTerminationStatus terminationStatus, int exitCode); private: + void setupWebChannelForUrl(const QUrl &url); bool acceptNavigationRequest(const QUrl &url, NavigationType type, bool isMainFrame) Q_DECL_OVERRIDE; bool certificateError(const QWebEngineCertificateError &error) Q_DECL_OVERRIDE; QStringList chooseFiles(FileSelectionMode mode, const QStringList &oldFiles, const QStringList &acceptedMimeTypes) Q_DECL_OVERRIDE; QWebEnginePage* createWindow(QWebEnginePage::WebWindowType type) Q_DECL_OVERRIDE; void handleUnknownProtocol(const QUrl &url); void desktopServicesOpen(const QUrl &url); static QString s_lastUploadLocation; static QUrl s_lastUnsupportedUrl; static QTime s_lastUnsupportedUrlTime; DelayedFileWatcher* m_fileWatcher; QEventLoop* m_runningLoop; QVector m_passwordEntries; int m_loadProgress; bool m_blockAlerts; bool m_secureStatus; QMetaObject::Connection m_contentsResizedConnection; }; #endif // WEBPAGE_H diff --git a/src/lib/webtab/searchtoolbar.cpp b/src/lib/webtab/searchtoolbar.cpp index 3e333d0a..54d41bf2 100644 --- a/src/lib/webtab/searchtoolbar.cpp +++ b/src/lib/webtab/searchtoolbar.cpp @@ -1,154 +1,154 @@ /* ============================================================ * Falkon - Qt web browser -* Copyright (C) 2010-2017 David Rosca +* Copyright (C) 2010-2018 David Rosca * * This program is free software: you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation, either version 3 of the License, or * (at your option) any later version. * * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * * You should have received a copy of the GNU General Public License * along with this program. If not, see . * ============================================================ */ #include "searchtoolbar.h" #include "tabbedwebview.h" #include "webpage.h" #include "lineedit.h" #include "ui_searchtoolbar.h" #include "iconprovider.h" #include #include SearchToolBar::SearchToolBar(WebView* view, QWidget* parent) : QWidget(parent) , ui(new Ui::SearchToolbar) , m_view(view) , m_findFlags(0) { setAttribute(Qt::WA_DeleteOnClose); ui->setupUi(this); ui->closeButton->setIcon(IconProvider::instance()->standardIcon(QStyle::SP_DialogCloseButton)); ui->next->setIcon(IconProvider::instance()->standardIcon(QStyle::SP_ArrowDown)); ui->next->setShortcut(QKeySequence("Ctrl+G")); ui->previous->setIcon(IconProvider::instance()->standardIcon(QStyle::SP_ArrowUp)); ui->previous->setShortcut(QKeySequence("Ctrl+Shift+G")); connect(ui->closeButton, SIGNAL(clicked()), this, SLOT(close())); connect(ui->lineEdit, SIGNAL(textChanged(QString)), this, SLOT(findNext())); connect(ui->lineEdit, SIGNAL(returnPressed()), this, SLOT(findNext())); connect(ui->next, SIGNAL(clicked()), this, SLOT(findNext())); connect(ui->previous, SIGNAL(clicked()), this, SLOT(findPrevious())); connect(ui->caseSensitive, SIGNAL(clicked()), this, SLOT(caseSensitivityChanged())); QShortcut* findNextAction = new QShortcut(QKeySequence("F3"), this); connect(findNextAction, SIGNAL(activated()), this, SLOT(findNext())); QShortcut* findPreviousAction = new QShortcut(QKeySequence("Shift+F3"), this); connect(findPreviousAction, SIGNAL(activated()), this, SLOT(findPrevious())); parent->installEventFilter(this); } void SearchToolBar::showMinimalInPopupWindow() { // Show only essentials widget + set minimum width ui->caseSensitive->hide(); ui->results->hide(); ui->horizontalLayout->setSpacing(2); ui->horizontalLayout->setContentsMargins(2, 6, 2, 6); setMinimumWidth(260); } void SearchToolBar::focusSearchLine() { ui->lineEdit->setFocus(); } void SearchToolBar::close() { hide(); searchText(QString()); m_view->setFocus(); deleteLater(); } void SearchToolBar::findNext() { m_findFlags = 0; updateFindFlags(); searchText(ui->lineEdit->text()); } void SearchToolBar::findPrevious() { m_findFlags = QWebEnginePage::FindBackward; updateFindFlags(); searchText(ui->lineEdit->text()); } void SearchToolBar::updateFindFlags() { if (ui->caseSensitive->isChecked()) { m_findFlags = m_findFlags | QWebEnginePage::FindCaseSensitively; } else { m_findFlags = m_findFlags & ~QWebEnginePage::FindCaseSensitively; } } void SearchToolBar::caseSensitivityChanged() { updateFindFlags(); searchText(QString()); searchText(ui->lineEdit->text()); } void SearchToolBar::searchText(const QString &text) { QPointer guard = this; m_view->findText(text, m_findFlags, [=](bool found) { if (!guard) { return; } if (ui->lineEdit->text().isEmpty()) found = true; if (!found) ui->results->setText(tr("No results found.")); else ui->results->clear(); ui->lineEdit->setProperty("notfound", QVariant(!found)); ui->lineEdit->style()->unpolish(ui->lineEdit); ui->lineEdit->style()->polish(ui->lineEdit); // Clear selection - m_view->page()->runJavaScript(QSL("window.getSelection().empty();")); + m_view->page()->runJavaScript(QSL("window.getSelection().empty();"), WebPage::SafeJsWorld); }); } bool SearchToolBar::eventFilter(QObject* obj, QEvent* event) { Q_UNUSED(obj); if (event->type() == QEvent::KeyPress && static_cast(event)->key() == Qt::Key_Escape) { close(); } return false; } SearchToolBar::~SearchToolBar() { delete ui; } diff --git a/src/plugins/GreaseMonkey/gm_script.cpp b/src/plugins/GreaseMonkey/gm_script.cpp index 82ad0cd1..801a2d2b 100644 --- a/src/plugins/GreaseMonkey/gm_script.cpp +++ b/src/plugins/GreaseMonkey/gm_script.cpp @@ -1,309 +1,309 @@ /* ============================================================ * GreaseMonkey plugin for Falkon -* Copyright (C) 2012-2017 David Rosca +* Copyright (C) 2012-2018 David Rosca * * This program is free software: you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation, either version 3 of the License, or * (at your option) any later version. * * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * * You should have received a copy of the GNU General Public License * along with this program. If not, see . * ============================================================ */ #include "gm_script.h" #include "gm_manager.h" #include "gm_downloader.h" -#include "qzregexp.h" #include "delayedfilewatcher.h" #include "mainapplication.h" +#include "webpage.h" #include #include #include #include #include GM_Script::GM_Script(GM_Manager* manager, const QString &filePath) : QObject(manager) , m_manager(manager) , m_fileWatcher(new DelayedFileWatcher(this)) , m_namespace("GreaseMonkeyNS") , m_startAt(DocumentEnd) , m_noframes(false) , m_fileName(filePath) , m_enabled(true) , m_valid(false) , m_updating(false) { parseScript(); connect(m_fileWatcher, SIGNAL(delayedFileChanged(QString)), this, SLOT(watchedFileChanged(QString))); } bool GM_Script::isValid() const { return m_valid; } QString GM_Script::name() const { return m_name; } QString GM_Script::nameSpace() const { return m_namespace; } QString GM_Script::fullName() const { return QString("%1/%2").arg(m_namespace, m_name); } QString GM_Script::description() const { return m_description; } QString GM_Script::version() const { return m_version; } QUrl GM_Script::downloadUrl() const { return m_downloadUrl; } QUrl GM_Script::updateUrl() const { return m_updateUrl; } GM_Script::StartAt GM_Script::startAt() const { return m_startAt; } bool GM_Script::noFrames() const { return m_noframes; } bool GM_Script::isEnabled() const { return m_valid && m_enabled; } void GM_Script::setEnabled(bool enable) { m_enabled = enable; } QStringList GM_Script::include() const { return m_include; } QStringList GM_Script::exclude() const { return m_exclude; } QStringList GM_Script::require() const { return m_require; } QString GM_Script::fileName() const { return m_fileName; } QWebEngineScript GM_Script::webScript() const { QWebEngineScript script; script.setSourceCode(QSL("%1\n%2").arg(m_manager->bootstrapScript(), m_script)); script.setName(fullName()); - script.setWorldId(QWebEngineScript::MainWorld); + script.setWorldId(WebPage::SafeJsWorld); script.setRunsOnSubFrames(!m_noframes); return script; } bool GM_Script::isUpdating() { return m_updating; } void GM_Script::updateScript() { if (!m_downloadUrl.isValid() || m_updating) return; m_updating = true; emit updatingChanged(m_updating); GM_Downloader *downloader = new GM_Downloader(m_downloadUrl, m_manager); downloader->updateScript(m_fileName); connect(downloader, &GM_Downloader::finished, this, [this]() { m_updating = false; emit updatingChanged(m_updating); }); connect(downloader, &GM_Downloader::error, this, [this]() { m_updating = false; emit updatingChanged(m_updating); }); downloadRequires(); } void GM_Script::watchedFileChanged(const QString &file) { if (m_fileName == file) { reloadScript(); } } void GM_Script::parseScript() { m_name.clear(); m_namespace = QSL("GreaseMonkeyNS"); m_description.clear(); m_version.clear(); m_include.clear(); m_exclude.clear(); m_require.clear(); m_downloadUrl.clear(); m_updateUrl.clear(); m_startAt = DocumentEnd; m_noframes = false; m_script.clear(); m_enabled = true; m_valid = false; QFile file(m_fileName); if (!file.open(QFile::ReadOnly)) { qWarning() << "GreaseMonkey: Cannot open file for reading" << m_fileName; return; } if (!m_fileWatcher->files().contains(m_fileName)) { m_fileWatcher->addPath(m_fileName); } const QByteArray fileData = file.readAll(); bool inMetadata = false; QTextStream stream(fileData); QString line; while (stream.readLineInto(&line)) { if (line.startsWith(QL1S("// ==UserScript=="))) { inMetadata = true; } if (line.startsWith(QL1S("// ==/UserScript=="))) { break; } if (!inMetadata) { continue; } if (!line.startsWith(QLatin1String("// @"))) { continue; } line = line.mid(3).replace(QLatin1Char('\t'), QLatin1Char(' ')); int index = line.indexOf(QLatin1Char(' ')); if (index < 0) { continue; } const QString key = line.left(index).trimmed(); const QString value = line.mid(index + 1).trimmed(); if (key.isEmpty() || value.isEmpty()) { continue; } if (key == QLatin1String("@name")) { m_name = value; } else if (key == QLatin1String("@namespace")) { m_namespace = value; } else if (key == QLatin1String("@description")) { m_description = value; } else if (key == QLatin1String("@version")) { m_version = value; } else if (key == QLatin1String("@updateURL")) { m_updateUrl = QUrl(value); } else if (key == QLatin1String("@downloadURL")) { m_downloadUrl = QUrl(value); } else if (key == QLatin1String("@include") || key == QLatin1String("@match")) { m_include.append(value); } else if (key == QLatin1String("@exclude") || key == QLatin1String("@exclude_match")) { m_exclude.append(value); } else if (key == QLatin1String("@require")) { m_require.append(value); } else if (key == QLatin1String("@run-at")) { if (value == QLatin1String("document-end")) { m_startAt = DocumentEnd; } else if (value == QLatin1String("document-start")) { m_startAt = DocumentStart; } else if (value == QLatin1String("document-idle")) { m_startAt = DocumentIdle; } } } if (!inMetadata) { qWarning() << "GreaseMonkey: File does not contain metadata block" << m_fileName; return; } if (m_include.isEmpty()) { m_include.append(QSL("*")); } const QString nspace = QCryptographicHash::hash(fullName().toUtf8(), QCryptographicHash::Md4).toHex(); const QString gmValues = m_manager->valuesScript().arg(nspace); m_script = QSL("(function(){%1\n%2\n%3\n})();").arg(gmValues, m_manager->requireScripts(m_require), fileData); m_valid = true; downloadRequires(); } void GM_Script::reloadScript() { parseScript(); m_manager->removeScript(this, false); m_manager->addScript(this); emit scriptChanged(); } void GM_Script::downloadRequires() { for (const QString &url : qAsConst(m_require)) { if (m_manager->requireScripts({url}).isEmpty()) { GM_Downloader *downloader = new GM_Downloader(QUrl(url), m_manager, GM_Downloader::DownloadRequireScript); connect(downloader, &GM_Downloader::finished, this, &GM_Script::reloadScript); } } }