electron icon indicating copy to clipboard operation
electron copied to clipboard

Add webview.loadURL

Open christoth opened this issue 10 years ago • 14 comments

Could you please expose the extraHeaders option from WebContents.loadURL as a WebView attribute? It would also be great to have access to loadURL on WebView as a method (like BrowserWindow). Thanks

christoth avatar Jan 14 '16 12:01 christoth

Adding loadURL to webview looks good to me. However adding extraHeaders attribute seems unnecessary, you can use the webRequest API.

zcbenz avatar Jan 14 '16 12:01 zcbenz

loadURL on webview would be great.

christoth avatar Jan 14 '16 13:01 christoth

@zcbenz is it as simple as this? https://github.com/atom/electron/pull/4186

CharlieHess avatar Jan 21 '16 21:01 CharlieHess

yep.

zcbenz avatar Jan 21 '16 21:01 zcbenz

having some trouble with this API. If I dont have the src attribute in the webview to start with, then the loadURL gives an error -

function updateWebviewURL(u) { var webv = document.getElementById('mainwebview'); if (webv) { webv.loadURL(u); webv.style.display = 'block'; } };

Uncaught TypeError: Cannot read property 'loadURL' of undefined node_modules/electron-prebuilt/dist/Electron.app/Contents/Resources/atom.asar/renderer/lib/web-view/web-view.js:410

using electron-prebuilt version 0.36.7

Anyone else seen this scenario?

rishibhargava avatar Feb 03 '16 05:02 rishibhargava

Yeah, I am having the same issue! The documentation states that you should use the dom-ready event first:

webview.addEventListener("dom-ready", function() {
  webview.openDevTools();
});

But trying to use .loadURL inside this event's callback will trigger the dom-ready event again once it's loaded, which calls the .loadURL!!

tamkeen-tms avatar Mar 14 '16 12:03 tamkeen-tms

Yes this is indeed how it works. I dealt with this by just checking whether this is the initial load or not.

Rishi Bhargava 408-905-8344 Demisto.comhttp://www.demisto.com/

From: Ahmed Zain <[email protected]mailto:[email protected]> Reply-To: atom/electron <[email protected]mailto:[email protected]> Date: Monday, March 14, 2016 at 5:09 AM To: atom/electron <[email protected]mailto:[email protected]> Cc: Rishi Bhargava <[email protected]mailto:[email protected]> Subject: Re: [electron] Add webview.loadURL (#4101)

Yeah, I am having the same issue! The documentation states that you should use the dom-ready event first:

webview.addEventListener("dom-ready", function() { webview.openDevTools(); });

But trying to use .loadURL inside this event's callback will trigger the dom-ready event again once it's loaded, which calls the .loadURL!!

— Reply to this email directly or view it on GitHubhttps://github.com/atom/electron/issues/4101#issuecomment-196281255.

rishibhargava avatar Mar 15 '16 00:03 rishibhargava

Sure, me too. But I think that there should be a closer look into this, or not. It's just a bit weird.

tamkeen-tms avatar Mar 15 '16 08:03 tamkeen-tms

I'm having the exact same issue with 'webview.loadURL()', if I use it inside the 'dom-ready' handler it will fire the event again, and therefore create a URL loading bucle. Is this the intended behaviour?

manuelro avatar Jan 21 '17 05:01 manuelro

I was having the same issue with the page below:

<webview id="app" preload="./preload.js" style="width:100%; height:100%">
</webview>

<script>
ipcRenderer.on('load-url', (events,url)=>{
    webview.loadURL(url)
})
</script>

This will give the following error:

Uncaught Error: The WebView must be attached to the DOM and the dom-ready event emitted before this method can be called.
    at HTMLElement.WebViewElement.getWebContentsId (web-view-impl.ts:223)
    at HTMLElement.loadURL (web-view-impl.ts:266)
    at EventEmitter.<anonymous> (index.html:8)
    at EventEmitter.emit (events.js:210)
    at Object.onMessage (init.ts:50)

Solution (More of a workaround)

Turns out this is caused by no source tag in webview. A quick workaround is just adding a dummy source to the initial webview

<webview id="app" preload="./preload.js" src=false style="width:100%; height:100%"></webview>

ignis-sec avatar Apr 17 '20 10:04 ignis-sec

@FlameOfIgnis unfortunately that didn't work for me, same error message.

Nantris avatar May 09 '20 23:05 Nantris

This is like pulling teeth. I thought I could fix this issue in 10-15 minutes, but it's now the second hour with no end in sight. We really have to load a site just to call loadURL?

Nantris avatar May 09 '20 23:05 Nantris

I use innerHtml,

const url = 'Your website'
document.body.innerHTML= `<webview style="height:100%" src="${url}" disablewebsecurity id="wv"></webview>`

kunfan96 avatar Oct 23 '20 08:10 kunfan96

It's highly inadvisable to use disablewebsecurity.

Nantris avatar Oct 23 '20 18:10 Nantris