Skip to content

Commit

Permalink
Adds url whitelist regex to ext options
Browse files Browse the repository at this point in the history
  • Loading branch information
Jyyjy committed Feb 7, 2024
1 parent 5ab92db commit 4c700b4
Show file tree
Hide file tree
Showing 6 changed files with 117 additions and 27 deletions.
58 changes: 51 additions & 7 deletions build/UserALEWebExtension/background.js
Original file line number Diff line number Diff line change
Expand Up @@ -428,6 +428,30 @@ var intervalCounter;
var intervalLog;
var cbHandlers = {};

/**
* Adds named callbacks to be executed when logging.
* @param {Object } newCallbacks An object containing named callback functions.
*/
function addCallbacks() {
for (var _len = arguments.length, newCallbacks = new Array(_len), _key = 0; _key < _len; _key++) {
newCallbacks[_key] = arguments[_key];
}
newCallbacks.forEach(function (source) {
var descriptors = Object.keys(source).reduce(function (descriptors, key) {
descriptors[key] = Object.getOwnPropertyDescriptor(source, key);
return descriptors;
}, {});
Object.getOwnPropertySymbols(source).forEach(function (sym) {
var descriptor = Object.getOwnPropertyDescriptor(source, sym);
if (descriptor.enumerable) {
descriptors[sym] = descriptor;
}
});
Object.defineProperties(cbHandlers, descriptors);
});
return cbHandlers;
}

/**
* Assigns the config and log container to be used by the logging functions.
* @param {Array} newLogs Log container.
Expand Down Expand Up @@ -1131,28 +1155,48 @@ var defaultConfig = {
authHeader: null,
toolName: 'useralePlugin',
version: version
},
pluginConfig: {
// Default to a regex that will match no string
urlWhitelist: '(?!x)x'
}
};
browser.storage.local.get(defaultConfig, function (res) {
options(res.useraleConfig);
});
var urlWhitelist;
function updateConfig(config) {
urlWhitelist = new RegExp(config.pluginConfig.urlWhitelist);
options(config.useraleConfig);
dispatchTabMessage(config.useraleConfig);
}
function dispatchTabMessage(message) {
browser.tabs.query({}, function (tabs) {
tabs.forEach(function (tab) {
browser.tabs.sendMessage(tab.id, message);
});
});
}
function filterUrl(log) {
if (urlWhitelist.test(log.pageUrl)) {
return log;
}
return false;
}
browser.storage.local.get(defaultConfig, function (res) {
addCallbacks({
filterUrl: filterUrl
});
updateConfig(res);
});
browser.runtime.onMessage.addListener(function (message) {
switch (message.type) {
// Handles logs rerouted from content and option scripts
case ADD_LOG:
log(message.payload);
var log$1 = filterUrl(message.payload);
if (log$1) {
log(log$1);
}
break;
case CONFIG_CHANGE:
console.log(message);
options(message.payload);
dispatchTabMessage(message);
updateConfig(message.payload);
break;
default:
console.log('got unknown message type ', message);
Expand Down
15 changes: 11 additions & 4 deletions build/UserALEWebExtension/options.js
Original file line number Diff line number Diff line change
Expand Up @@ -1142,13 +1142,17 @@ function setConfig() {
if (config.userId && password) {
config.authHeader = "Basic " + btoa("".concat(config.userId, ":").concat(password));
}
browser.storage.local.set({
useraleConfig: config
}, function () {
var payload = {
useraleConfig: config,
pluginConfig: {
urlWhitelist: document.getElementById("filter").value
}
};
browser.storage.local.set(payload, function () {
options(config);
browser.runtime.sendMessage({
type: CONFIG_CHANGE,
payload: config
payload: payload
});
});
}
Expand All @@ -1161,6 +1165,9 @@ function getConfig() {
document.getElementById("tool").value = config.toolName;
document.getElementById("version").value = config.version;
});
browser.storage.local.get("pluginConfig", function (res) {
document.getElementById("filter").value = res.pluginConfig.urlWhitelist;
});
}
document.addEventListener("DOMContentLoaded", getConfig);
document.addEventListener("submit", setConfig);
Expand Down
4 changes: 4 additions & 0 deletions build/UserALEWebExtension/optionsPage.html
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,10 @@ <h1>Options</h1>
<input id="version"/>
<br/>

<label>URL whitelist regex:</label>
<input id="filter"/>
<br/>

<div align="right">
<button type="submit">Save</button>
</div>
Expand Down
51 changes: 37 additions & 14 deletions src/UserALEWebExtension/background.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,17 +24,27 @@ import * as userale from '../main.js';
import { browser } from './globals.js';

// Initalize userale plugin options
const defaultConfig = {useraleConfig: {
url: 'http://localhost:8000',
userId: 'pluginUser',
authHeader: null,
toolName: 'useralePlugin',
version: userale.version,
}};
const defaultConfig = {
useraleConfig: {
url: 'http://localhost:8000',
userId: 'pluginUser',
authHeader: null,
toolName: 'useralePlugin',
version: userale.version,
},
pluginConfig: {
// Default to a regex that will match no string
urlWhitelist: '(?!x)x'
}
};

browser.storage.local.get(defaultConfig, (res) => {
userale.options(res.useraleConfig);
});
var urlWhitelist;

function updateConfig(config) {
urlWhitelist = new RegExp(config.pluginConfig.urlWhitelist);
userale.options(config.useraleConfig);
dispatchTabMessage(config.useraleConfig);
}

function dispatchTabMessage(message) {
browser.tabs.query({}, function (tabs) {
Expand All @@ -44,17 +54,30 @@ function dispatchTabMessage(message) {
});
}

function filterUrl(log) {
if(urlWhitelist.test(log.pageUrl)) {
return log
}
return false;
}

browser.storage.local.get(defaultConfig, (res) => {
userale.addCallbacks({filterUrl:filterUrl});
updateConfig(res);
});

browser.runtime.onMessage.addListener(function (message) {
switch (message.type) {
// Handles logs rerouted from content and option scripts
case MessageTypes.ADD_LOG:
userale.log(message.payload);
let log = filterUrl(message.payload);
if(log) {
userale.log(log);
}
break;

case MessageTypes.CONFIG_CHANGE:
console.log(message);
userale.options(message.payload);
dispatchTabMessage(message);
updateConfig(message.payload);
break;

default:
Expand Down
12 changes: 10 additions & 2 deletions src/UserALEWebExtension/options.js
Original file line number Diff line number Diff line change
Expand Up @@ -39,9 +39,14 @@ function setConfig() {
config.authHeader = "Basic " + btoa(`${config.userId}:${password}`);
}

browser.storage.local.set({useraleConfig: config}, () => {
let payload = {
useraleConfig: config,
pluginConfig: {urlWhitelist: document.getElementById("filter").value}
};

browser.storage.local.set(payload, () => {
userale.options(config);
browser.runtime.sendMessage({ type: MessageTypes.CONFIG_CHANGE, payload: config });
browser.runtime.sendMessage({ type: MessageTypes.CONFIG_CHANGE, payload: payload });
});
}

Expand All @@ -55,6 +60,9 @@ function getConfig() {
document.getElementById("tool").value = config.toolName;
document.getElementById("version").value = config.version;
});
browser.storage.local.get("pluginConfig", (res) => {
document.getElementById("filter").value = res.pluginConfig.urlWhitelist;
});
}

document.addEventListener("DOMContentLoaded", getConfig);
Expand Down
4 changes: 4 additions & 0 deletions src/UserALEWebExtension/optionsPage.html
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,10 @@ <h1>Options</h1>
<input id="version"/>
<br/>

<label>URL whitelist regex:</label>
<input id="filter"/>
<br/>

<div align="right">
<button type="submit">Save</button>
</div>
Expand Down

0 comments on commit 4c700b4

Please sign in to comment.