electron 网页和主进程通讯

38次阅读

共计 742 个字符,预计需要花费 2 分钟才能阅读完成。

前端网页
import {
RxIpc
} from ‘rx-ipc-electron/lib/rx-ipc’;

export const IsElectron = window.navigator.userAgent.toLowerCase().indexOf(‘electron’) !== -1;

export function ajax(config) {
if (!IsElectron) return Promise.reject(‘only for electron’);

const {
ipcRenderer
} = eval(`require(‘electron’)`);
const rxIpc = new RxIpc(ipcRenderer);

return new Promise((resolve, reject) => {
rxIpc.runCommand(‘ajax’, null, config).subscribe(resolve, reject);
});
}

window[‘test1’] = async function test1() {
const url = ‘https://www.baidu.com/s’;

const params = {
wd: 1
};

const rsp = await ajax({
url,
method: ‘get’,
params,
});

console.log(rsp);
}

electron 进程
import rxIpc from ‘rx-ipc-electron/lib/main’;
import {Observable} from ‘rxjs’;
import axios from ‘axios’;

rxIpc.registerListener(‘ajax’, config => {
return Observable.from(axios(config));
});

正文完
 0