-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathlog.js
More file actions
128 lines (116 loc) · 3.99 KB
/
log.js
File metadata and controls
128 lines (116 loc) · 3.99 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
// log.js
// Version: 1.0
// Instructions for AI: do not remove comments! MUST NOT REMOVE COMMENTS.
/**
* Logging utility for the OneClickPrompts Chrome Extension.
*
* All logging should now use the logConCgp function defined in this file.
* This ensures consistent logging format and centralized control over logging behavior.
*
* ## How to Use:
*
* To log messages, simply call the logConCgp function with your message and any additional data.
*
* ```javascript
* // Basic log message
* logConCgp('This is a log message.');
*
* // Log message with additional data
* logConCgp('User data:', userData);
* ```
*/
/**
* Logs messages to the console with a consistent prefix.
*
* @param {string} message - The message to log.
* @param {...any} optionalParams - Additional parameters to log.
*/
(function installBrowserConsoleLogGate() {
const root = globalThis;
if (!root || root.__OCPBrowserConsoleLogGateInstalled || !root.console) {
return;
}
const gatedMethods = ['log', 'info', 'debug', 'warn', 'error'];
const originalConsole = {};
let storagePreferenceReady = false;
let storagePreferenceDisablesLogs = false;
function getConfigPreference() {
const config = root.globalMaxExtensionConfig;
if (config && typeof config.disableBrowserConsoleLogs === 'boolean') {
return config.disableBrowserConsoleLogs;
}
return null;
}
function browserConsoleLogsAreDisabled() {
const configPreference = getConfigPreference();
if (configPreference !== null) {
return configPreference;
}
if (!storagePreferenceReady) {
return true;
}
return storagePreferenceDisablesLogs;
}
function storageGet(keys) {
return new Promise(resolve => {
try {
if (!root.chrome?.storage?.local) {
resolve({});
return;
}
root.chrome.storage.local.get(keys, result => {
if (root.chrome.runtime?.lastError) {
resolve({});
return;
}
resolve(result || {});
});
} catch (_) {
resolve({});
}
});
}
async function refreshStoragePreference() {
const current = await storageGet(['currentProfile']);
const profileName = current.currentProfile || 'Default';
const profileKey = `profiles.${profileName}`;
const profileResult = await storageGet([profileKey]);
storagePreferenceDisablesLogs = !!profileResult[profileKey]?.disableBrowserConsoleLogs;
storagePreferenceReady = true;
}
gatedMethods.forEach((methodName) => {
const originalMethod = root.console[methodName];
if (typeof originalMethod !== 'function') {
return;
}
originalConsole[methodName] = originalMethod.bind(root.console);
root.console[methodName] = (...args) => {
if (!storagePreferenceReady && getConfigPreference() === null) {
return;
}
if (browserConsoleLogsAreDisabled()) {
return;
}
originalConsole[methodName](...args);
};
});
refreshStoragePreference();
root.chrome?.storage?.onChanged?.addListener((changes, namespace) => {
if (namespace !== 'local') {
return;
}
if (changes.currentProfile || Object.keys(changes).some(key => key.startsWith('profiles.'))) {
storagePreferenceReady = false;
refreshStoragePreference();
}
});
root.__OCPBrowserConsoleLogGateInstalled = true;
root.OCPBrowserConsoleLogGate = {
isDisabled: browserConsoleLogsAreDisabled,
refresh: refreshStoragePreference,
originalConsole
};
})();
function logConCgp(message, ...optionalParams) {
console.log(`[OneClickPrompts] ${message}`, ...optionalParams);
}