简体   繁体   English

反应+ Electron webview标签预加载js不起作用

[英]React + Electron webview tag preload js not working

When I use webview tag, preload js not working当我使用 webview 标签时,预加载 js 不起作用

my code is as follows:我的代码如下:

// test.js
<div>
    <webview id="foo" src="https://www.github.com/" preload="file://preload.js"/>
</div>

my preload.js file:我的 preload.js 文件:

// preload.js
alert('preload success')

I also tried this:我也试过这个:

// test.js
<div>
    <webview id="foo" src="https://www.github.com/" preload={`file://${__dirname}/preload.js`}/>
</div>

and I even intercepted the file protocol in main.js我什至截获了 main.js 中的文件协议

// main.js
app.on('ready', () => {
  protocol.interceptFileProtocol('file', (request, callback) => {
    console.log('success')
    const url = request.url.substr(7);
    callback({path: path.normalize(`${__dirname}/${url}`)});
  }, (error) => {
    if (error) console.error('Failed to intercept protocol');
  });
})
;

But none of these attempts worked.但这些尝试都没有奏效。 There is no "success" in the console and no "success" alert.控制台中没有“成功”,也没有“成功”警报。 But if I use an absolute path like this:但是如果我使用这样的绝对路径:

// test.js
<div>
    <webview id="foo" src="https://www.github.com/" preload="file:///home/ppp/electron-dict/src/components/preload.js"/>
</div>

the code runs fine and my application alerts "success."代码运行良好,我的应用程序提示“成功”。 Why is this?为什么是这样?

my project path:我的项目路径:

├── public
│   ├── index.html
│   ├── main.js
├── src
│   ├── App.js
│   ├── components
│   │   ├── test.js
│   │   ├── preload.js
│   └── index.js

Electron and React versions: Electron 和 React 版本:

├─ electron@7.1.1
├─ react@16.11.0

Indicate preload is a top level attribute of the webview tag, not inside webpreferences指示预加载是 webview 标记的顶级属性,不在 webpreferences 内

<webview src="https://www.github.com/" preload="./test.js"></webview>

A String that specifies a script that will be loaded before other scripts run in the guest page.一个字符串,它指定将在访客页面中运行其他脚本之前加载的脚本。 The protocol of script's URL must be either file: or asar: , because it will be loaded by require in guest page under the hood.脚本的 URL 的协议必须是file:asar: ,因为它会在后台的访客页面中由require加载。

When the guest page doesn't have node integration this script will still have access to all Node APIs, but global objects injected by Node will be deleted after this script has finished executing.当访客页面没有节点集成时,此脚本仍然可以访问所有 Node API,但在此脚本执行完成后,由 Node 注入的全局对象将被删除。

Note: This option will appear as preloadURL (not preload ) in the webPreferences specified to the will-attach-webview event.注意:此选项将在指定给will-attach-webview事件的webPreferences中显示为preloadURL (而不是preload )。

You can find more info on Electron preload script for webview not working?您可以找到有关 webview 的Electron 预加载脚本不起作用的更多信息?

I have been able to preload js, but not using the preload attribute.我已经能够预加载 js,但没有使用 preload 属性。 Instead, I inject it from the main process:相反,我从主进程注入它:

app.on('web-contents-created', (_event, contents) => {
  contents.on('will-attach-webview', (_wawevent, webPreferences, _params) => {
    webPreferences.preloadURL = `file://${__dirname}/webview-preload.js`;
  });
});

Using React in the renderer.在渲染器中使用 React。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM