EandrewJones commented on code in PR #457: URL: https://github.com/apache/flagon-useralejs/pull/457#discussion_r1617654398
########## src/UserALEWebExtension/background.ts: ########## @@ -0,0 +1,244 @@ +/* + * 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. + */ + +import * as MessageTypes from "@/UserALEWebExtension/messageTypes"; +import * as userale from "@/main"; +import { browser } from "@/UserALEWebExtension/globals"; +import { Extension, Logging } from "@/types"; + +// Initalize userale plugin options +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", + }, +}; + +let urlWhitelist: RegExp; +const tabToHttpSession: { [id: string]: any } = {}; +let browserSessionId: string | null = null; + +/** + * Add log to UserALE buffer for sending to backend + * @param {any} message The message to send + * @return {void} + */ +function addLog(message: any) { + let log = message.payload; + log.browserSessionId = browserSessionId; + // Apply url filter to logs generated outside the background page. + log = filterUrl(log); + if (log) { + userale.log(log); + } +} + +/** + * add tab id to http session id mapping + * @param {any} message The message to send + * @param {browser.runtime.MessageSender} sender The sender of the message + * @return {void} + */ +function updateTabToHttpSessionMapping( + message: any, + sender: browser.runtime.MessageSender, +) { + if (sender.tab?.id) { + tabToHttpSession[sender.tab.id] = message.payload; + } +} +/** + * Apply the extension config to both the background and content instances of userale + * @param {Extension.ConfigPayload} config The extension config to apply + * @return {void} + */ +function updateConfig(config: Extension.ConfigPayload) { + urlWhitelist = new RegExp(config.pluginConfig.urlWhitelist); + userale.options(config.useraleConfig); + // TODO: tabs need a page load to apply this config change. + dispatchTabMessage(config.useraleConfig); +} + +/** + * Send a message to all tabs + * @param {any} message The message to send + * @return {void} + */ +function dispatchTabMessage(message: any) { + // @ts-expect-error Typescript is not aware that firefox's broswer is overloaded + // to support chromium style MV2 callbacks + browser.tabs.query({}, function (tabs: browser.tabs.Tab[]) { + tabs.forEach(function (tab) { + if (!tab.id) return; + browser.tabs.sendMessage(tab.id, message); + }); + }); +} + +/** + * Callback for filtering out logs with urls that do not match the regex defined in extension options. + * @param {Logging.Log} log The candidate log + * @return {Object} The transformed log + */ +function filterUrl(log: Logging.Log) { + if (urlWhitelist.test(log.pageUrl as string)) { + return log; + } + return false; +} + +/** + * Callback for setting the session id's of tab logs to that of the target tab + * @param {Logging.Log} log The candidate log + * @return {Object} The transformed log + */ +function injectSessions(log: Logging.Log) { + const id = (log?.details as Logging.JSONObject).id as string | undefined; + if (id && id in tabToHttpSession) { + log.httpSessionId = tabToHttpSession[id]; + } else { + log.httpSessionId = null; + } + log.browserSessionId = browserSessionId; + return log; +} +// @ts-expect-error Typescript is not aware that firefox's broswer is overloaded +// to support chromium style MV2 callbacks +browser.storage.local.get(defaultConfig, (res) => { + // Apply url filter to logs generated by the background page. + userale.addCallbacks({ filterUrl, injectSessions }); + updateConfig(res); + const userAleHttpSessionId = window.sessionStorage.getItem( + "userAleHttpSessionId", + ); + browserSessionId = userAleHttpSessionId + ? JSON.parse(userAleHttpSessionId) + : null; Review Comment: @Jyyjy The null is likely happening here. -- This is an automated message from the Apache Git Service. To respond to the message, please log on to GitHub and use the URL above to go to the specific comment. To unsubscribe, e-mail: notifications-unsubscr...@flagon.apache.org For queries about this service, please contact Infrastructure at: us...@infra.apache.org --------------------------------------------------------------------- To unsubscribe, e-mail: notifications-unsubscr...@flagon.apache.org For additional commands, e-mail: notifications-h...@flagon.apache.org