Add /openurl command (#2926)

Usage: `/openurl <URL>`. Opens the provided URL in the browser.

Co-authored-by: Rasmus Karlsson <rasmus.karlsson@pajlada.com>
This commit is contained in:
Tal Neoran 2021-06-27 14:40:44 +03:00 committed by GitHub
parent c722f085d0
commit 991892ee76
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
4 changed files with 41 additions and 3 deletions

View file

@ -2,6 +2,8 @@
## Unversioned ## Unversioned
- Minor: Added `/openurl` command. Usage: `/openurl <URL>`. Opens the provided URL in the browser. (#2461, #2926)
## 2.3.3 ## 2.3.3
- Major: Added username autocompletion popup menu when typing usernames with an @ prefix. (#1979, #2866) - Major: Added username autocompletion popup menu when typing usernames with an @ prefix. (#1979, #2866)

View file

@ -18,6 +18,7 @@
#include "util/CombinePath.hpp" #include "util/CombinePath.hpp"
#include "util/FormatTime.hpp" #include "util/FormatTime.hpp"
#include "util/Helpers.hpp" #include "util/Helpers.hpp"
#include "util/IncognitoBrowser.hpp"
#include "util/StreamLink.hpp" #include "util/StreamLink.hpp"
#include "util/Twitch.hpp" #include "util/Twitch.hpp"
#include "widgets/Window.hpp" #include "widgets/Window.hpp"
@ -786,6 +787,39 @@ void CommandController::initialize(Settings &, Paths &paths)
} }
return ""; return "";
}); });
this->registerCommand("/openurl", [](const QStringList &words,
const ChannelPtr channel) {
if (words.size() < 2)
{
channel->addMessage(makeSystemMessage("Usage: /openurl <URL>."));
return "";
}
QUrl url = QUrl::fromUserInput(words.mid(1).join(" "));
if (!url.isValid())
{
channel->addMessage(makeSystemMessage("Invalid URL specified."));
return "";
}
bool res = false;
if (supportsIncognitoLinks() && getSettings()->openLinksIncognito)
{
res = openLinkIncognito(url.toString(QUrl::FullyEncoded));
}
else
{
res = QDesktopServices::openUrl(url);
}
if (!res)
{
channel->addMessage(makeSystemMessage("Could not open URL."));
}
return "";
});
} }
void CommandController::save() void CommandController::save()

View file

@ -84,12 +84,14 @@ bool supportsIncognitoLinks()
#endif #endif
} }
void openLinkIncognito(const QString &link) bool openLinkIncognito(const QString &link)
{ {
#ifdef Q_OS_WIN #ifdef Q_OS_WIN
auto command = getCommand(link); auto command = getCommand(link);
QProcess::startDetached(command); return QProcess::startDetached(command);
#else
return false;
#endif #endif
} }

View file

@ -5,6 +5,6 @@
namespace chatterino { namespace chatterino {
bool supportsIncognitoLinks(); bool supportsIncognitoLinks();
void openLinkIncognito(const QString &link); bool openLinkIncognito(const QString &link);
} // namespace chatterino } // namespace chatterino