▒ 目录 ▒
🛫 导读
需求
开发环境
| 版本号 | 描述 |
|---|
| 文章日期 | 2023-11-07 | |
| 操作系统 | Win10 - 22H2 | 19045.3570 |
| 示例工作目录 | J:\_ALL\JOB\sw\_nginx\map-ys | 执行json-server服务器 |
| | |
1️⃣ Adblock等插件拦截
2️⃣ 【失败】Content-Security-Policy
启动服务器json-server
html中的meta字段
3️⃣ 【失败】https vs http
webPreferences & allowRunningInsecureContent
new BrowserWindow({
webPreferences:{
webSecurity: false,
allowRunningInsecureContent: true,
}
}
disable-features
app.commandLine.appendSwitch('disable-features', 'OutOfBlinkCors');
4️⃣ 【失败】检测fetch
fetch被魔改了
5️⃣ 【失败】使用axios
插入axios库
6️⃣ 【成功】require(‘http’)
var http = require('http');
function post(action,send,callback){
var options = {
hostname: '127.0.0.1',
port: 16010,
path: action,
method: 'POST',
headers: {
'Content-Type': 'application/x-www-form-urlencoded; charset=UTF-8'
}
};
var req = http.request(options, function (res) {
var body="";
res.setEncoding('utf8');
res.on('data', function (chunk) {
body += chunk;
});
res.on('end', function(){
var json = JSON.parse(body);
callback(json);
});
});
req.on('error', function (e) {
console.log('problem with request: ' + e.message);
});
req.write(send);
req.end();
}
post('/posts', '{"id":3}', ()=>{})
7️⃣ 【完美解决】取消webRequest.onBeforeRequest
function blockHttpRequests() {
return;
var e;
i()
.session.fromPartition(
(e = g.windowOptions.webPreferences.partition) !== null &&
e !== void 0
? e
: ""
)
.webRequest.onBeforeRequest({ urls: ["http://*/*"] }, (e, t) => {
t({
cancel:
e.webContents && e.webContents.getURL().startsWith("file://"),
});
});
}
🛬 文章小结
📖 参考资料