2017-08-28 62 views
2

我想在使用電子的瀏覽器中打開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); 
}); 
+0

請提供代碼,你有麻煩。 – escapesequence

+0

https://pastebin.com/raw/XUdb7DdV – Adrian

回答

1

我找到了解決辦法。 我改變了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視圖中更改頁面。 但它在默認瀏覽器中打開鏈接。

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); 
    } 
}); 
+0

我試圖使用代碼,並嘗試了一些東西。但我不明白標準瀏覽器中的鏈接是打開的。 – Adrian

+0

看我的編輯,記錄的數據是什麼? – Fraser

0

如果我理解這樣做的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

相關問題