npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

@meetfranz/electron-cookies

v3.0.2

Published

Provides document.cookie support for Electron

Downloads

89

Readme

electron-cookies

Provides document.cookie support for Electron. For use with Amplitude and Google Analytics in Electron.

The underlying cookie implementation is Salesforce's tough-cookie library and Exponent's WebStorageCookieStore for persistence.

Installation

npm install @meetfranz/electron-cookies --save

Usage

import ElectronCookies from "@meetfranz/electron-cookies";

// Add support for document.cookie, using the given origin (protocol, host, and
// port)
ElectronCookies.enable({
  origin: "https://example.com",
});

// Remove support for document.cookie. Cookies are not cleared from the
// underlying storage.
ElectronCookies.disable();

Behavior

When getting or setting a cookie, the browser needs to know the current URL of the page.

In Electron, HTML files are usually served from the local filesystem, which has no domain. electron-cookies provides you a way to specify the origin of the URL to use:

ElectronCookies.enable({ origin: "https://example.com" });

This tells electron-cookies the origin of the URL to use when accessing cookies. The path of the URL is the relative path from the app's root to the path of the HTML file. So if the app is at /Users/you/Desktop/ and the HTML file is at /Users/you/Desktop/web/index.html, the synthesized URL will be https://example.com/web/index.html.

Alternatively, if you omit an origin, the full file: URL of the HTML file is used instead.

Google Analytics

To use Google Analytics in Electron, we use code like this:

<script>
  (function (i, s, o, g, r, a, m) {
    i["GoogleAnalyticsObject"] = r;
    (i[r] =
      i[r] ||
      function () {
        (i[r].q = i[r].q || []).push(arguments);
      }),
      (i[r].l = 1 * new Date());
    (a = s.createElement(o)), (m = s.getElementsByTagName(o)[0]);
    a.async = 1;
    a.src = g;
    m.parentNode.insertBefore(a, m);
  })(
    window,
    document,
    "script",
    "https://www.google-analytics.com/analytics.js",
    "ga"
  );

  (() => {
    "use strict";
    let ElectronCookies = require("@meetfranz/electron-cookies");
    ElectronCookies.enable({ origin: "https://example.com" });

    ga("create", GOOGLE_ANALYTICS_ID, "auto");
    ga("set", "location", "https://example.com/");
    ga("set", "checkProtocolTask", null);
    ga("send", "pageview");
  })();
</script>

Notable changes to the standard code include:

  • The analytics script URL is prefixed with https: instead of inheriting the page's protocol
  • We enable electron-cookies
  • We set the location field in Google Analytics
  • The checkProtocolTask field is set to null to disable checking for a file: protocol