3

前端网页

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));
});

Chobits
7.4k 声望171 粉丝

保卫骡博 Chobits#3780