V2EX = way to explore
V2EX 是一个关于分享和探索的地方
现在注册
已注册用户请  登录
skylee03
V2EX  ›  Node.js

Electron 中使用 webRequest.onBeforeRequest 重定向请求 URL 失败

  •  
  •   skylee03 · 2020-09-05 21:03:10 +08:00 · 3585 次点击
    这是一个创建于 1300 天前的主题,其中的信息可能已经有所发展或是发生改变。

    如题,我想用 Electron 中 webRequest.onBeforeRequest 重定向某个网站的 swf 资源。之前用 Chrome 插件实现时,用 chrome.webRequest.onBeforeRequest.addListener 成功达到想要的效果,但是用 Electron 实现时,console.log("Test Passed.")这句话可以被正常触发,但是最终加载的资源却仍然是原地址的资源。

    const { app, BrowserWindow } = require('electron')
    const { resolve } = require('path')
    app.commandLine.appendSwitch('ppapi-flash-path', resolve(__dirname, 'pepflashplayer64_32_0_0_314.dll'))
    app.commandLine.appendSwitch('ppapi-flash-version', '32.0.0.314')
    
    function createWindow() {
        win = new BrowserWindow({
    		autoHideMenuBar: true,
    		show: false,
    		fullscreen: false,
    		webPreferences: {plugins: true}
    	})
    	win.maximize()
    	win.webContents.openDevTools()
    	win.webContents.session.webRequest.onBeforeRequest(async ({url}, callback) => {
    		var reg = new RegExp("http://<source-domain>/test~[0-9]*.swf", 'g')
    		if(reg.test(url)) console.log("Test Passed.")
    		callback(reg.test(url) ? {redirectURL: "http://<target-domain>/test.swf"} : {})
    	})
    	win.loadURL('http://<source-domain>')
        win.on('closed', () => {win = null})
    }
    app.on('ready', createWindow)
    
    1 条回复    2020-09-12 22:44:51 +08:00
    skylee03
        1
    skylee03  
    OP
       2020-09-12 22:44:51 +08:00
    把调试输出那句注释掉就 work 了,神奇。
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   我们的愿景   ·   实用小工具   ·   5734 人在线   最高记录 6543   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 34ms · UTC 06:24 · PVG 14:24 · LAX 23:24 · JFK 02:24
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.