使用Electron在浏览器中打开iframe链接

3
我想在Electron中使用浏览器打开iframe链接。我找到了一些解决方案,但它们都不起作用,以下是我尝试过的示例: 我认为问题在于链接在scr标签中。
寻找可能的解决方案,弄清为什么没有起作用。
这是一个iframe元素的示例。
<iframe src="https://rcm-eu.amazon-adsystem.com/e/cm?o=3&p=48&l=ur1&category=channels&banner=138WWDCBD6MQJVWGMHG2&f=ifr&linkID=0335593f7b48da8f8d1dab568039dc08&t=adrgoe-21&tracking_id=adrgoe-21" width="728" height="90" scrolling="no" border="0" marginwidth="0" style="border:none;" frameborder="0"></iframe>

以下是我的 Electron 代码:

const shell = require('electron').shell;

// assuming $ is jQuery
$(document).on('click', 'a[href^="http"]', function(event) {
    event.preventDefault();
    shell.openExternal(this.href);
});

请提供您遇到问题的代码。 - escapesequence
https://pastebin.com/raw/XUdb7DdV - Adrian
3个回答

1
我找到了一个解决方案。我将 iframe 改为了 webview:
<webview id="webview" src="https://stackoverflow.com/" nodeintegration></webview>

JS现在是:

const {shell} = require('electron')
const webview = document.querySelector('webview')
webview.addEventListener('will-navigate', (e) => {
  const protocol = require('url').parse(e.url).protocol
  if (protocol === 'http:' || protocol === 'https:') {
    shell.openExternal(e.url)
  }
});

除了'will-navigate',你可以选择不同的操作。在这里找到全部内容

webview.addEventListener('will-navigate', (e) => {

现在我需要找出如何停止WebView中的页面更改,但它会在默认浏览器中打开链接。

0
如果我正确理解 Github 上的 threads,其中一种方法是使用 <webview> 而不是 <iframe>。然后在你的 main.js / 浏览器进程中放入类似以下的代码,而不是 渲染器进程。
app.on('web-contents-created', (event, contents) => {
  if (contents.getType() === 'webview') {
    contents.on('will-navigate', (event, url) => {
      event.preventDefault();
      shell.openExternal(url);
    });
  }
});

将代码放在渲染进程中不起作用,至少在 Electron 1.8.4 中是这样。


0

你正在检测对 a[href^="http"] 的点击,但你的标签是一个 iframe

实际上,你应该给这个 iframe 一个 id 或者其他什么东西,然后处理你的点击事件。例如:

<iframe id="myframe" src="...></iframe>

并且

const shell = require('electron').shell;

// assuming $ is jQuery
$(document).on('click', 'a[href^="http"]', function(event) {
    event.preventDefault();
    var iframe = document.getElementById('myframe')
    console.log(iframe, event.target) // what are these?
    if(iframe) {
       shell.openExternal(iframe.href);
    }
});

我尝试使用这段代码并对其进行了几次尝试。但是我不明白如何在标准浏览器中打开链接。 - Adrian

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接