electron 获取本地 javascript 文件的 URL 和 return

electron get URL of local javascript file and return that

我正在尝试模仿一些在 chrome 扩展中很容易实现的行为,现在我的应用程序越来越大,我正在尝试将其作为独立应用程序重建到 electron 中。

遗憾的是我 运行 遇到了一个问题,我尝试为某个网页提供我自己的 js。在 chrome 扩展中,我将通过以下方式实现:

chrome.webRequest.onBeforeRequest.addListener(function (details) {
        console.log("Patched init.js");
        return {redirectUrl: chrome.extension.getURL("init.js")};
    }, {urls: ["https://example.com/js/init.js"]}, ["blocking"]);

我只是找不到在电子中这样做的正确方法。确实需要return本地修改的js文件

我可以在 onBeforeRequest 中使用什么,但遗憾的是我仍然没有像 chrome 扩展(getUrl 函数)那样的本地文件。

session.defaultSession.webRequest.onBeforeRequest(function (details) {
        console.log("Patched jQuery");
        return {redirectUrl: getUrlFunctionNeeded("init")};
    }, {urls: ["https://example.com/js/init.js"]}, ["blocking"]);

Best regards

如果有人对该解决方案感兴趣,我认为在电子应用程序中创建一个带有节点静态的小型 Web 服务器就足够了。然后我使用 localhost:port/staticmap/my.js 作为 URL,而不是 extention.getURL().