简体   繁体   English

从Electron应用程序的外部站点加载div

[英]Loading div from external site in Electron app

TLDR: How can I show #div from another URL inside Electron app. TLDR:如何从Electron应用程序内的另一个URL显示#div。

I'm learning javascript and I've came across this problem. 我正在学习javascript,遇到了这个问题。 I wanted to load specific div from another website inside electron app. 我想从电子应用程序内的另一个网站加载特定的div。 Already tried looking for the solution online and reading docs, but that failed. 已经尝试过在线寻找解决方案并阅读文档,但是失败了。 I've tried using javascript with this: 我试过使用javascript:

$("#test_div-in_electron").load("https://some-website.com #test-div-on-webpage")

with scripts in index.html: 使用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>

And it shows - nothing. 它显示-什么都没有。 blank div. 空白区

What you are doing is called a Cross Origin Request: 您正在做的事情称为跨源请求:

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. 当Web应用程序请求其来源(域,协议和端口)与其来源不同的资源时,它会发出跨域HTTP请求。

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 . 跨域请求的示例:从http://domain-a.com服务的Web应用程序的前端JavaScript代码使用XMLHttpRequest发出对http://api.domain-b.com/data.json的请求。

For security reasons, browsers restrict cross-origin HTTP requests initiated from within scripts. 出于安全原因,浏览器限制了从脚本内部发起的跨域HTTP请求。 For example, XMLHttpRequest and the Fetch API follow the same-origin policy. 例如,XMLHttpRequest和Fetch API遵循同源策略。 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. 这意味着使用这些API的Web应用程序只能从加载该应用程序的同一来源请求HTTP资源,除非来自其他来源的响应包括正确的CORS标头。

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

In Electron, you can disable this security on opening the main window: 在Electron中,可以在打开主窗口时禁用此安全性:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM