opening links in private browsing mode (windows)

tested on firefox, chrome and internet explorer
This commit is contained in:
fourtf
2018-10-16 16:07:59 +02:00
parent bf8bf37eab
commit d14a4df9e3
4 changed files with 76 additions and 64 deletions
+64 -43
View File
@@ -1,40 +1,50 @@
#include "incognitobrowser.hpp" #include "IncognitoBrowser.hpp"
#include <QProcess> #include <QProcess>
#include <QRegularExpression>
#include <QSettings> #include <QSettings>
#include <QVariant> #include <QVariant>
#include "debug/Log.hpp" #include "debug/Log.hpp"
#ifdef Q_OS_WIN
namespace chatterino { namespace chatterino {
namespace { namespace {
QString getAppPath(const QString &executableName) #ifdef Q_OS_WIN
QString injectPrivateSwitch(QString command)
{ {
auto paths = QStringList{ // list of command line switches to turn on private browsing in browsers
// clang-format off static auto switches = std::vector<std::pair<QString, QString>>{
"HKEY_CURRENT_USER\\SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\App Paths\\" + executableName, {"firefox", "-private-window"}, {"chrome", "-incognito"},
"HKEY_LOCAL_MACHINE\\SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\App Paths\\" + executableName {"vivaldi", "--private"}, {"opera", "-newprivatetab"},
// clang-format on {"iexplore", "-private"},
}; };
for (const auto &path : paths) { // transform into regex and replacement string
auto val = QSettings(path, QSettings::NativeFormat) std::vector<std::pair<QRegularExpression, QString>> replacers;
.value("Default") for (const auto &switch_ : switches) {
.toString(); replacers.emplace_back(
if (!val.isNull()) { QRegularExpression("(" + switch_.first + "\\.exe\"?).*"),
return val; "\\1 " + switch_.second);
log("(" + switch_.first + "\\.exe\"?).*");
}
// try to find matching regex and apply it
for (const auto &replacement : replacers) {
if (replacement.first.match(command).hasMatch()) {
command.replace(replacement.first, replacement.second);
return command;
} }
} }
// couldn't match any browser -> unknown browser
return QString(); return QString();
} }
} // namespace
void openLinkIncognito(const QString &link) QString getCommand(const QString &link)
{ {
auto browserChoice = QSettings("HKEY_CURRENT_" // get default browser prog id
auto browserId = QSettings("HKEY_CURRENT_"
"USER\\Software\\Microsoft\\Windows\\Shell\\" "USER\\Software\\Microsoft\\Windows\\Shell\\"
"Associations\\UrlAssociatio" "Associations\\UrlAssociatio"
"ns\\http\\UserChoice", "ns\\http\\UserChoice",
@@ -42,33 +52,44 @@ void openLinkIncognito(const QString &link)
.value("Progid") .value("Progid")
.toString(); .toString();
if (!browserChoice.isNull()) { // get default browser start command
if (browserChoice == "FirefoxURL") { auto command = QSettings("HKEY_CLASSES_ROOT\\" + browserId +
// Firefox "\\shell\\open\\command",
auto path = getAppPath("firefox.exe"); QSettings::NativeFormat)
.value("Default")
.toString();
if (command.isNull()) return QString();
QProcess::startDetached(path, {"-private-window", link}); log(command);
} else if (browserChoice == "ChromeHTML") {
// Chrome
auto path = getAppPath("chrome.exe");
QProcess::startDetached(path, {"-incognito", link}); // inject switch to enable private browsing
} command = injectPrivateSwitch(command);
// Possible implementation for MS Edge. if (command.isNull()) return QString();
// Doesn't quite work yet.
/* // link
else if (browserChoice == "AppXq0fevzme2pys62n3e0fbqa7peapykr8v") { command += " " + link;
// Edge
QProcess::startDetached("C:\\Windows\\System32\\cmd.exe", return command;
{"/c", "start",
"shell:AppsFolder\Microsoft.MicrosoftEdge_"
"8wekyb3d8bbwe!MicrosoftEdge",
"-private", link});
}
*/
} }
#endif
} // namespace
bool supportsIncognitoLinks()
{
#ifdef Q_OS_WIN
return !getCommand("").isNull();
#else
return false;
#endif
}
void openLinkIncognito(const QString &link)
{
#ifdef Q_OS_WIN
auto command = getCommand(link);
QProcess::startDetached(command);
#endif
} }
} // namespace chatterino } // namespace chatterino
#endif
+4 -8
View File
@@ -2,13 +2,9 @@
#include <QtGlobal> #include <QtGlobal>
#ifdef Q_OS_WIN
// only supported on windows right now
# define INCOGNITO_LINKS_SUPPORTED
namespace chatterino { namespace chatterino {
void openLinkIncognito(const QString &link);
}
#endif bool supportsIncognitoLinks();
void openLinkIncognito(const QString &link);
} // namespace chatterino
+7 -9
View File
@@ -16,6 +16,7 @@
#include "singletons/Theme.hpp" #include "singletons/Theme.hpp"
#include "singletons/WindowManager.hpp" #include "singletons/WindowManager.hpp"
#include "util/DistanceBetweenPoints.hpp" #include "util/DistanceBetweenPoints.hpp"
#include "util/IncognitoBrowser.hpp"
#include "widgets/Scrollbar.hpp" #include "widgets/Scrollbar.hpp"
#include "widgets/TooltipWidget.hpp" #include "widgets/TooltipWidget.hpp"
#include "widgets/dialogs/UserInfoPopup.hpp" #include "widgets/dialogs/UserInfoPopup.hpp"
@@ -1222,6 +1223,10 @@ void ChannelView::addContextMenuItems(
menu->addAction("Open link", menu->addAction("Open link",
[url] { QDesktopServices::openUrl(QUrl(url)); }); [url] { QDesktopServices::openUrl(QUrl(url)); });
if (supportsIncognitoLinks()) {
menu->addAction("Open link incognito",
[url] { openLinkIncognito(url); });
}
menu->addAction("Copy link", menu->addAction("Copy link",
[url] { QApplication::clipboard()->setText(url); }); [url] { QApplication::clipboard()->setText(url); });
@@ -1256,15 +1261,8 @@ void ChannelView::addContextMenuItems(
R"(^(?:https?:\/\/)?(?:www\.|go\.)?twitch\.tv\/(?<username>[a-z0-9_]{3,}))", R"(^(?:https?:\/\/)?(?:www\.|go\.)?twitch\.tv\/(?<username>[a-z0-9_]{3,}))",
QRegularExpression::CaseInsensitiveOption); QRegularExpression::CaseInsensitiveOption);
static QSet<QString> ignoredUsernames{ static QSet<QString> ignoredUsernames{
"videos", "videos", "settings", "directory", "jobs", "friends",
"settings", "inventory", "payments", "subscriptions", "messages",
"directory",
"jobs",
"friends",
"inventory",
"payments",
"subscriptions",
"messages",
}; };
auto twitchMatch = auto twitchMatch =
+1 -4
View File
@@ -469,10 +469,7 @@ void SplitContainer::paintEvent(QPaintEvent *)
if (notebook != nullptr) { if (notebook != nullptr) {
if (notebook->getPageCount() > 1) { if (notebook->getPageCount() > 1) {
text += text += "\n\nAfter adding hold <Ctrl+Alt> to move or split it.";
"\n\nTip: After adding a split you can hold <Ctrl+Alt> to "
"move it or split it "
"further.";
} }
} }