javascript - 在 Electron 应用程序中从外部站点加载 div

标签 javascript electron

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.

https://developer.mozilla.org/en-US/docs/Web/HTTP/CORS

在 Electron 中,您可以在打开主窗口时禁用此安全性:

new BrowserWindow({
  ..
  webPreferences: {
    ..
    webSecurity: false
  }
});

关于javascript - 在 Electron 应用程序中从外部站点加载 div,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52475151/

相关文章:

javascript - 如何在将鼠标指针移到按钮上时显示更多信息?

javascript - 如何为 Electron 应用程序提供 Django

javascript - jquery fancybox - 防止在 fancybox 外点击关闭

javascript - 如何在任务中使用命令行参数退出应用程序

javascript - 如何从 Electron 应用程序检测桌面空闲时间?

javascript - 如何在 Electron 渲染器进程中使用 RxJs 等模块?

electron - 安装与python脚本通信后无法拖动文件

javascript - 在两个 Javascript 文件之间共享常量值

javascript - 只检测鼠标滚动一次

javascript - 具有插值的 ng 样式不渲染背景图像?