1
mirror of https://invent.kde.org/network/falkon.git synced 2024-12-20 10:46:35 +01:00

GreaseMonkey: Implement include/exclude matching

Somehow I was under false impression that QtWebEngineScript
takes care of the metadata parsing. It does not, so we must
run the script only on correct sites.
This commit is contained in:
David Rosca 2016-02-29 17:18:45 +01:00
parent 4f5d3193ae
commit 46e0b9d807

View File

@ -160,6 +160,28 @@ void GM_Script::watchedFileChanged(const QString &file)
} }
} }
static QString toJavaScriptList(const QStringList &patterns)
{
QString out;
foreach (const QString &pattern, patterns) {
QString p;
if (pattern.startsWith(QL1C('/')) && pattern.endsWith(QL1C('/')) && pattern.size() > 1) {
p = pattern.mid(1, pattern.size() - 2);
} else {
p = pattern;
p.replace(QL1S("."), QL1S("\\."));
p.replace(QL1S("*"), QL1S(".*"));
}
p = QSL("'%1'").arg(p);
if (out.isEmpty()) {
out.append(p);
} else {
out.append(QL1C(',') + p);
}
}
return QSL("[%1]").arg(out);
}
void GM_Script::parseScript() void GM_Script::parseScript()
{ {
m_name.clear(); m_name.clear();
@ -268,7 +290,25 @@ void GM_Script::parseScript()
const QString nspace = QCryptographicHash::hash(fullName().toUtf8(), QCryptographicHash::Md4).toHex(); const QString nspace = QCryptographicHash::hash(fullName().toUtf8(), QCryptographicHash::Md4).toHex();
const QString gmValues = m_manager->valuesScript().arg(nspace); const QString gmValues = m_manager->valuesScript().arg(nspace);
const QString runCheck = QString(QL1S("for (var value of %1) {"
" var re = new RegExp(value);"
" if (re.test(window.location.href)) {"
" return;"
" }"
"}"
"__qz_includes = false;"
"for (var value of %2) {"
" var re = new RegExp(value);"
" if (re.test(window.location.href)) {"
" __qz_includes = true;"
" break;"
" }"
"}"
"if (!__qz_includes) {"
" return;"
"}"
"delete __qz_includes;")).arg(toJavaScriptList(m_exclude), toJavaScriptList(m_include));
m_script = QSL("(function(){%1\n%2\n%3\n})();").arg(gmValues, m_manager->requireScripts(requireList), fileData); m_script = QSL("(function(){%1\n%2\n%3\n%4\n})();").arg(runCheck, gmValues, m_manager->requireScripts(requireList), fileData);
m_valid = true; m_valid = true;
} }