diff options
Diffstat (limited to 'src/core/renderer/user_resource_controller.cpp')
-rw-r--r-- | src/core/renderer/user_resource_controller.cpp | 28 |
1 files changed, 26 insertions, 2 deletions
diff --git a/src/core/renderer/user_resource_controller.cpp b/src/core/renderer/user_resource_controller.cpp index bae1d95dc..eed520876 100644 --- a/src/core/renderer/user_resource_controller.cpp +++ b/src/core/renderer/user_resource_controller.cpp @@ -58,6 +58,8 @@ #include "type_conversion.h" #include "user_script.h" +#include <QRegularExpression> + Q_GLOBAL_STATIC(UserResourceController, qt_webengine_userResourceController) static content::RenderView * const globalScriptsIndex = 0; @@ -65,6 +67,28 @@ static content::RenderView * const globalScriptsIndex = 0; // Scripts meant to run after the load event will be run 500ms after DOMContentLoaded if the load event doesn't come within that delay. static const int afterLoadTimeout = 500; +static bool regexMatchesURL(const std::string &pat, const GURL &url) { + QRegularExpression qre(QtWebEngineCore::toQt(pat)); + qre.setPatternOptions(QRegularExpression::CaseInsensitiveOption); + if (!qre.isValid()) + return false; + return qre.match(QtWebEngineCore::toQt(url.spec())).hasMatch(); +} + +static bool includeRuleMatchesURL(const std::string &pat, const GURL &url) +{ + // Match patterns for greasemonkey's @include and @exclude rules which can + // be either strings with wildcards or regular expressions. + if (pat.front() == '/' && pat.back() == '/') { + std::string re(++pat.cbegin(), --pat.cend()); + if (regexMatchesURL(re, url)) + return true; + } else if (base::MatchPattern(url.spec(), pat)) { + return true; + } + return false; +} + static bool scriptMatchesURL(const UserScriptData &scriptData, const GURL &url) { // Logic taken from Chromium (extensions/common/user_script.cc) bool matchFound; @@ -82,7 +106,7 @@ static bool scriptMatchesURL(const UserScriptData &scriptData, const GURL &url) if (!scriptData.globs.empty()) { matchFound = false; for (auto it = scriptData.globs.begin(), end = scriptData.globs.end(); it != end; ++it) { - if (base::MatchPattern(url.spec(), *it)) + if (includeRuleMatchesURL(*it, url)) matchFound = true; } if (!matchFound) @@ -91,7 +115,7 @@ static bool scriptMatchesURL(const UserScriptData &scriptData, const GURL &url) if (!scriptData.excludeGlobs.empty()) { for (auto it = scriptData.excludeGlobs.begin(), end = scriptData.excludeGlobs.end(); it != end; ++it) { - if (base::MatchPattern(url.spec(), *it)) + if (includeRuleMatchesURL(*it, url)) return false; } } |