| 185 | } |
| 186 | |
| 187 | void UserScriptModel::load() |
| 188 | { |
| 189 | // Load user script template |
| 190 | QFile templateFile(":/GreaseMonkeyAPI.js"); |
| 191 | if (!templateFile.exists() || !templateFile.open(QIODevice::ReadOnly)) |
| 192 | return; |
| 193 | |
| 194 | m_scriptTemplate = QString(templateFile.readAll()); |
| 195 | templateFile.close(); |
| 196 | |
| 197 | // Attempt to load statuses of user scripts, storing results in temporary hash map |
| 198 | QFile scriptMgrFile(m_settings->getPathValue(BrowserSetting::UserScriptsConfig)); |
| 199 | QHash<QString, bool> scriptStatuses; |
| 200 | if (scriptMgrFile.exists() && scriptMgrFile.open(QIODevice::ReadOnly)) |
| 201 | { |
| 202 | // Attempt to parse status file |
| 203 | QByteArray userScriptInfo = scriptMgrFile.readAll(); |
| 204 | scriptMgrFile.close(); |
| 205 | |
| 206 | // For each file, determine if it is enabled or not |
| 207 | QJsonDocument scriptInfoDoc(QJsonDocument::fromJson(userScriptInfo)); |
| 208 | QJsonObject scriptInfoObj = scriptInfoDoc.object(); |
| 209 | for (auto it = scriptInfoObj.begin(); it != scriptInfoObj.end(); ++it) |
| 210 | { |
| 211 | auto itVal = it.value(); |
| 212 | if (!itVal.isObject()) |
| 213 | continue; |
| 214 | |
| 215 | QString scriptFileName = it.key(); |
| 216 | bool status = itVal.toObject().value(QStringLiteral("enabled")).toBool(true); |
| 217 | |
| 218 | scriptStatuses[scriptFileName] = status; |
| 219 | } |
| 220 | } |
| 221 | |
| 222 | // Load user scripts |
| 223 | QDir scriptDir(m_settings->getPathValue(BrowserSetting::UserScriptsDir)); |
| 224 | if (!scriptDir.exists()) |
| 225 | scriptDir.mkpath(scriptDir.absolutePath()); |
| 226 | |
| 227 | m_userScriptDir = scriptDir.absolutePath(); |
| 228 | |
| 229 | QDir scriptDepDir(QString("%1/Dependencies").arg(m_userScriptDir)); |
| 230 | if (!scriptDepDir.exists()) |
| 231 | scriptDepDir.mkpath(scriptDepDir.absolutePath()); |
| 232 | |
| 233 | m_scriptDepDir = scriptDepDir.absolutePath(); |
| 234 | |
| 235 | QDirIterator scriptDirItr(m_userScriptDir, QDir::Files); |
| 236 | while (scriptDirItr.hasNext()) |
| 237 | { |
| 238 | UserScript script; |
| 239 | if (script.load(scriptDirItr.next(), m_scriptTemplate)) |
| 240 | { |
| 241 | auto statusIt = scriptStatuses.find(script.m_fileName); |
| 242 | if (statusIt != scriptStatuses.end()) |
| 243 | script.m_isEnabled = statusIt.value(); |
| 244 | m_scripts.push_back(script); |
no test coverage detected