Cookies模块无法在Electron应用程序中运行

时间:2016-03-02 17:55:34

标签: reactjs electron

我正在尝试从浏览器标题返回cookie,但我甚至无法让模块工作。我可以运行应用程序,但收到错误消息:Error Alert。我正在使用Node.JS和Electron + Webpack + React。这是我的代码:

var app = require('app');
var BrowserWindow = require('browser-window');
var http = require('http');
var Cookies = require('./node_modules/cookies');
require('crash-reporter').start();

app.on('window-all-closed', function() {
  if (process.platform != 'darwin') {
    app.quit();
  }
});

app.on('ready', function() {
  mainWindow = new BrowserWindow({
  	frame: true,
  	width: 1200,
  	resizable: false,
  	height: 800,
    'web-preferences': {'web-security': false}});
  
  mainWindow.setMenu(null);

  mainWindow.loadUrl('file://' + __dirname + '/public/index.html');

  mainWindow.openDevTools();

  mainWindow.on('closed', function() {
    mainWindow = null;
  });
  
  mainWindow.on.session.Cookies.get({}, function(error, Cookies) {
    if (error) throw error;
    console.log(Cookies);
  });
});

的package.json:

{
  "name": "BSB-app",
  "version": "0.1.0",
  "description": "",
  "main": "main.js",
  "devDependencies": {
    "babel": "^6.5.2",
    "babel-cli": "^6.6.0",
    "babel-core": "^6.5.2",
    "babel-loader": "^6.2.3",
    "babel-preset-es2015": "^6.6.0",
    "babel-preset-react": "^6.5.0",
    "babel-register": "^6.6.0",
    "css-loader": "^0.23.1",
    "electron-cookies": "^1.1.0",
    "electron-packager": "^5.2.1",
    "electron-rebuild": "^1.1.3",
    "less": "^2.6.0",
    "less-loader": "^2.2.2",
    "node-libs-browser": "^1.0.0",
    "style-loader": "^0.13.0",
    "webpack": "^1.12.14",
    "webpack-dev-server": "^1.14.1"
  },
  "dependencies": {
    "babel-polyfill": "^6.6.1",
    "electron-prebuilt": "^0.36.8",
    "exports-loader": "^0.6.3",
    "imports-loader": "^0.6.5",
    "react": "^0.14.7",
    "react-dom": "^0.14.7",
    "react-redux": "^4.4.0",
    "redux": "^3.3.1",
    "whatwg-fetch": "^0.11.0"
  },
  "scripts": {
    "start": "electron .",
    "watch": "node_modules/.bin/webpack-dev-server",
    "electron-rebuild": "node_modules/.bin/electron-rebuild"
  }
}

Webpack.config.js:

const webpack = require('webpack');

const production = process.env.NODE_ENV === 'production';

module.exports = {
  entry: {	
    app: ['webpack/hot/dev-server', './javascripts/entry.js']
  },

  output: {
    path: './public/built',
    filename: 'bundle.js',
    publicPath: 'http://localhost:8080/built/'
  },

  devServer: {
    contentBase: './public',
    publicPath: 'http://localhost:8080/built/'
  },
  resolveLoader: {
      modulesDirectories: [
          'node_modules'
      ]
	},
  module: {
    loaders: [
      { test: /\.jsx?$/, loader: 'babel-loader', query: { presets: ['es2015', 'react'] }, exclude: /node_modules/ },
      { test: /\.css$/, loader: 'style-loader!css-loader' },
      { test: /\.less$/, loader: 'style-loader!css-loader!less-loader'},
    ]
  },

  plugins: [
    new webpack.HotModuleReplacementPlugin(),
    new webpack.DefinePlugin({
      'process.env': {NODE_ENV: JSON.stringify(process.env.NODE_ENV)},
    }),
    new webpack.ProvidePlugin({
    'fetch': 'imports?this=>global!exports?global.fetch!whatwg-fetch'
  }),
  	new webpack.IgnorePlugin(new RegExp("^(fs|ipc)$"))
  ]
}

1 个答案:

答案 0 :(得分:1)

要在渲染器进程中使用的electron-cookies包,您尝试在主/浏览器进程中使用它。在渲染过程中,您应该能够执行以下操作:

require('electron-cookies');

document.cookie = 'key=value; key2=value2';
// or to clear the cookies
document.clearCookies();