From 8c78715271f4390d20362c11e2c1fea112c8804f Mon Sep 17 00:00:00 2001 From: Tamius Han Date: Tue, 21 Jan 2020 00:41:06 +0100 Subject: [PATCH] additional logger functions --- src/ext/lib/Logger.js | 56 +++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 56 insertions(+) diff --git a/src/ext/lib/Logger.js b/src/ext/lib/Logger.js index db5d7a4..cfbcca0 100644 --- a/src/ext/lib/Logger.js +++ b/src/ext/lib/Logger.js @@ -13,7 +13,9 @@ class Logger { if (!this.conf.fileOptions === undefined) { this.conf.fileOptions = {}; } + this.isBackgroundPage = !!conf.isBackgroundPage; this.history = []; + this.globalHistory = {}; this.startTime = performance.now(); this.temp_disable = false; this.stopTime = conf.timeout ? performance.now() + (conf.timeout * 1000) : undefined; @@ -250,7 +252,61 @@ class Logger { } } + addLogFromPage(host, tabId, frameId, pageHistory) { + if (! this.globalHistory[host]) { + this.globalHistory[host] = {}; + } + if (! this.globalHistory[tabId || 'tab']) { + this.globalHistory[host][tabId || 'tab'] = {}; + } + if (!this.globalHistory[frameId || 'top']) { + this.globalHistory[host][tabId || 'tab'][frameId || 'top'] = pageHistory; + } else { + this.globalHistory[host][tabId || 'tab'][frameId || 'top'].push(...pageHistory); + } + } + // export log file — only works on background page + async exportLogToFile() { + const exportObject = {'pageLogs': JSON.parse(JSON.stringify({...this.globalHistory}))}; + exportObject['logger-settings'] = this.conf.fileOptions; + exportObject['backgroundLog'] = JSON.parse(JSON.stringify(this.history)); + exportObject['popupLog'] = 'NOT IMPLEMENTED'; + + const blob = new Blob([JSON.stringify(exportObject)], {type: 'application/json'}); + const fileUrl = URL.createObjectURL(blob); + + try { + if (BrowserDetect.firefox) { + await browser.permissions.request({permissions: ['downloads']}); + browser.downloads.download({saveAs: true, filename: 'extension-log.json', url: fileUrl}); + } else if (BrowserDetect.chrome) { + const ths = this; + + chrome.permissions.request( + {permissions: ['downloads']}, + (granted) => { + if (granted) { + chrome.downloads.download({saveAs: true, filename: 'extension-log.json', url: fileUrl}); + } else { + ths.downloadPermissionError = true + } + } + ) + } + this.globalHistory = {}; + this.history = []; + } catch (e) { + this.downloadPermissionError = true; + } + } + + // used for instances where logging is limited to a single page and is timed + addLogAndExport(host, pageHistory) { + this.globalHistory = {}; + this.globalHistory[host || 'no-host-provided'] = pageHistory; + this.exportLogToFile(); + } } export default Logger;