From 8eed3431c8954180abe5ab142c39495f259e89ad Mon Sep 17 00:00:00 2001 From: Tamius Han Date: Sun, 2 Feb 2020 00:57:54 +0100 Subject: [PATCH] Show log results in popup --- src/csui/LoggerUi.vue | 138 +++++++++++++++++++++++++++++++++++----- src/ext/lib/Logger.js | 42 +++++++++--- src/ext/uw.js | 39 +++++++++++- src/res/css/common.scss | 3 + 4 files changed, 196 insertions(+), 26 deletions(-) diff --git a/src/csui/LoggerUi.vue b/src/csui/LoggerUi.vue index dfd0129..906720e 100644 --- a/src/csui/LoggerUi.vue +++ b/src/csui/LoggerUi.vue @@ -1,15 +1,37 @@ diff --git a/src/ext/lib/Logger.js b/src/ext/lib/Logger.js index 2570cdf..3efe468 100644 --- a/src/ext/lib/Logger.js +++ b/src/ext/lib/Logger.js @@ -3,12 +3,14 @@ import { decycle } from 'json-cyclic'; import { sleep } from '../../common/js/utils'; class Logger { - constructor(conf) { + constructor(options) { this.onLogEndCallbacks = []; this.history = []; this.globalHistory = {}; this.isContentScript = false; this.isBackgroundScript = true; + + this.vuexStore = options?.vuexStore; } static saveConfig(conf) { @@ -179,14 +181,15 @@ class Logger { finish() { if (!this.isBackgroundScript) { this.conf.allowLogging = false; - const logJson = JSON.stringify(decycle(this.history)); - this.log('force', 'debugr', 'Calling all log end callbacks. There\'s this many of them:', 1); - for(const f of this.onLogEndCallbacks) { - f(logJson); - } - } else { - this.exportLogToFile(); + // const logJson = JSON.stringify(decycle(this.history)); + // this.log('force', 'debugr', 'Calling all log end callbacks. There\'s this many of them:', 1); + // for(const f of this.onLogEndCallbacks) { + // f(logJson); + // } + // } else { + // this.exportLogToFile(); } + this.saveToVuex(); } parseStack() { @@ -445,6 +448,29 @@ class Logger { } } + saveToVuex() { + console.info('[info] will attempt to save to vuex store.'); + if (!this.conf?.fileOptions?.enabled || this.isBackgroundScript) { + console.info('[info] Logging to file is either disabled or we\'re not on the content script. Not saving.'); + return; + } + + if (!this.vuexStore) { + console.error("[info] No vue store. Log will not be exported."); + return; + } + + console.info('[info] vuex store present. Parsing logs.'); + + const exportObject = { + pageLogs: JSON.stringify(decycle(this.history)), + backgroundLogs: JSON.stringify(decycle(this.globalHistory)), + loggerFileOptions: JSON.stringify(this.conf.fileOptions), + } + + this.vuexStore.dispatch('uw-set-log', JSON.stringify(exportObject)); + } + // export log file — only works on background page async exportLogToFile() { // don't export log if logging to file is not enabled diff --git a/src/ext/uw.js b/src/ext/uw.js index f8f628d..b17a3ae 100644 --- a/src/ext/uw.js +++ b/src/ext/uw.js @@ -7,7 +7,13 @@ import CommsClient from './lib/comms/CommsClient'; import PageInfo from './lib/video-data/PageInfo'; import Logger from './lib/Logger'; +// vue dependency imports import Vue from 'vue'; +import Vuex from 'vuex'; +import VuexWebExtensions from 'vuex-webextensions'; + +global.browser = require('webextension-polyfill'); + import LoggerUi from '../csui/LoggerUi'; if(Debug.debug){ @@ -35,6 +41,7 @@ class UW { this.settings = undefined; this.actionHandler = undefined; this.logger = undefined; + this.store = {}; } async init(){ @@ -77,7 +84,7 @@ class UW { 'handleMouseMove': false } }; - this.logger = new Logger(); + this.logger = new Logger({vuexStore: this.store}); await this.logger.init(loggingOptions); // await this.logger.init(); // not needed if logging options are provided at creation } @@ -141,6 +148,33 @@ class UW { } + initVue() { + Vue.prototype.$browser = global.browser; + Vue.use(Vuex); + this.store = new Vuex.Store({ + plugins: [VuexWebExtensions({ + persistentStates: [ + 'uwLog' + ], + })], + state: { + uwLog: '', + }, + mutations: { + 'uw-set-log'(state, payload) { + console.info('setting state') + state['uwLog'] = payload; + } + }, + actions: { + 'uw-set-log' ({commit}, payload) { + console.info("comitting uw-set-log with payload", payload); + commit('uw-set-log', payload); + } + } + }) + } + createUi() { console.log("CREATING UI"); const random = Math.round(Math.random() * 69420); @@ -152,11 +186,13 @@ class UW { document.body.appendChild(rootDiv); + new Vue({ el: `#${uwid}`, components: { LoggerUi }, + store: this.store, render(h) { return h('logger-ui'); } @@ -165,4 +201,5 @@ class UW { } var main = new UW(); +main.initVue(); main.init(); diff --git a/src/res/css/common.scss b/src/res/css/common.scss index 234b61e..d700e96 100644 --- a/src/res/css/common.scss +++ b/src/res/css/common.scss @@ -386,3 +386,6 @@ small { .ltr { direction: ltr; } +.monospace { + font-family: 'Overpass Mono'; +}