gpt4 book ai didi

iframe - 在Electron中禁用同源策略(需要与iframe一起使用)

转载 作者:行者123 更新时间:2023-12-03 12:28:19 28 4
gpt4 key购买 nike

由于不需要代码,我会尽量缩短-我在Electron应用程序中放置了一个iframe标签,并且我希望JavaScript从中检索一些信息,但是由于同源政策,我无法做到这一点。我可以禁用它吗?注意:我是iframe中页面的所有者。

var validate = document.getElementById('validate');
validate.onclick = function() {
var input = document.getElementById('serial').value;
var validator = document.createElement('iframe');
validator.src = "http://**********.com/?wvkey=" + input;
body.appendChild(validator);
var status = validator.contentWindow.document.getElementById('status').innerHTML;
if (status === "Key Status: Active") {
window.location.assign("../dashboard/index.html");
}
else {
window.location.assign("../support/keys/invalid/index.html");
}
}

单击页面上的按钮时,该代码应检查iframe中值为“关键状态:有效”的文本。如果条件为真,则应将用户重定向到另一个页面。如果为假,则应将他重定向到错误页面。但是,在我的应用中,iframe只会显示在后台,并且不会执行任何操作-不会重定向。

main.js(部分):
win = new BrowserWindow({
height: 700,
resizable: false,
show: false,
webPreferences: {
nodeIntegration: true,
webSecurity: false
},
width: 790

})

编辑(更新)
var validate = document.getElementById('validate');
validate.onclick = function() {
var input = document.getElementById('serial').value;
var validator = document.createElement('iframe');
validator.onload = function() {
var status = validator.contentWindow.document.getElementById('status').innerHTML;
if (status === "Key Status: Active") {
window.location.assign("../dashboard/index.html");
}
else {
window.location.assign("../support/keys/invalid/index.html");
}
};
validator.src = "http://awebsite.com/?wvkey=" + input;
body.appendChild(validator);
}

最佳答案

由于您的问题是如何在 Electron 窗口中禁用安全功能(如CORS),因此可以使用以下设置:

win = new BrowserWindow({
webPreferences: {
webSecurity: false
}
});

但是,我只会将此作为最后的选择,而是让您的网站发送适当的CORS header 。

*编辑*

看到您的源代码后,我相信您的代码存在问题:
var validator = document.createElement('iframe');
validator.src = "http://**********.com/?wvkey=" + input;

这会将您的网站加载到 <iframe>对象中,但是这是异步发生的。意味着您需要等待页面完成加载才能继续:
var status = validator.contentWindow.document.getElementById('status').innerHTML;

将您的代码更改为如下所示:
var validator = document.createElement('iframe');
validator.onload = function() {
var status = validator.contentWindow.document.getElementById('status').innerHTML;
[...]
};
validator.src = "http://**********.com/?wvkey=" + input;
body.appendChild(validator);

请注意,您仍然需要webSecurity或CORS设置才能访问 validator框架内容。

关于iframe - 在Electron中禁用同源策略(需要与iframe一起使用),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57813941/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com