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

2020-09-05 21:03:10 +08:00
 skylee03

如题,我想用 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)
3637 次点击
所在节点    Node.js
1 条回复
skylee03
2020-09-12 22:44:51 +08:00
把调试输出那句注释掉就 work 了,神奇。

这是一个专为移动设备优化的页面(即为了让你能够在 Google 搜索结果里秒开这个页面),如果你希望参与 V2EX 社区的讨论,你可以继续到 V2EX 上打开本讨论主题的完整版本。

https://www.v2ex.com/t/704538

V2EX 是创意工作者们的社区,是一个分享自己正在做的有趣事物、交流想法,可以遇见新朋友甚至新机会的地方。

V2EX is a community of developers, designers and creative people.

© 2021 V2EX