forked from mbnuqw/sidebery
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
22 changed files
with
224 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,67 @@ | ||
/** | ||
* Log info | ||
*/ | ||
function infoLog(msg) { | ||
if (!this._logs) this._logs = [] | ||
this._logs.push(`${Date.now()} [INFO::background] ${msg}`) | ||
} | ||
|
||
/** | ||
* Log warn | ||
*/ | ||
function warnLog(msg) { | ||
if (!this._logs) this._logs = [] | ||
this._logs.push(`${Date.now()} [WARN::background] ${msg}`) | ||
} | ||
|
||
/** | ||
* Log err | ||
*/ | ||
function errLog(msg) { | ||
if (!this._logs) this._logs = [] | ||
this._logs.push(`${Date.now()} [ERR::background] ${msg}`) | ||
} | ||
|
||
/** | ||
* Writes logs | ||
*/ | ||
function log(logs) { | ||
if (!this._logs) this._logs = [] | ||
this._logs.push(...logs) | ||
|
||
if (this._logs.length > 123) { | ||
browser.storage.local.set({ logs: this._logs }) | ||
this._logs = [] | ||
} | ||
} | ||
|
||
/** | ||
* Returns all logs | ||
*/ | ||
async function getLogs() { | ||
let storage = { logs: [] } | ||
try { | ||
storage = await browser.storage.local.get(storage) | ||
} catch (err) { | ||
// ... | ||
} | ||
|
||
let logs = storage.logs | ||
|
||
if (this._logs) logs = logs.concat(this._logs) | ||
|
||
return logs | ||
} | ||
|
||
function initLogs() { | ||
window.getLogs = this.actions.getLogs | ||
} | ||
|
||
export default { | ||
infoLog, | ||
warnLog, | ||
errLog, | ||
log, | ||
getLogs, | ||
initLogs, | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,65 @@ | ||
interface ActionContextTEMP { | ||
actions: { [key: string]: (...args: any[]) => any } | ||
state: { | ||
instanceType: string | ||
bg: WEPort | ||
windowId: string | ||
} | ||
|
||
_logs: string[] | undefined | ||
_logTimeout: number | undefined | ||
} | ||
|
||
enum LogLvl { | ||
Info = 'INFO', | ||
Warn = 'WARN', | ||
Err = 'ERR', | ||
} | ||
|
||
/** | ||
* Log info | ||
*/ | ||
function infoLog(this: ActionContextTEMP, msg: string): void { | ||
this.actions.log(LogLvl.Info, msg) | ||
} | ||
|
||
/** | ||
* Log warn | ||
*/ | ||
function warnLog(this: ActionContextTEMP, msg: string): void { | ||
this.actions.log(LogLvl.Warn, msg) | ||
} | ||
|
||
/** | ||
* Log err | ||
*/ | ||
function errLog(this: ActionContextTEMP, msg: string): void { | ||
this.actions.log(LogLvl.Err, msg) | ||
} | ||
|
||
/** | ||
* Log msg at given level in mem and send | ||
*/ | ||
const LOG_DEBOUNCE = 1000 | ||
function log(this: ActionContextTEMP, lvl: LogLvl, msg: string): void { | ||
if (!this._logs) this._logs = [] | ||
const log = `${Date.now()} [${lvl}::${this.state.instanceType}-${this.state.windowId}] ${msg}` | ||
this._logs.push(log) | ||
|
||
if (this._logTimeout) window.clearTimeout(this._logTimeout) | ||
this._logTimeout = setTimeout(() => { | ||
if (this.state.bg && !this.state.bg.error) { | ||
this.state.bg.postMessage({ action: 'log', args: [this._logs] }) | ||
} else { | ||
browser.runtime.sendMessage({ instanceType: 'bg', action: 'log', args: [this._logs] }) | ||
} | ||
this._logs = [] | ||
}, LOG_DEBOUNCE) | ||
} | ||
|
||
export default { | ||
infoLog, | ||
warnLog, | ||
errLog, | ||
log, | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.