From 7cbb9c44b9802bc48e66a74acf69bba0a0584c84 Mon Sep 17 00:00:00 2001 From: Liang Ding Date: Mon, 25 Jul 2022 22:51:01 +0800 Subject: [PATCH] =?UTF-8?q?:unlock:=20=E5=8F=91=E8=B5=B7=E4=BA=92=E8=81=94?= =?UTF-8?q?=E7=BD=91=E6=9C=8D=E5=8A=A1=E8=AF=B7=E6=B1=82=E6=97=B6=E7=BB=95?= =?UTF-8?q?=E8=BF=87=E5=AE=89=E5=85=A8=E7=AD=96=E7=95=A5=20Fix=20https://g?= =?UTF-8?q?ithub.com/siyuan-note/siyuan/issues/5516?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- app/electron/main.js | 39 +++++++++++++++++++++++++++++++++------ 1 file changed, 33 insertions(+), 6 deletions(-) diff --git a/app/electron/main.js b/app/electron/main.js index 0fd89b9b2..43c5346ee 100644 --- a/app/electron/main.js +++ b/app/electron/main.js @@ -88,10 +88,8 @@ try { } } catch (e) { console.error(e) - require('electron'). - dialog. - showErrorBox('创建配置目录失败 Failed to create config directory', - '思源需要在用户家目录下创建配置文件夹(~/.config/siyuan),请确保该路径具有写入权限。\n\nSiYuan needs to create a configuration folder (~/.config/siyuan) in the user\'s home directory. Please make sure that the path has write permissions.') + require('electron').dialog.showErrorBox('创建配置目录失败 Failed to create config directory', + '思源需要在用户家目录下创建配置文件夹(~/.config/siyuan),请确保该路径具有写入权限。\n\nSiYuan needs to create a configuration folder (~/.config/siyuan) in the user\'s home directory. Please make sure that the path has write permissions.') app.exit() } @@ -192,8 +190,37 @@ const boot = () => { }) require('@electron/remote/main').enable(mainWindow.webContents) - mainWindow.webContents.userAgent = 'SiYuan/' + appVer + - ' https://b3log.org/siyuan Electron' + mainWindow.webContents.userAgent = 'SiYuan/' + appVer + ' https://b3log.org/siyuan Electron' + + // 发起互联网服务请求时绕过安全策略 https://github.com/siyuan-note/siyuan/issues/5516 + mainWindow.webContents.session.webRequest.onBeforeSendHeaders( + (details, cb) => { + if (-1 < details.url.indexOf('bili')) { + // B 站不移除 Referer https://github.com/siyuan-note/siyuan/issues/94 + cb({requestHeaders: details.requestHeaders}) + return + } + + for (let key in details.requestHeaders) { + if ('referer' === key.toLowerCase()) { + delete details.requestHeaders[key] + } + } + cb({requestHeaders: details.requestHeaders}) + }) + mainWindow.webContents.session.webRequest.onHeadersReceived((details, cb) => { + for (let key in details.responseHeaders) { + if ('x-frame-options' === key.toLowerCase()) { + delete details.responseHeaders[key] + } else if ('content-security-policy' === key.toLowerCase()) { + delete details.responseHeaders[key] + } else if ('access-control-allow-origin' === key.toLowerCase()) { + delete details.responseHeaders[key] + } + } + cb({responseHeaders: details.responseHeaders}) + }) + mainWindow.webContents.on('did-finish-load', () => { if ('win32' === process.platform || 'linux' === process.platform) { siyuanOpenURL = process.argv.find((arg) => arg.startsWith('siyuan://'))