feat: Allow /openurl to force use private/non-private mode (#5149)

Examples:
 - /openurl https://twitch.tv/forsen
   with the setting "Open links in incognito/private mode" enabled
   Opens https://twitch.tv/forsen in private mode
 - /openurl https://twitch.tv/forsen
   with the setting "Open links in incognito/private mode" disabled
   Opens https://twitch.tv/forsen in normal mode
 - /openurl https://twitch.tv/forsen --private
   with the setting "Open links in incognito/private mode" disabled
   Opens https://twitch.tv/forsen in private mode
 - /openurl https://twitch.tv/forsen --no-private
   with the setting "Open links in incognito/private mode" enabled
   Opens https://twitch.tv/forsen in normal mode
This commit is contained in:
pajlada 2024-02-03 12:39:05 +01:00 committed by GitHub
parent a6c450f16b
commit 0393146187
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
2 changed files with 75 additions and 4 deletions

View file

@ -28,6 +28,7 @@
- Minor: Chatters from recent-messages are now added to autocompletion. (#5116)
- Minor: Added a _System_ theme that updates according to the system's color scheme (requires Qt 6.5). (#5118)
- Minor: Added icons for newer versions of macOS. (#5148)
- Minor: Added the `--incognito/--no-incognito` options to the `/openurl` command, allowing you to override the "Open links in incognito/private mode" setting. (#5149)
- Minor: Added support for the `{input.text}` placeholder in the **Split** -> **Run a command** hotkey. (#5130)
- Bugfix: Fixed an issue where certain emojis did not send to Twitch chat correctly. (#4840)
- Bugfix: Fixed capitalized channel names in log inclusion list not being logged. (#4848)

View file

@ -24,6 +24,7 @@
#include "widgets/splits/SplitContainer.hpp"
#include "widgets/Window.hpp"
#include <QCommandLineParser>
#include <QDesktopServices>
#include <QString>
#include <QUrl>
@ -414,26 +415,95 @@ QString clearmessages(const CommandContext &ctx)
QString openURL(const CommandContext &ctx)
{
/**
* The /openurl command
* Takes a positional argument as the URL to open
*
* Accepts the option --private or --no-private (or --incognito or --no-incognito).
* These options will force the URL to be opened in private or non-private mode, regardless of the
* default incognito mode setting.
*
* Examples:
* - /openurl https://twitch.tv/forsen
* with the setting "Open links in incognito/private mode" enabled
* Opens https://twitch.tv/forsen in private mode
* - /openurl https://twitch.tv/forsen
* with the setting "Open links in incognito/private mode" disabled
* Opens https://twitch.tv/forsen in normal mode
* - /openurl https://twitch.tv/forsen --private
* with the setting "Open links in incognito/private mode" disabled
* Opens https://twitch.tv/forsen in private mode
* - /openurl https://twitch.tv/forsen --no-private
* with the setting "Open links in incognito/private mode" enabled
* Opens https://twitch.tv/forsen in normal mode
*/
if (ctx.channel == nullptr)
{
return "";
}
if (ctx.words.size() < 2)
QCommandLineParser parser;
parser.addPositionalArgument("URL", "The URL to open");
QCommandLineOption privateModeOption(
{
"private",
"incognito",
},
"Force private mode. Cannot be used together with --no-private");
QCommandLineOption noPrivateModeOption(
{
"no-private",
"no-incognito",
},
"Force non-private mode. Cannot be used together with --private");
parser.addOptions({
privateModeOption,
noPrivateModeOption,
});
parser.parse(ctx.words);
const auto &positionalArguments = parser.positionalArguments();
if (positionalArguments.isEmpty())
{
ctx.channel->addMessage(makeSystemMessage("Usage: /openurl <URL>"));
ctx.channel->addMessage(makeSystemMessage(
"Usage: /openurl <URL> [--incognito/--no-incognito]"));
return "";
}
auto urlString = parser.positionalArguments().at(0);
QUrl url = QUrl::fromUserInput(ctx.words.mid(1).join(" "));
QUrl url = QUrl::fromUserInput(urlString);
if (!url.isValid())
{
ctx.channel->addMessage(makeSystemMessage("Invalid URL specified."));
return "";
}
auto preferPrivateMode = getSettings()->openLinksIncognito.getValue();
auto forcePrivateMode = parser.isSet(privateModeOption);
auto forceNonPrivateMode = parser.isSet(noPrivateModeOption);
if (forcePrivateMode && forceNonPrivateMode)
{
ctx.channel->addMessage(makeSystemMessage(
"Error: /openurl may only be called with --incognito or "
"--no-incognito, not both at the same time."));
return "";
}
bool usePrivateMode = false;
if (forceNonPrivateMode)
{
usePrivateMode = false;
}
else if (supportsIncognitoLinks() &&
(forcePrivateMode || preferPrivateMode))
{
usePrivateMode = true;
}
bool res = false;
if (supportsIncognitoLinks() && getSettings()->openLinksIncognito)
if (usePrivateMode)
{
res = openLinkIncognito(url.toString(QUrl::FullyEncoded));
}