在使用电子的浏览器中打开iframe链接
问题描述:
我想在使用电子的浏览器中打开iframe链接。我找到了一些解决方案,但他们没有工作,这里有例子我想:在使用电子的浏览器中打开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>
这是我的电子代码
const shell = require('electron').shell;
// assuming $ is jQuery
$(document).on('click', 'a[href^="http"]', function(event) {
event.preventDefault();
shell.openExternal(this.href);
});
答
我找到了解决办法。 我改变了iframe来网页视图:
<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)
}
});
而不是 '将导航的',你可以选择不同的操作。 Find the all here
webview.addEventListener('will-navigate', (e) => {
现在我必须找出,如何停止在web视图中更改页面。 但它在默认浏览器中打开链接。
答
您在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);
}
});
答
如果我理解这样做的threads在github上一个方法是使用一个<webview>
代替的。然后把这样的代码在你的main.js /浏览器进程不渲染过程
app.on('web-contents-created', (event, contents) => {
if (contents.getType() === 'webview') {
contents.on('will-navigate', (event, url) => {
event.preventDefault();
shell.openExternal(url);
});
}
});
把代码中渲染过程将无法正常工作,至少为电子1.8.4
请提供代码,你有麻烦。 – escapesequence
https://pastebin.com/raw/XUdb7DdV – Adrian