TLDR:如何从 Electron 应用程序内的另一个 URL 显示 #div。
我正在学习javascript,我遇到了这个问题。我想从 Electron 应用程序内的另一个网站加载特定的 div。已经尝试在线寻找解决方案并阅读文档,但失败了。 我尝试过使用 javascript 来实现:
$("#test_div-in_electron").load("https://some-website.com #test-div-on-webpage")
在index.html中使用脚本:
<script>if (typeof module === 'object') {window.module = module; module = undefined;}</script>
<script src="../app/resources/js/renderer_main.js"></script>
<script>if (window.module) module = window.module;</script>
它显示 - 什么也没有。空白div。
最佳答案
您所做的称为跨源请求:
A web application makes a cross-origin HTTP request when it requests a resource >that has a different origin (domain, protocol, and port) than its own origin.
An example of a cross-origin request: The frontend JavaScript code for a web application served from http://domain-a.com uses XMLHttpRequest to make a request for http://api.domain-b.com/data.json.
For security reasons, browsers restrict cross-origin HTTP requests initiated from within scripts. For example, XMLHttpRequest and the Fetch API follow the same-origin policy. This means that a web application using those APIs can only request HTTP resources from the same origin the application was loaded from, unless the response from the other origin includes the right CORS headers.
在 Electron 中,您可以在打开主窗口时禁用此安全性:
new BrowserWindow({
..
webPreferences: {
..
webSecurity: false
}
});
关于javascript - 在 Electron 应用程序中从外部站点加载 div,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52475151/