The background,

How to inject JS into electron

Some older brother proposed using preload.js injection

I was surprised, indicating disbelief, and then thought carefully:

“With preload.js, you can preload some variables from the electron main thread and hang them on the window. Later, you can get the corresponding variables from the Render thread.

In other words, the window obtained in preload.js is the window of the later Render thread, which theoretically can do injection operation.

If I add the window’s lifecycle to preload.js, and get the corresponding DOM node by id or whatever after rendering, I should theoretically be able to hijack some buttons, which means the injection succeeded.”

Two, said to do

1, the old rule, vue+vue-cli-plugin-electron- Builder to create the project

2, background.js, directly load www.baidu.com

3. Add preload.js

4. Add the window lifecycle to preload.js

window.load = function(){ <! -- XXXXX -->}Copy the code

Effect of three,

Click Baidu to trigger my code

Video:

Screenshots:

Refer to the article

preload.js

Node Integration