This is an automated email from the ASF dual-hosted git repository.

jky pushed a commit to branch test
in repository https://gitbox.apache.org/repos/asf/flagon-useralejs.git

commit 92e8a5d7fed56aa296588ab03bb561f4e905fad0
Author: Jason Young <[email protected]>
AuthorDate: Wed Feb 7 17:51:37 2024 -0500

    Adds url whitelist regex to ext options
---
 build/UserALEWebExtension/background.js    | 90 ++++++++++++++++++++++--------
 build/UserALEWebExtension/options.js       | 15 +++--
 build/UserALEWebExtension/optionsPage.html |  4 ++
 src/UserALEWebExtension/background.js      | 63 ++++++++++++++++-----
 src/UserALEWebExtension/options.js         | 12 +++-
 src/UserALEWebExtension/optionsPage.html   |  4 ++
 6 files changed, 145 insertions(+), 43 deletions(-)

diff --git a/build/UserALEWebExtension/background.js 
b/build/UserALEWebExtension/background.js
index 82a41c0..42a3550 100644
--- a/build/UserALEWebExtension/background.js
+++ b/build/UserALEWebExtension/background.js
@@ -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.
@@ -1082,21 +1106,21 @@ function log(customLog) {
   }
 }
 
-/*
-* Licensed to the Apache Software Foundation (ASF) under one or more
-* contributor license agreements.  See the NOTICE file distributed with
-    * this work for additional information regarding copyright ownership.
-* The ASF licenses this file to You under the Apache License, Version 2.0
-* (the "License"); you may not use this file except in compliance with
-    * the License.  You may obtain a copy of the License at
-*
-*   http://www.apache.org/licenses/LICENSE-2.0
-*
-* Unless required by applicable law or agreed to in writing, software
-* distributed under the License is distributed on an "AS IS" BASIS,
-* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
-* See the License for the specific language governing permissions and
-* limitations under the License.
+/*
+* Licensed to the Apache Software Foundation (ASF) under one or more
+* contributor license agreements.  See the NOTICE file distributed with
+    * this work for additional information regarding copyright ownership.
+* The ASF licenses this file to You under the Apache License, Version 2.0
+* (the "License"); you may not use this file except in compliance with
+    * the License.  You may obtain a copy of the License at
+*
+*   http://www.apache.org/licenses/LICENSE-2.0
+*
+* Unless required by applicable law or agreed to in writing, software
+* distributed under the License is distributed on an "AS IS" BASIS,
+* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+* See the License for the specific language governing permissions and
+* limitations under the License.
 */
 
 
@@ -1131,11 +1155,18 @@ 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) {
@@ -1143,20 +1174,33 @@ function dispatchTabMessage(message) {
     });
   });
 }
-browser.runtime.onMessage.addListener(function (message, sender, sendResponse) 
{
+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:
-      var log$1 = message.payload;
+      var var log$1 = filterUrl$1 = message.payload;
       if ("tab" in sender && "id" in sender.tab) {
         log$1["tabId"] = sender.tab.id;
       }
       log(log$1);
+      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);
diff --git a/build/UserALEWebExtension/options.js 
b/build/UserALEWebExtension/options.js
index 8528ad8..628b79e 100644
--- a/build/UserALEWebExtension/options.js
+++ b/build/UserALEWebExtension/options.js
@@ -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
     });
   });
 }
@@ -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);
diff --git a/build/UserALEWebExtension/optionsPage.html 
b/build/UserALEWebExtension/optionsPage.html
index ff92cf0..9a3f363 100644
--- a/build/UserALEWebExtension/optionsPage.html
+++ b/build/UserALEWebExtension/optionsPage.html
@@ -45,6 +45,10 @@
     <input id="version"/>
     <br/>
 
+    <label>URL whitelist regex:</label>
+    <input id="filter"/>
+    <br/>
+
     <div align="right">
     <button type="submit">Save</button>
     </div>
diff --git a/src/UserALEWebExtension/background.js 
b/src/UserALEWebExtension/background.js
index 9e70d06..8c33b3f 100644
--- a/src/UserALEWebExtension/background.js
+++ b/src/UserALEWebExtension/background.js
@@ -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) {
@@ -44,6 +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);
+});
+
+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, sender, sendResponse) 
{
   switch (message.type) {
     // Handles logs rerouted from content and option scripts 
@@ -52,13 +86,14 @@ browser.runtime.onMessage.addListener(function (message, 
sender, sendResponse) {
       if("tab" in sender && "id" in sender.tab) {
         log["tabId"] = sender.tab.id;
       }
-      userale.log(log);
+      log = filterUrl(log);
+      if(log) {
+        userale.log(log);
+      }
       break;
 
     case MessageTypes.CONFIG_CHANGE:
-      console.log(message);
-      userale.options(message.payload);
-      dispatchTabMessage(message);
+      updateConfig(message.payload);
       break;
 
     default:
diff --git a/src/UserALEWebExtension/options.js 
b/src/UserALEWebExtension/options.js
index b2ecbdb..53b5765 100644
--- a/src/UserALEWebExtension/options.js
+++ b/src/UserALEWebExtension/options.js
@@ -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 });
   });
 }
 
@@ -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);
diff --git a/src/UserALEWebExtension/optionsPage.html 
b/src/UserALEWebExtension/optionsPage.html
index ff92cf0..9a3f363 100644
--- a/src/UserALEWebExtension/optionsPage.html
+++ b/src/UserALEWebExtension/optionsPage.html
@@ -45,6 +45,10 @@
     <input id="version"/>
     <br/>
 
+    <label>URL whitelist regex:</label>
+    <input id="filter"/>
+    <br/>
+
     <div align="right">
     <button type="submit">Save</button>
     </div>

Reply via email to