Skip to content

Instantly share code, notes, and snippets.

@bensoutendijk
Last active September 2, 2020 21:22
Show Gist options
  • Save bensoutendijk/e2f350e9717f898683f54cc4bcc47510 to your computer and use it in GitHub Desktop.
Save bensoutendijk/e2f350e9717f898683f54cc4bcc47510 to your computer and use it in GitHub Desktop.
(function recordingRatePerPage() {
const config = {
recordingRules: [
['base', '*', 5],
],
allowReroll: false,
cookieKey: 'mf_recordingRules',
};
const getLocationURI = () => {
let res = '';
if (typeof config.locationProperties === 'undefined')
config.locationProperties = ['pathname'];
if (!Array.isArray(config.locationProperties))
throw new Error('Invalid location properties in config');
if (config.locationProperties.includes('protocol'))
res += window.location.protocol;
if (config.locationProperties.includes('host'))
res += window.location.host;
if (config.locationProperties.includes('pathname'))
res += window.location.pathname;
if (config.locationProperties.includes('search'))
res += window.location.search;
if (config.locationProperties.includes('hash'))
res += window.location.hash;
return res;
};
const setCookie = (key, value) => {
document.cookie = `${key}=${value}; expires=${new Date().setDate(new Date().getDate() + 1)}; path=/`;
};
const getCookie = (key) => {
const cookies = document.cookie.split(';');
const res = cookies.find((cookie) => cookie.includes(`${key}=`));
if (typeof res !== 'undefined') {
return res.split('=')[1];
}
return '';
};
function setRecordingRules(recordingRules) {
setCookie(config.cookieKey, btoa(JSON.stringify(recordingRules)));
}
function getRecordingRules() {
const res = getCookie(config.cookieKey);
if (!res)
return res;
return JSON.parse(atob(res));
}
const validate = (data) => {
if (!Array.isArray(data)) {
return false;
}
for (let i = 0; i < data.length; i += 1) {
const item = data[i];
const [rule, pattern, rate] = item;
if (typeof rule !== 'string'
|| typeof pattern !== 'string'
|| typeof rate !== 'number') {
return false;
}
}
return true;
};
const testRate = (rate) => {
return Math.floor(Math.random() * 100) < rate;
};
const testRule = (rule, pattern) => {
switch (rule) {
case 'base':
return true;
case 'equals':
if (pattern.toLowerCase() === getLocationURI().toLowerCase())
return true;
break;
case 'contains':
if (getLocationURI().toLowerCase().includes(pattern.toLowerCase()))
return true;
break;
case 'startsWith':
if (getLocationURI().toLowerCase().indexOf(pattern.toLowerCase()) === 0)
return true;
break;
case 'regex':
if (new RegExp(pattern.toLowerCase()).test(getLocationURI().toLowerCase()))
return true;
break;
default:
break;
}
return false;
};
const testPage = (data) => {
let res;
// Check for matched recording rule
for (let i = 0; i < data.length; i += 1) {
const recordingRule = data[i];
const isMatched = recordingRule[3];
if (typeof isMatched !== 'undefined') {
if (isMatched === true) {
res = true;
break;
}
if (!config.allowReroll && isMatched === false) {
res = false;
}
}
}
if (typeof res !== 'undefined') {
return res;
}
for (let i = 0; i < data.length; i += 1) {
const recordingRule = data[i];
const [rule, pattern, rate] = recordingRule;
if (testRule(rule, pattern)) {
if (testRate(rate)) {
recordingRule[3] = true;
return true;
}
recordingRule[3] = false;
}
}
return false;
};
let data = getRecordingRules();
if (!validate(config.recordingRules))
return;
if (!validate(data)) {
data = config.recordingRules;
setRecordingRules(data);
}
window.mouseflowAutoStart = testPage(data);
setRecordingRules(data);
}());
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment