Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Electron preload script for webview not working?

I have this webview that I load like this:

<webview id="link-view"
        src="http://url.espace.com/"
        preload="./preload.js"
        autosize="on"
        partition="persist:link"></webview>

In the preload.js file : I have this

console.log("test");

This doesn't even work..

Can someone explain?

like image 970
julestruong Avatar asked Mar 19 '16 15:03

julestruong


3 Answers

I just tried the same scenario, and it worked as expected.

Do you have the <webview> dev tools open? As a <webview>has its own dev tools, That would be the only reason i can see for the preload script not showing the console.log output. They can be opened by:

var webview = document.getElementById("link-view");
webview.openDevTools();

Or your path to the script is incorrect, which i doubt as you seem sure about that.

like image 77
teimaj Avatar answered Oct 29 '22 07:10

teimaj


Using latest Electron 2.0.0 (beta-5, at the time of this writing), the preload attribute demands a file: (or asar:) protocol followed by an absolute path. Paths such as ./preload.js don't seem to work anymore and raise an exception: Only "file:" protocol is supported in "preload" attribute.

like image 25
Nicolás Fantone Avatar answered Oct 29 '22 07:10

Nicolás Fantone


This solved my problem in electron 3.0.0-beta.4 using react.js

<webview src={'http://example.com'} preload={`file://${__dirname}/preload.js`}/>
like image 38
Luis Solórzano Avatar answered Oct 29 '22 06:10

Luis Solórzano