mirror of
https://github.com/HugoAura/Seewo-HugoAura.git
synced 2026-06-22 16:24:27 +08:00
[Feat] Bump version to 0.1.0-beta && 1st rel
This commit is contained in:
135
src/aura/init/rendererHook/hooksManager.js
Normal file
135
src/aura/init/rendererHook/hooksManager.js
Normal file
@@ -0,0 +1,135 @@
|
||||
const fs = require("fs");
|
||||
const path = require("path");
|
||||
|
||||
class HooksManager {
|
||||
loadHooks() {
|
||||
if (global.__HUGO_AURA__.hooks) {
|
||||
return global.__HUGO_AURA__.hooks;
|
||||
}
|
||||
|
||||
const hooksPath = path.join(__dirname, "../../../aura/ui/hooks");
|
||||
const hooks = new Map();
|
||||
|
||||
try {
|
||||
const files = fs.readdirSync(hooksPath);
|
||||
files.forEach((file) => {
|
||||
if (!file.endsWith(".js")) return;
|
||||
|
||||
try {
|
||||
const hook = require(path.join(hooksPath, file));
|
||||
const targetWindow = hook.windowName || path.basename(file, ".js");
|
||||
hooks.set(targetWindow, hook);
|
||||
console.log(
|
||||
`[HugoAura / Init] Loaded hook for window: ${targetWindow}`
|
||||
);
|
||||
} catch (err) {
|
||||
console.error(
|
||||
`[HugoAura / Init / Error] Failed to load hook ${file}:`,
|
||||
err
|
||||
);
|
||||
}
|
||||
});
|
||||
} catch (err) {
|
||||
console.error(
|
||||
"[HugoAura / Init / Error] Failed to read hooks directory:",
|
||||
err
|
||||
);
|
||||
}
|
||||
|
||||
global.__HUGO_AURA__.hooks = hooks;
|
||||
return hooks;
|
||||
}
|
||||
|
||||
cleanupWindow(windowKey, listeners) {
|
||||
console.log(
|
||||
`[HugoAura / Cleanup / ${windowKey}] Window destroyed, cleaning up...`
|
||||
);
|
||||
|
||||
if (listeners) {
|
||||
const { webContents, domReadyListener, destroyedListener } = listeners;
|
||||
webContents.removeListener("dom-ready", domReadyListener);
|
||||
webContents.removeListener("destroyed", destroyedListener);
|
||||
}
|
||||
|
||||
global.__HUGO_AURA__.hookedWindows.delete(windowKey);
|
||||
}
|
||||
|
||||
handleWindowHook(webContents, hookConfig, windowName) {
|
||||
if (!hookConfig) return;
|
||||
|
||||
const windowKey = `${hookConfig.windowName || windowName}`;
|
||||
if (global.__HUGO_AURA__.hookedWindows.has(windowKey)) {
|
||||
console.log(
|
||||
`[HugoAura / Init] Duplicate hook for ${windowKey}, ignoring...`
|
||||
);
|
||||
return;
|
||||
}
|
||||
|
||||
console.log(`[HugoAura / Init] Hook is initializing for ${windowKey}...`);
|
||||
console.log(
|
||||
`[HugoAura / Init] Hook loaded at: ${new Date().toISOString()}`
|
||||
);
|
||||
|
||||
const domReadyListener = () => {
|
||||
try {
|
||||
console.log(
|
||||
`[HugoAura / UI / Verb / ${windowKey}] Loading injection script...`
|
||||
);
|
||||
|
||||
const injectionScript = fs
|
||||
.readFileSync(path.join(__dirname, "./injection.js"), "utf8")
|
||||
.replace('"__TEMPLATE_TARGETS__"', JSON.stringify(hookConfig.targets))
|
||||
.replace(
|
||||
'"__TEMPLATE_GLOBAL_STYLES__"',
|
||||
JSON.stringify(hookConfig.globalStyles || [])
|
||||
)
|
||||
.replace(
|
||||
'"__TEMPLATE_GLOBAL_JS__"',
|
||||
JSON.stringify(hookConfig.globalJS || [])
|
||||
)
|
||||
.replace("__TEMPLATE_ON_LOADED__", hookConfig.onLoaded || "");
|
||||
|
||||
webContents
|
||||
.executeJavaScript(injectionScript, true)
|
||||
.then(() =>
|
||||
console.log(
|
||||
`[HugoAura / UI / Done / ${windowKey}] Injection script executed`
|
||||
)
|
||||
)
|
||||
.catch((err) =>
|
||||
console.error(
|
||||
`[HugoAura / UI / Error / ${windowKey}] Failed to execute injection script:`,
|
||||
err
|
||||
)
|
||||
);
|
||||
} catch (err) {
|
||||
console.error(
|
||||
`[HugoAura / UI / Error / ${windowKey}] Failed to load UI hook:`,
|
||||
err
|
||||
);
|
||||
}
|
||||
};
|
||||
|
||||
const destroyedListener = () => {
|
||||
this.cleanupWindow(
|
||||
windowKey,
|
||||
global.__HUGO_AURA__.hookedWindows.get(windowKey)
|
||||
);
|
||||
};
|
||||
|
||||
webContents.on("dom-ready", domReadyListener);
|
||||
webContents.on("destroyed", destroyedListener);
|
||||
|
||||
global.__HUGO_AURA__.hookedWindows.set(windowKey, {
|
||||
webContents,
|
||||
domReadyListener,
|
||||
destroyedListener,
|
||||
});
|
||||
|
||||
console.log(
|
||||
`[HugoAura / Init / Success / ${windowKey}] Hook initialized successfully!`
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
module.exports = HooksManager;
|
||||
337
src/aura/init/rendererHook/injection.js
Normal file
337
src/aura/init/rendererHook/injection.js
Normal file
@@ -0,0 +1,337 @@
|
||||
(() => {
|
||||
const waitForElement = (selector, timeout = 5000) => {
|
||||
return new Promise((resolve, reject) => {
|
||||
if (document.querySelector(selector)) {
|
||||
return resolve(document.querySelector(selector));
|
||||
}
|
||||
|
||||
const observer = new MutationObserver((mutations) => {
|
||||
if (document.querySelector(selector)) {
|
||||
observer.disconnect();
|
||||
resolve(document.querySelector(selector));
|
||||
}
|
||||
});
|
||||
|
||||
observer.observe(document.body, {
|
||||
childList: true,
|
||||
subtree: true,
|
||||
});
|
||||
|
||||
setTimeout(() => {
|
||||
observer.disconnect();
|
||||
reject("[HugoAura / UI / Injection] Timeout waiting for element");
|
||||
}, timeout);
|
||||
});
|
||||
};
|
||||
|
||||
const createStore = () => {
|
||||
const store = {
|
||||
router: "/",
|
||||
};
|
||||
const internal = {
|
||||
get: (key) => store[key],
|
||||
set: (key, value) => {
|
||||
store[key] = value;
|
||||
return true;
|
||||
},
|
||||
};
|
||||
|
||||
window.$store = {
|
||||
get: (key) => {
|
||||
const stack = new Error().stack;
|
||||
if (stack.includes("aura/ui/pages/")) {
|
||||
return internal.get(key);
|
||||
}
|
||||
return undefined;
|
||||
},
|
||||
set: (key, value) => {
|
||||
const stack = new Error().stack;
|
||||
if (stack.includes("aura/ui/pages/")) {
|
||||
return internal.set(key, value);
|
||||
}
|
||||
return false;
|
||||
},
|
||||
};
|
||||
|
||||
return internal;
|
||||
};
|
||||
|
||||
const store = createStore();
|
||||
|
||||
const createUILoader = () => {
|
||||
const modules = "__TEMPLATE_TARGETS__";
|
||||
const containers = new Map();
|
||||
const observers = new Map();
|
||||
const moduleResources = new Map();
|
||||
const globalScripts = new Set();
|
||||
|
||||
const insertElement = (target, element, mode = "appendChild") => {
|
||||
const elementId = element.id;
|
||||
if (document.getElementById(elementId)) {
|
||||
console.log(
|
||||
`[HugoAura / UI / Warning] Element ${elementId} already exists, skipping insertion`
|
||||
);
|
||||
return;
|
||||
}
|
||||
|
||||
switch (mode) {
|
||||
case "insertBefore":
|
||||
target.parentNode.insertBefore(element, target);
|
||||
break;
|
||||
case "insertAfter":
|
||||
target.parentNode.insertBefore(element, target.nextSibling);
|
||||
break;
|
||||
case "appendChild":
|
||||
default:
|
||||
target.appendChild(element);
|
||||
}
|
||||
};
|
||||
|
||||
const loadGlobalJS = async () => {
|
||||
const scripts = "__TEMPLATE_GLOBAL_JS__";
|
||||
for (const scriptPath of scripts) {
|
||||
try {
|
||||
const script = document.createElement("script");
|
||||
script.src = `../../aura/${scriptPath}`;
|
||||
document.body.appendChild(script);
|
||||
globalScripts.add(script);
|
||||
await new Promise((resolve, reject) => {
|
||||
script.onload = resolve;
|
||||
script.onerror = reject;
|
||||
});
|
||||
console.log(
|
||||
`[HugoAura / UI / Global] Loaded global script: ${scriptPath}`
|
||||
);
|
||||
} catch (err) {
|
||||
console.error(
|
||||
`[HugoAura / UI / Error] Failed to load global script ${scriptPath}:`,
|
||||
err
|
||||
);
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
const monitorParent = (moduleKey, target, _mode) => {
|
||||
if (observers.has(moduleKey)) {
|
||||
observers.get(moduleKey).disconnect();
|
||||
}
|
||||
|
||||
const elementId = `aura-container-${moduleKey.replace(/\./g, "-")}`;
|
||||
const observer = new MutationObserver((_mutations) => {
|
||||
if (!document.getElementById(elementId)) {
|
||||
let targetElement = document.querySelector(
|
||||
modules[moduleKey].pageSelector
|
||||
);
|
||||
if (
|
||||
targetElement &&
|
||||
modules[moduleKey].active &&
|
||||
modules[moduleKey].revive
|
||||
) {
|
||||
if (!document.getElementById(elementId)) {
|
||||
console.log(
|
||||
`[HugoAura / UI / Revival] Reviving module ${moduleKey}`
|
||||
);
|
||||
this.loadModule(moduleKey, true);
|
||||
}
|
||||
}
|
||||
}
|
||||
});
|
||||
|
||||
observer.observe(target.parentNode, {
|
||||
childList: true,
|
||||
subtree: false,
|
||||
});
|
||||
|
||||
observers.set(moduleKey, observer);
|
||||
};
|
||||
|
||||
const loader = {
|
||||
async loadModule(moduleKey, isRevive = false) {
|
||||
if (!modules[moduleKey]?.active) return;
|
||||
|
||||
try {
|
||||
const config = modules[moduleKey];
|
||||
const target = await waitForElement(config.pageSelector);
|
||||
const elementId = `aura-container-${moduleKey.replace(/\./g, "-")}`;
|
||||
|
||||
if (document.getElementById(elementId)) {
|
||||
console.log(
|
||||
`[HugoAura / UI / Warning] Module ${moduleKey} already loaded, skipping`
|
||||
);
|
||||
return;
|
||||
}
|
||||
|
||||
const container = document.createElement("div");
|
||||
container.id = elementId;
|
||||
containers.set(moduleKey, container);
|
||||
|
||||
const resources = new Set();
|
||||
moduleResources.set(moduleKey, resources);
|
||||
|
||||
if (config.pageCSS && !isRevive) {
|
||||
const link = document.createElement("link");
|
||||
link.rel = "stylesheet";
|
||||
link.href = `../../aura/${config.pageCSS}`;
|
||||
document.head.appendChild(link);
|
||||
resources.add(link);
|
||||
}
|
||||
|
||||
const html = await fetch(`../../aura/${config.pageURI}`).then((r) =>
|
||||
r.text()
|
||||
);
|
||||
container.innerHTML = html;
|
||||
|
||||
insertElement(target, container, config.selectorMode);
|
||||
monitorParent(moduleKey, target, container, config.selectorMode);
|
||||
|
||||
if (config.pageScript && !isRevive) {
|
||||
const script = document.createElement("script");
|
||||
script.src = `../../aura/${config.pageScript}`;
|
||||
document.body.appendChild(script);
|
||||
resources.add(script);
|
||||
}
|
||||
|
||||
const observer = new MutationObserver(() => {
|
||||
if (
|
||||
!document.contains(container) &&
|
||||
modules[moduleKey].active &&
|
||||
modules[moduleKey].revive
|
||||
) {
|
||||
this.loadModule(moduleKey, true);
|
||||
}
|
||||
});
|
||||
|
||||
observer.observe(document.body, {
|
||||
childList: true,
|
||||
subtree: true,
|
||||
});
|
||||
|
||||
observers.set(moduleKey, observer);
|
||||
} catch (err) {
|
||||
console.error(
|
||||
`[HugoAura / UI / Error] Failed to load module ${moduleKey}:`,
|
||||
err
|
||||
);
|
||||
}
|
||||
},
|
||||
|
||||
unloadModule(moduleKey) {
|
||||
const container = containers.get(moduleKey);
|
||||
const observer = observers.get(moduleKey);
|
||||
const resources = moduleResources.get(moduleKey);
|
||||
|
||||
if (observer) {
|
||||
observer.disconnect();
|
||||
observers.delete(moduleKey);
|
||||
}
|
||||
|
||||
if (container) {
|
||||
container.remove();
|
||||
containers.delete(moduleKey);
|
||||
}
|
||||
|
||||
if (resources) {
|
||||
resources.forEach((element) => element.remove());
|
||||
moduleResources.delete(moduleKey);
|
||||
}
|
||||
},
|
||||
|
||||
handleModuleChange(moduleKey, path = []) {
|
||||
const fullPath = [...path, moduleKey].join(".");
|
||||
if (path.length === 0 && modules[moduleKey].active) {
|
||||
this.loadModule(moduleKey);
|
||||
} else if (path.length === 0) {
|
||||
this.unloadModule(moduleKey);
|
||||
} else {
|
||||
if (moduleKey === "active") {
|
||||
if (modules[path[0]].active) {
|
||||
this.loadModule(path[0]);
|
||||
} else {
|
||||
this.unloadModule(path[0]);
|
||||
}
|
||||
} else {
|
||||
this.unloadModule(path[0]);
|
||||
this.loadModule(path[0]);
|
||||
}
|
||||
}
|
||||
},
|
||||
};
|
||||
|
||||
const createDeepProxy = (target, handler, path = []) => {
|
||||
return new Proxy(target, {
|
||||
get(target, prop) {
|
||||
const value = Reflect.get(target, prop);
|
||||
if (typeof value === "object" && value !== null) {
|
||||
return createDeepProxy(value, handler, [...path, prop]);
|
||||
}
|
||||
return value;
|
||||
},
|
||||
set(target, prop, value) {
|
||||
console.debug(
|
||||
`[HugoAura / UI / Debug] Setting property: ${[...path, prop].join(
|
||||
"."
|
||||
)}`
|
||||
);
|
||||
const result = Reflect.set(target, prop, value);
|
||||
if (result) {
|
||||
handler([...path], prop, value);
|
||||
}
|
||||
return result;
|
||||
},
|
||||
deleteProperty(target, prop) {
|
||||
console.debug(
|
||||
`[HugoAura / UI / Debug] Deleting property: ${[...path, prop].join(
|
||||
"."
|
||||
)}`
|
||||
);
|
||||
const result = Reflect.deleteProperty(target, prop);
|
||||
if (result) {
|
||||
handler([...path], prop, undefined);
|
||||
}
|
||||
return result;
|
||||
},
|
||||
});
|
||||
};
|
||||
|
||||
const initialLoad = async () => {
|
||||
try {
|
||||
await loadGlobalJS();
|
||||
|
||||
for (const [key, config] of Object.entries(modules)) {
|
||||
if (config.active) {
|
||||
await loader.loadModule(key);
|
||||
}
|
||||
}
|
||||
} catch (err) {
|
||||
console.error("[HugoAura / UI / Error] Initial load failed:", err);
|
||||
}
|
||||
};
|
||||
|
||||
initialLoad();
|
||||
|
||||
return createDeepProxy(modules, (path, prop, value) => {
|
||||
loader.handleModuleChange(prop, path);
|
||||
});
|
||||
};
|
||||
|
||||
window.__HUGO_AURA_LOADER__ = createUILoader();
|
||||
|
||||
const loadGlobalStyles = async () => {
|
||||
const styles = "__TEMPLATE_GLOBAL_STYLES__";
|
||||
styles.forEach((style) => {
|
||||
const link = document.createElement("link");
|
||||
link.rel = "stylesheet";
|
||||
link.href = `../../aura/${style}`;
|
||||
document.head.appendChild(link);
|
||||
});
|
||||
};
|
||||
|
||||
const init = async () => {
|
||||
await loadGlobalStyles();
|
||||
__TEMPLATE_ON_LOADED__;
|
||||
};
|
||||
|
||||
init().catch((err) =>
|
||||
console.error("[HugoAura / UI / Error] Initialization failed:", err)
|
||||
);
|
||||
})();
|
||||
300
src/aura/init/rendererHook/networkHook.js
Normal file
300
src/aura/init/rendererHook/networkHook.js
Normal file
@@ -0,0 +1,300 @@
|
||||
const fs = require("fs");
|
||||
const path = require("path");
|
||||
const os = require("os");
|
||||
|
||||
class NetworkHook {
|
||||
ruleCache = new Map();
|
||||
|
||||
loadRewriteRules(config) {
|
||||
const networkConfig = config.networkRewrite || {};
|
||||
const rules = [];
|
||||
|
||||
Object.entries(networkConfig).forEach(([rulePath, ruleConfig]) => {
|
||||
if (this.ruleCache.has(rulePath) && !ruleConfig.enabled) {
|
||||
console.log(
|
||||
`[HugoAura / NetworkHook] Skipping disabled rule: ${rulePath}`
|
||||
);
|
||||
return;
|
||||
}
|
||||
|
||||
try {
|
||||
let rule = this.ruleCache.get(rulePath);
|
||||
|
||||
if (!rule) {
|
||||
rule = require(path.join(
|
||||
__dirname,
|
||||
"../../../aura/jsRewrite/network",
|
||||
rulePath
|
||||
));
|
||||
this.ruleCache.set(rulePath, rule);
|
||||
}
|
||||
|
||||
if (ruleConfig.enabled) {
|
||||
rules.push({
|
||||
id: rulePath,
|
||||
type: rule.type, // 'localResource' or 'networkRequest'
|
||||
urlPattern: rule.urlPattern,
|
||||
requestHook: rule.requestHook || null,
|
||||
responseHook: rule.responseHook || null,
|
||||
beginOfHook: rule.beginOfHook || null,
|
||||
endOfHook: rule.endOfHook || null,
|
||||
hookedContent: rule.hookedContent || null,
|
||||
hookedContentFunc: rule.hookedContentFunc || null,
|
||||
});
|
||||
console.log(`[HugoAura / NetworkHook] Loaded rule: ${rulePath}`);
|
||||
}
|
||||
} catch (err) {
|
||||
console.error(
|
||||
`[HugoAura / NetworkHook] Failed to load rule ${rulePath}:`,
|
||||
err
|
||||
);
|
||||
}
|
||||
});
|
||||
|
||||
return rules;
|
||||
}
|
||||
|
||||
installHook(session, config) {
|
||||
const rules = this.loadRewriteRules(config);
|
||||
if (rules.length === 0) return;
|
||||
|
||||
session.webRequest.onBeforeRequest(
|
||||
{ urls: ["*://*/*", "file://*"] },
|
||||
(details, callback) => {
|
||||
let modified = false;
|
||||
let redirectURL = null;
|
||||
let newBody = null;
|
||||
|
||||
if (details.url.includes("hugo-aura-temp")) {
|
||||
callback({});
|
||||
return;
|
||||
}
|
||||
|
||||
for (const rule of rules) {
|
||||
if (this.matchUrl(rule.urlPattern, details.url)) {
|
||||
console.log(
|
||||
`[HugoAura / NetworkHook] Rule ${rule.id} matched URL: ${details.url}`
|
||||
);
|
||||
|
||||
if (
|
||||
rule.type === "localResource" &&
|
||||
details.url.startsWith("file://")
|
||||
) {
|
||||
console.log(
|
||||
`[HugoAura / NetworkHook] Processing rule ${rule.id}, mode: localResource`
|
||||
);
|
||||
modified = true;
|
||||
redirectURL = this.processLocalResource(
|
||||
rule,
|
||||
details
|
||||
).redirectURL;
|
||||
} else if (rule.type === "networkRequest" && rule.requestHook) {
|
||||
console.log(
|
||||
`[HugoAura / NetworkHook] Processing rule ${rule.id}, mode: networkRequest`
|
||||
);
|
||||
try {
|
||||
const originalRequest = JSON.parse(JSON.stringify(details));
|
||||
const result = rule.requestHook(originalRequest);
|
||||
|
||||
if (result) {
|
||||
if (result.redirectURL) {
|
||||
redirectURL = result.redirectURL;
|
||||
modified = true;
|
||||
}
|
||||
|
||||
if (result.requestBody) {
|
||||
newBody = result.requestBody;
|
||||
modified = true;
|
||||
}
|
||||
}
|
||||
} catch (err) {
|
||||
console.error(
|
||||
`[HugoAura / NetworkHook] Error in request hook:`,
|
||||
err
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
if (modified) break;
|
||||
}
|
||||
}
|
||||
|
||||
if (modified && redirectURL) {
|
||||
callback({ redirectURL });
|
||||
} else if (modified && newBody) {
|
||||
callback({ requestBody: newBody });
|
||||
} else {
|
||||
callback({});
|
||||
}
|
||||
}
|
||||
);
|
||||
|
||||
session.webRequest.onHeadersReceived(
|
||||
{ urls: ["*://*/*", "file://*"] },
|
||||
(details, callback) => {
|
||||
for (const rule of rules) {
|
||||
if (
|
||||
rule.type === "networkRequest" &&
|
||||
rule.responseHook &&
|
||||
this.matchUrl(rule.urlPattern, details.url)
|
||||
) {
|
||||
try {
|
||||
const originalResponse = JSON.parse(JSON.stringify(details));
|
||||
const result = rule.responseHook(originalResponse);
|
||||
|
||||
if (result && result.responseHeaders) {
|
||||
callback({ responseHeaders: result.responseHeaders });
|
||||
return;
|
||||
}
|
||||
} catch (err) {
|
||||
console.error(
|
||||
`[HugoAura / NetworkHook] Error in response hook:`,
|
||||
err
|
||||
);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
callback({});
|
||||
}
|
||||
);
|
||||
}
|
||||
|
||||
processLocalResource(rule, details) {
|
||||
try {
|
||||
const filePath = new URL(details.url).pathname;
|
||||
const normalizedPath = decodeURIComponent(
|
||||
process.platform === "win32" ? filePath.substring(1) : filePath
|
||||
);
|
||||
|
||||
if (fs.existsSync(normalizedPath)) {
|
||||
let content = fs.readFileSync(normalizedPath, "utf8");
|
||||
|
||||
if (
|
||||
(rule.beginOfHook || rule.beginOfHookRegex) &&
|
||||
(rule.endOfHook || rule.endOfHookRegex) &&
|
||||
(rule.hookedContent || rule.hookedContentFunc)
|
||||
) {
|
||||
let startIdx = -1,
|
||||
endIdx = -1;
|
||||
|
||||
if (rule.beginOfHookRegex) {
|
||||
const beginRegex = new RegExp(rule.beginOfHookRegex);
|
||||
const beginMatch = content.match(beginRegex);
|
||||
if (beginMatch) {
|
||||
startIdx = beginMatch.index;
|
||||
}
|
||||
} else if (rule.beginOfHook) {
|
||||
startIdx = content.indexOf(rule.beginOfHook);
|
||||
}
|
||||
|
||||
if (rule.endOfHookRegex) {
|
||||
const endRegex = new RegExp(rule.endOfHookRegex);
|
||||
const endContent = content.substring(startIdx);
|
||||
const endMatch = endContent.match(endRegex);
|
||||
if (endMatch) {
|
||||
endIdx = startIdx + endMatch.index + endMatch[0].length;
|
||||
}
|
||||
} else if (rule.endOfHook) {
|
||||
const endContent = content.substring(startIdx);
|
||||
const relativeEndIdx = endContent.indexOf(rule.endOfHook);
|
||||
if (relativeEndIdx !== -1) {
|
||||
endIdx = startIdx + relativeEndIdx + rule.endOfHook.length;
|
||||
}
|
||||
}
|
||||
|
||||
if (startIdx !== -1 && endIdx !== -1) {
|
||||
let beginHook =
|
||||
rule.beginOfHook ||
|
||||
(rule.beginOfHookRegex
|
||||
? content.substring(
|
||||
startIdx,
|
||||
startIdx + content.substring(startIdx).search(/[;\s{]/)
|
||||
)
|
||||
: "");
|
||||
|
||||
let endHook =
|
||||
rule.endOfHook ||
|
||||
(rule.endOfHookRegex
|
||||
? content.substring(
|
||||
endIdx,
|
||||
endIdx + content.substring(endIdx).search(/[;\s{]/)
|
||||
)
|
||||
: "");
|
||||
|
||||
let hookContent;
|
||||
if (rule.hookedContentFunc) {
|
||||
hookContent = rule.hookedContentFunc
|
||||
.toString()
|
||||
.match(/{([\s\S]*)}/)[1]
|
||||
.trim();
|
||||
} else {
|
||||
hookContent = rule.hookedContent;
|
||||
}
|
||||
|
||||
hookContent = this.minifyCode(hookContent);
|
||||
|
||||
content =
|
||||
content.substring(0, startIdx) +
|
||||
beginHook +
|
||||
hookContent +
|
||||
endHook +
|
||||
content.substring(endIdx);
|
||||
|
||||
const tempDir = path.join(os.tmpdir(), "hugo-aura-temp");
|
||||
if (!fs.existsSync(tempDir)) {
|
||||
fs.mkdirSync(tempDir, { recursive: true });
|
||||
}
|
||||
|
||||
const tempFile = path.join(tempDir, path.basename(normalizedPath));
|
||||
fs.writeFileSync(tempFile, content, "utf8");
|
||||
|
||||
return {
|
||||
redirectURL: `file://${
|
||||
process.platform === "win32" ? "/" : ""
|
||||
}${encodeURI(tempFile.replace(/\\/g, "/"))}`, // Seewo Hugo is still on Node 12 / Electron 8 TwT
|
||||
};
|
||||
} else {
|
||||
console.warn(
|
||||
`[HugoAura / NetworkHook] Could not find match points in file: ${normalizedPath}`
|
||||
);
|
||||
return undefined;
|
||||
}
|
||||
} else {
|
||||
console.error(
|
||||
`[HugoAura / NetworkHook] Error processing rule:`,
|
||||
rule
|
||||
);
|
||||
}
|
||||
} else {
|
||||
console.error(
|
||||
`[HugoAura / NetworkHook] Error processing local file: normalizedPath not exists`,
|
||||
normalizedPath
|
||||
);
|
||||
}
|
||||
} catch (err) {
|
||||
console.error(
|
||||
`[HugoAura / NetworkHook] Error processing local file:`,
|
||||
err
|
||||
);
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
minifyCode(code) {
|
||||
return code.replace(/\s+/g, " ").trim();
|
||||
}
|
||||
|
||||
matchUrl(pattern, url) {
|
||||
if (typeof pattern === "string") {
|
||||
return url.includes(pattern);
|
||||
} else if (pattern instanceof RegExp) {
|
||||
return pattern.test(url);
|
||||
} else if (typeof pattern === "function") {
|
||||
return pattern(url);
|
||||
}
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
module.exports = NetworkHook;
|
||||
Reference in New Issue
Block a user