Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Js Error #71

Open
Hunlongyu opened this issue Jul 9, 2020 · 4 comments
Open

Js Error #71

Hunlongyu opened this issue Jul 9, 2020 · 4 comments

Comments

@Hunlongyu
Copy link

我使用的是这个 electron + vue 插件: https://nklayman.github.io/vue-cli-plugin-electron-builder/
不是常见的 Electron-vue 插件.
我在主进程 background.js 里:

import { ipcMain } from 'electron'
import { ebtMain } from 'electron-baidu-tongji'
ebtMain(ipcMain)

这样写报错.

截图如下:

Snipaste_2020-07-09_13-54-31.png

@Hunlongyu
Copy link
Author

希望能早点解决呀~

@joehecn
Copy link
Owner

joehecn commented Sep 14, 2020

平时这个包在自用,抱歉刚刚看到这个问题,我没有用过你提到的插件,或者你试试整体导入呢?

import { ipcMain } from 'electron'
import baiduTongji  from 'electron-baidu-tongji'
const { ebtMain } = baiduTongji
ebtMain(ipcMain)

这篇文章或许对你有帮助:Node.js 如何处理 ES6 模块

@greper
Copy link

greper commented Nov 12, 2020

我使用vue-cli-plugin-electron-builder 有同样的问题
主要原因是那个superagent 引用了formidable
这个formidable很多文件开头都有下面的代码
if (global.GENTLY) require = GENTLY.hijack(require);
webpack编译完成后会把require置空,即使将global.GENTLY=false也无济于事
formidable的issue上有说升级到v2可以解决,我尝试过,会有另外的问题。

一个http请求而已,用superagent有点杀鸡用牛刀的感觉了
所以我的解决方案是把superagent换成request
主要代码其实挺简单的就是一个index.js
直接把index.js 放到自己项目里面修改如下部分即可


const request = require('request')

/**
 * first step
 * @param {*} ipcMain
 */
const ebtMain = (ipcMain, isDevelopment) => {
  /* istanbul ignore else */
  if (!(ipcMain && ipcMain.on)) {
    throw new TypeError('require ipcMain')
  }

  // step 2
  ipcMain.on('electron-baidu-tongji-message', (event, arg) => {
    // electron 生产模式下是直接请求文件系统,没有 http 地址
    // 前台拿不到 hm.js 的内容
    request({
      url: `https://hm.baidu.com/hm.js?${arg}`,
      method: 'GET',
      headers: {
        Referer: 'https://hm.baidu.com/'
      }
    },
    (err, response, body) => {
      if (err) {
        console.error('百度统计请求出错', err)
      }
      const rource = '(h.c.b.su=h.c.b.u||document.location.href),h.c.b.u=f.protocol+"//"+document.location.host+'
      /* istanbul ignore else */
      if (body && body.indexOf(rource) >= 0) {
        // step 3
        let text = body
        ....

@joehecn
Copy link
Owner

joehecn commented Nov 18, 2020

@greper 谢谢指教,空闲的时候,我升级一下吧,我打算不用外部依赖,直接用nodejs 的标准模块 https 解决了

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants