-
Notifications
You must be signed in to change notification settings - Fork 26
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Adds url whitelist regex to ext options #410
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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. | ||
|
@@ -1131,32 +1155,59 @@ 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) { | ||
console.log(config); | ||
urlWhitelist = new RegExp(config.pluginConfig.urlWhitelist); | ||
options(config.useraleConfig); | ||
// TODO: tabs need a page load to apply this config change. | ||
dispatchTabMessage(config.useraleConfig); | ||
} | ||
function dispatchTabMessage(message) { | ||
browser.tabs.query({}, function (tabs) { | ||
tabs.forEach(function (tab) { | ||
browser.tabs.sendMessage(tab.id, message); | ||
}); | ||
}); | ||
} | ||
|
||
// Filter out logs with urls that do not match the regex defined in extension options. | ||
function filterUrl(log) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Code smell to have two different return types depending on conditional. Since you're not actually modifying the log, you don't need to return the log. Better to have function name like There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This function is a little wonky because it has to match the pattern expected by addcallbacks(). Callbacks are called by package log functions and modify logs or drop them if given a falsey value. I've already created a ticket to change this callback functionality to match the pattern you used for modifying headers. Once the core api is changed, we can modify how we're using it here. Here's some background on why addcallbacks is the way it is: There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Understood. |
||
if (urlWhitelist.test(log.pageUrl)) { | ||
return log; | ||
} | ||
return false; | ||
} | ||
browser.storage.local.get(defaultConfig, function (res) { | ||
// Apply url filter to logs generated by the background page. | ||
addCallbacks({ | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Again please add brief explanation of why we need add the filterUrl as a callback since in the lines right below we apply the function directly in the browser event handler. |
||
filterUrl: filterUrl | ||
}); | ||
updateConfig(res); | ||
}); | ||
browser.runtime.onMessage.addListener(function (message, sender, sendResponse) { | ||
switch (message.type) { | ||
// Handles logs rerouted from content and option scripts | ||
// Handles logs rerouted from content and option scripts. | ||
case ADD_LOG: | ||
var log$1 = message.payload; | ||
if ("tab" in sender && "id" in sender.tab) { | ||
log$1["tabId"] = sender.tab.id; | ||
} | ||
log(log$1); | ||
// Apply url filter to logs generated outside the background page. | ||
log$1 = filterUrl(log$1); | ||
if (log$1) { | ||
console.log("match"); | ||
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); | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -1100,6 +1100,7 @@ function options(newConfig) { | |
// browser is defined in firefox, but chrome uses the 'chrome' global. | ||
var browser = browser || chrome; | ||
function rerouteLog(log) { | ||
console.log("reroute"); | ||
browser.runtime.sendMessage({ | ||
type: ADD_LOG, | ||
payload: log | ||
|
@@ -1127,7 +1128,7 @@ function rerouteLog(log) { | |
*/ | ||
|
||
addCallbacks({ | ||
reroute: rerouteLog | ||
rerouteLog: rerouteLog | ||
}); | ||
function setConfig() { | ||
var config = { | ||
|
@@ -1142,13 +1143,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 | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. FYI: JS will automatically infer the key if your value of the same name. So |
||
}); | ||
}); | ||
} | ||
|
@@ -1161,6 +1166,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); | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Please add some comments to this function to help describe what's happening here at a business logic level.