Fix whitelist matching issues

This PR will add features below and closes #93

- ignore trailing `/` or `@@`
- ignore the `http://` or `https://` prefixes on whitelisted URLs
- ignore case sensitivity in URL
This commit is contained in:
Cengiz Ilerler
2021-01-06 21:13:17 -05:00
committed by GitHub
parent f6ebcd4ed6
commit f63e5b9519

View File

@@ -146,35 +146,46 @@ class WakaTimeCore {
getHeartbeat(url, list) { getHeartbeat(url, list) {
var lines = list.split('\n'); var lines = list.split('\n');
for (var i = 0; i < lines.length; i ++) { for (var i = 0; i < lines.length; i++) {
// Trim all lines from the list one by one // strip (http:// or https://) and trailing (`/` or `@@`)
var cleanLine = lines[i].trim(); var line = lines[i];
var cleanLine = line.trim().replace(/(\/|@@)$/, '').replace(/^(?:https?:\/\/)?/i, '');
// If by any chance one line in the list is empty, ignore it var emptyLine = cleanLine === '';
if (cleanLine === '') { if (emptyLine) {
continue; continue;
} }
var projectIndicatorCharacters = '@@';
// If url contains the current line return object var projectIndicatorIndex = cleanLine.lastIndexOf(projectIndicatorCharacters);
if (url.indexOf(cleanLine.split('@@')[0]) > -1) { var projectIndicatorExists = projectIndicatorIndex > -1;
if (cleanLine.split('@@')[1]) { var projectName = null;
return { var urlFromLine = cleanLine;
url: cleanLine.split('@@')[0], if (projectIndicatorExists) {
project: cleanLine.split('@@')[1] var start = projectIndicatorIndex + projectIndicatorCharacters.length;
}; projectName = cleanLine.substring(start);
} urlFromLine = cleanLine.replace(cleanLine.substring(projectIndicatorIndex), '').replace(/\/$/, '');
else { }
return { var schemaHttpExists = url.match(/^http:\/\//i);
url: cleanLine.split('@@')[0], var schemaHttpsExists = url.match(/^https:\/\//i);
project: null, var schema = '';
}; if (schemaHttpExists) {
} schema = 'http://';
} }
if (schemaHttpsExists) {
schema = 'https://';
}
var cleanUrl = url.trim().replace(/(\/|@@)$/, '').replace(/^(?:https?:\/\/)?/i, '');
var startsWithUrl = cleanUrl.toLowerCase().indexOf(urlFromLine.toLowerCase()) > -1;
if (startsWithUrl) {
return {
url: schema + urlFromLine,
project: projectName
};
}
} }
return { return {
url: null, url: null,
project: null, project: null,
}; };
} }