| 66 | } |
| 67 | |
| 68 | bool UserScript::load(const QString &file, const QString &templateData) |
| 69 | { |
| 70 | QFile f(file); |
| 71 | if (!f.exists() || !f.open(QIODevice::ReadOnly | QIODevice::Text)) |
| 72 | return false; |
| 73 | |
| 74 | m_dependencyData.clear(); |
| 75 | m_fileName = file; |
| 76 | |
| 77 | // Read file line by line, adding contents to local data buffer and initially parsing the metadata block |
| 78 | bool foundMetaDataStart = false, foundMetaDataEnd = false; |
| 79 | QRegularExpression metaDataStart("// ==UserScript=="), |
| 80 | metaDataEnd("// ==/UserScript=="), |
| 81 | keyValuePair("// @([\\w-]+)( +)?([^\\n]*)?"); |
| 82 | QByteArray metaData; |
| 83 | QByteArray scriptData; |
| 84 | |
| 85 | QTextStream stream(&f); |
| 86 | while (!stream.atEnd()) |
| 87 | { |
| 88 | QString line = stream.readLine(); |
| 89 | if (!foundMetaDataStart) |
| 90 | foundMetaDataStart = metaDataStart.match(line).hasMatch(); |
| 91 | else if (!foundMetaDataEnd) |
| 92 | { |
| 93 | QRegularExpressionMatch keyValMatch = keyValuePair.match(line); |
| 94 | if (keyValMatch.hasMatch()) |
| 95 | { |
| 96 | QString key = keyValMatch.captured(1), value = (keyValMatch.lastCapturedIndex() > 1 ? keyValMatch.captured(3) : QString()); |
| 97 | if (key.compare("name") == 0) |
| 98 | m_name = value; |
| 99 | else if (key.compare("namespace") == 0) |
| 100 | m_namespace = value; |
| 101 | else if (key.compare("description") == 0) |
| 102 | m_description = value; |
| 103 | else if (key.compare("version") == 0) |
| 104 | m_version = value; |
| 105 | else if (key.compare("noframes") == 0) |
| 106 | m_noSubFrames = true; |
| 107 | else if (key.compare("include") == 0) |
| 108 | m_includes.push_back(getRegExp(value)); |
| 109 | else if (key.compare("exclude") == 0) |
| 110 | m_excludes.push_back(getRegExp(value)); |
| 111 | else if (key.compare("match") == 0) |
| 112 | m_includes.push_back(CommonUtil::getRegExpForMatchPattern(value)); |
| 113 | else if (key.compare("require") == 0) |
| 114 | m_dependencies.push_back(value); |
| 115 | else if (key.compare("run-at") == 0) |
| 116 | { |
| 117 | if (value.compare("document-end") == 0) |
| 118 | m_injectionTime = ScriptInjectionTime::DocumentEnd; |
| 119 | else if (value.compare("document-start") == 0) |
| 120 | m_injectionTime = ScriptInjectionTime::DocumentStart; |
| 121 | else if (value.compare("document-idle") == 0) |
| 122 | m_injectionTime = ScriptInjectionTime::DocumentIdle; |
| 123 | } |
| 124 | |
| 125 | metaData.append(line.replace("'", "\\'")); |
no test coverage detected