V2EX = way to explore
V2EX 是一个关于分享和探索的地方
Sign Up Now
For Existing Member  Sign In
skylee03
V2EX  ›  Node.js

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

  •  
  •   skylee03 · Sep 5, 2020 · 4541 views
    This topic created in 2060 days ago, the information mentioned may be changed or developed.

    如题,我想用 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 replies    2020-09-12 22:44:51 +08:00
    skylee03
        1
    skylee03  
    OP
       Sep 12, 2020
    把调试输出那句注释掉就 work 了,神奇。
    About   ·   Help   ·   Advertise   ·   Blog   ·   API   ·   FAQ   ·   Solana   ·   4093 Online   Highest 6679   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 24ms · UTC 00:52 · PVG 08:52 · LAX 17:52 · JFK 20:52
    ♥ Do have faith in what you're doing.