├── .gitignore ├── Gruntfile.coffee ├── LICENSE.txt ├── README.md ├── package.json ├── spec └── electron_cookies_spec.coffee └── src ├── index.coffee └── index.js /.gitignore: -------------------------------------------------------------------------------- 1 | node_modules 2 | spec/electron_cookies_spec.js 3 | -------------------------------------------------------------------------------- /Gruntfile.coffee: -------------------------------------------------------------------------------- 1 | module.exports = (grunt) -> 2 | grunt.loadNpmTasks 'grunt-contrib-coffee' 3 | grunt.loadNpmTasks 'grunt-simple-mocha' 4 | grunt.loadNpmTasks 'grunt-contrib-watch' 5 | grunt.initConfig 6 | coffee: 7 | dev: files: 'src/index.js': 'src/*.coffee' 8 | test: files: 'spec/electron_cookies_spec.js': 'spec/*.coffee' 9 | simplemocha: dev: 10 | src: 'spec/electron_cookies_spec.js' 11 | options: 12 | reporter: 'spec' 13 | slow: 200 14 | timeout: 1000 15 | watch: all: 16 | files: [ 17 | 'src/*.coffee' 18 | 'spec/*.coffee' 19 | ] 20 | tasks: [ 21 | 'buildDev' 22 | 'buildTest' 23 | 'test' 24 | ] 25 | grunt.registerTask 'test', 'simplemocha:dev' 26 | grunt.registerTask 'buildDev', 'coffee:dev' 27 | grunt.registerTask 'buildTest', 'coffee:test' 28 | grunt.registerTask 'default', [ 29 | 'buildDev' 30 | 'buildTest' 31 | 'test' 32 | 'watch:all' 33 | ] 34 | 35 | # --- 36 | # generated by js2coffee 2.0.4 37 | -------------------------------------------------------------------------------- /LICENSE.txt: -------------------------------------------------------------------------------- 1 | The MIT License (MIT) 2 | 3 | Copyright (c) 2015 Hank Stoever 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. 22 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # Important! This repository has moved. 2 | 3 | For the currenlty maintained version, see [heap/electron-cookies](https://github.com/heap/electron-cookies). 4 | 5 | # Electron Cookies 6 | 7 | (Formerly called `atom-shell-cookies`) 8 | 9 | Adds support for cookies in Electron. Cookies are persisted through localStorage. 10 | 11 | Forked from https://gist.github.com/paulcbetts/2d2de55d137a1cf9d1ac. 12 | 13 | ## Why? 14 | 15 | Electron's `renderer` environment doesn't come with built in support for a `document.cookie` API. Thus, if you want to use Google Analytics or another client-side analytics library, they won't work because they can't set cookies. 16 | 17 | By using this package, you can drop client-side analytics code into your app and it will work splendidly. 18 | 19 | ## Installation 20 | 21 | ```bash 22 | npm install electron-cookies 23 | ``` 24 | 25 | ## Usage 26 | 27 | In your app's `renderer` code, just require this package: 28 | 29 | ```js 30 | require('electron-cookies') 31 | ``` 32 | 33 | ## Contributing 34 | 35 | Original code is written in `src/index.coffee`, with tests at `spec/electron_cookies_spec.coffee`. Write code in coffeescript, and run `grunt` to compile coffeescript on the fly. 36 | -------------------------------------------------------------------------------- /package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "electron-cookies", 3 | "version": "1.1.0", 4 | "description": "Adds support for cookies in Electron.", 5 | "main": "src/index.js", 6 | "scripts": { 7 | "test": "grunt test" 8 | }, 9 | "keywords": [ 10 | "atom", 11 | "shell", 12 | "cookies", 13 | "electron" 14 | ], 15 | "author": "Hank Stoever", 16 | "license": "MIT", 17 | "repository": "hstove/electron-cookies", 18 | "devDependencies": { 19 | "chai": "^2.3.0", 20 | "grunt": "^0.4.5", 21 | "grunt-contrib-coffee": "^0.13.0", 22 | "grunt-contrib-watch": "^0.6.1", 23 | "grunt-simple-mocha": "^0.4.0", 24 | "mocha": "^2.2.4", 25 | "shoulda": "0.0.1" 26 | } 27 | } 28 | -------------------------------------------------------------------------------- /spec/electron_cookies_spec.coffee: -------------------------------------------------------------------------------- 1 | {expect} = require 'chai' 2 | global.document = {} 3 | global.localStorage = {} 4 | require('../src/index.coffee') 5 | 6 | describe 'document.cookies', -> 7 | it 'should work like a browser', -> 8 | expect(document.cookie).to.eql('') 9 | document.cookie = 'blah' 10 | expect(document.cookie).to.eql('blah') 11 | document.cookie = 'key=value' 12 | expect(document.cookie).to.eql('blah; key=value') 13 | document.cookie = 'key2=value2' 14 | expect(document.cookie).to.eql('blah; key=value; key2=value2') 15 | 16 | describe 'document.clearCookies', -> 17 | it 'should clear cookies', -> 18 | expect(document.clearCookies()).to.eql(true) 19 | document.cookie = 'key2=value2' 20 | expect(document.clearCookies()).to.eql(true) 21 | expect(document.cookie).to.eql('') 22 | -------------------------------------------------------------------------------- /src/index.coffee: -------------------------------------------------------------------------------- 1 | do (document) -> 2 | localStorage.cookies ||= '{}' 3 | document.__defineGetter__ 'cookie', -> 4 | cookies = JSON.parse(localStorage.cookies || '{}') 5 | output = [] 6 | for cookieName, val of cookies 7 | validName = cookieName && cookieName.length > 0 8 | res = if validName then "#{cookieName}=#{val}" else val 9 | output.push res 10 | output.join '; ' 11 | 12 | document.__defineSetter__ 'cookie', (s) -> 13 | parts = s.split('=') 14 | if parts.length == 2 15 | [key, value] = parts 16 | else 17 | [value] = parts 18 | key = '' 19 | cookies = JSON.parse(localStorage.cookies || '{}') 20 | cookies[key] = value 21 | localStorage.cookies = JSON.stringify(cookies) 22 | key + '=' + value 23 | 24 | document.clearCookies = -> delete localStorage.cookies 25 | 26 | # Pretend that we're hosted on an Internet Website 27 | document.__defineGetter__ 'location', -> 28 | url = 'electron-renderer.com' 29 | 30 | href: 'http://' + url 31 | protocol: 'http:' 32 | host: url 33 | hostname: url 34 | port: '' 35 | pathname: '/' 36 | search: '' 37 | hash: '' 38 | username: '' 39 | password: '' 40 | origin: 'http://' + url 41 | 42 | document.__defineSetter__ 'location', -> 43 | -------------------------------------------------------------------------------- /src/index.js: -------------------------------------------------------------------------------- 1 | (function() { 2 | (function(document) { 3 | localStorage.cookies || (localStorage.cookies = '{}'); 4 | document.__defineGetter__('cookie', function() { 5 | var cookieName, cookies, output, res, val, validName; 6 | cookies = JSON.parse(localStorage.cookies || '{}'); 7 | output = []; 8 | for (cookieName in cookies) { 9 | val = cookies[cookieName]; 10 | validName = cookieName && cookieName.length > 0; 11 | res = validName ? cookieName + "=" + val : val; 12 | output.push(res); 13 | } 14 | return output.join('; '); 15 | }); 16 | document.__defineSetter__('cookie', function(s) { 17 | var cookies, key, parts, value; 18 | parts = s.split('='); 19 | if (parts.length === 2) { 20 | key = parts[0], value = parts[1]; 21 | } else { 22 | value = parts[0]; 23 | key = ''; 24 | } 25 | cookies = JSON.parse(localStorage.cookies || '{}'); 26 | cookies[key] = value; 27 | localStorage.cookies = JSON.stringify(cookies); 28 | return key + '=' + value; 29 | }); 30 | document.clearCookies = function() { 31 | return delete localStorage.cookies; 32 | }; 33 | document.__defineGetter__('location', function() { 34 | var url; 35 | url = 'electron-renderer.com'; 36 | return { 37 | href: 'http://' + url, 38 | protocol: 'http:', 39 | host: url, 40 | hostname: url, 41 | port: '', 42 | pathname: '/', 43 | search: '', 44 | hash: '', 45 | username: '', 46 | password: '', 47 | origin: 'http://' + url 48 | }; 49 | }); 50 | return document.__defineSetter__('location', function() {}); 51 | })(document); 52 | 53 | }).call(this); 54 | --------------------------------------------------------------------------------