-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathplugin-engine.js
More file actions
74 lines (66 loc) · 2.17 KB
/
plugin-engine.js
File metadata and controls
74 lines (66 loc) · 2.17 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
/**
* Fluxon Plugin Engine
* Manages plugin registration and hook execution.
*/
const plugins = [];
/**
* Registers a new plugin.
* @param {Object} plugin - Plugin object with hooks: onIntercept, onPreDownload, onAction.
* Optional metadata fields: description {string}, defaultEnabled {boolean}
*/
function registerPlugin(plugin) {
plugins.push(plugin);
console.log(`Plugin registered: ${plugin.name || 'unnamed'}`);
}
/** Returns metadata for all registered plugins (safe to serialise). */
function getPlugins() {
return plugins.map(p => ({
name: p.name || 'unnamed',
description: p.description || '',
defaultEnabled: p.defaultEnabled !== false,
options: p.options || []
}));
}
/**
* Executes a hook across all enabled registered plugins.
* Plugin enabled state is read from `pluginsConfig` in chrome.storage.local.
* @param {string} hookName - Name of the hook.
* @param {any} arg - Argument passed to the hook.
*/
async function executeHook(hookName, arg) {
// Load per-plugin enabled state
let pluginsConfig = {};
try {
const stored = await new Promise(resolve => chrome.storage.local.get(['pluginsConfig'], resolve));
pluginsConfig = stored.pluginsConfig || {};
} catch (_) {}
let result = {};
for (const plugin of plugins) {
// Skip disabled plugins
const cfg = pluginsConfig[plugin.name];
const isEnabled = cfg ? cfg.enabled !== false : (plugin.defaultEnabled !== false);
if (!isEnabled) continue;
// If a specific plugin is targeted, skip others
if (arg && arg.pluginName && plugin.name !== arg.pluginName) continue;
if (plugin[hookName] && typeof plugin[hookName] === 'function') {
try {
const hookArg = (typeof arg === 'object' && arg !== null) ? { ...arg, pluginConfig: cfg || {} } : arg;
const hookResult = await plugin[hookName](hookArg);
if (hookResult) {
result = { ...result, ...hookResult };
}
} catch (e) {
console.error(`Error in plugin hook ${hookName}:`, e);
}
}
}
return result;
}
// Export for service worker
if (typeof self !== 'undefined') {
self.pluginEngine = {
registerPlugin,
getPlugins,
executeHook
};
}