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

@getdesigned-vienna/cookie-consent

v1.1.5

Published

Cookie Consent completely configured via HTML

Downloads

5

Readme

Getdesigned Cookie Consent (GDCC)

  • GDCC is a JS library that helps setting up a cookie consent easily
  • No JS configuration required, everything is set in the cookie consent form
  • Supports single cookies and groups
  • Supports replacing certain content (e.g. YouTube) with replacements as long as the required cookie is not permitted
  • Supports i18n via HTML attributes
  • Is capable of deleting cookies, if the category is disabled.
  • Comes with a handy debug mode to help you setup everything

Installation

  • Install via npm or yarn

      npm i @getdesigned-vienna/cookie-consent
      yarn add @getdesigned-vienna/cookie-consent
  • Include jQuery (> 3) and fg-cookie in your built JS file (both come as GDCC npm dependency)

  • Make sure your built JS file is included in your source

  • Initialize Getdesigned Cookie Consent once the DOM is ready. Setting the first (and only) param to true enables debug mode (Have a look at the console, especially errors and warnings when debug mode is enabled).

      $(function () {
          gdcc = new GdCookieConsent(true);
      });
  • Make sure your HTML includes the GDCC wrapper (e.g. a DIV) with the cookie consent form inside and maybe a noscript tag:

      <div id="gdcc-wrapper">
          <noscript>...</noscript>
          <form>
          ...
          </form>
      </div>
  • Now setup your cookie configuration using checkboxes (inputs) with labels and optionally fieldsets with legends.

    • Every checkbox NEEDS a unique ID, used for referencing in labels and content elements (see further below)

    • Fieldsets can have an ID for referencing all included checkboxes as a group from submit buttons

    • Required cookies are marked with the attributes "checked" and "disabled"

       <fieldset>
            <legend>Functional</legend>
            <input type="checkbox" id="session" checked disabled/>
            <label for="session">Session Cookie</label>
            <input type="checkbox" id="consent" checked disabled/>
            <label for="consent">Cookie Consent</label>
        </fieldset>
        <fieldset id="social-media">
            <legend>Social Media</legend>
            <input type="checkbox" id="youtube"/>
            <label for="youtube">YouTube</label>
            <input type="checkbox" id="twitter"/>
            <label for="twitter">Twitter</label>
        </fieldset>
  • Finally add at least one submit button to the form which will automatically trigger saving the consent.

    • Alternatively you can use any DOM element with the class ".gdcc-save-consent".

    • The optional attribute "data-gdcc-select" allows to define one or more IDs of inputs or fieldsets to be activated automatically (presets). Use * to select all and - to select only required cookies.

        <input type="submit" value="Save consent"/>
        <input type="submit" data-gdcc-select="marketing, image, twitter" value="Marketing + Image"/>
        <a href="#" class="gdcc-save-consent" data-gdcc-select="*">Allow all</a>
        <input type="submit" data-gdcc-select="-" value="Go minimum"/>
  • GDCC is now ready to use and should save your cookie consent settings.

  • If something is not working, enable debug mode (see above) and check the console of your browser.

Prepare your content

  • Content to be shown/hidden when a certain cookie is (not) accepted can be defined in 3 ways. "optin" means that the content is shown only if the corresponding cookie is accepted, "optout" is shown otherwise. You must always reference the corresponding cookie via its checkboxes' ID:

    • Add classes "gdcc-optin-<input id>" and "gdcc-optout-<input id>", e.g.

        <div>
            <img class="gdcc-optin-image gdcc-hide" data-gdcc-src="https://placekitten.com/g/300/200"/>
            <div class="gdcc-optout-image"><h2 style="background-color: forestgreen">Image Replacement #1</h2></div>
        </div> 
    • Add classes "gdcc-optin" and "gdcc-optout" together with an attribute "data-gdcc-cookie=<input id>", e.g.

        <div>
            <img class="gdcc-optin gdcc-hide" data-gdcc-cookie="image" data-gdcc-src="https://placekitten.com/g/200/300"/>
            <div class="gdcc-optout" data-gdcc-cookie="image"><h2 style="background-color: forestgreen">Image Replacement #2</h2></div>
        </div>
    • Instead of a CSS class you can also use the attribute "data-gdcc-selector", e.g. "data-gdcc-selector=gdcc-optin-<input id>"

  • "optin" content will automatically get the css class "gdcc-hide" added (see abolve). Make sure it is already in place when you define optin content to avoid flashing and accidental loading of the resource. Otherwise GDCC will complain anyway (in debug mode).

  • HTML elements with a "src" attribute, which are optin, should provide the attribute as "data-gdcc-src". GDCC will handle the replacement automatically, if the consent is given. E.g.

      <iframe class="gdcc-optin-youtube gdcc-hide" 
          data-gdcc-src="https://www.youtube.com/embed/ePkGwBTkUXs" frameborder="0"
          width="1280" height="720"
          allow="accelerometer; autoplay; encrypted-media; gyroscope; picture-in-picture" allowfullscreen></iframe>
      <div class="gdcc-optout-youtube"><h2 style="background-color: forestgreen">YouTube Replacement</h2></div>
  • The same logic applies to HTML elements having a "href" attribute.

      <link data-gdcc-href="https://external-css.com/alternate.css" class="gdcc-optin-extfonts" rel="stylesheet" type="text/css" />
      <script data-gdcc-src="googleanalytics.js" data-gdcc-selector="gdcc-optin-googleanalytics" type="text/javascript"></script>
  • The "type" attribute normally set to "text/css" and "type=text/javascript" should be set to "text/plain" instead. GDCC will take care.

  • Additionally, you can use inline scripts. Therefore, use the "data-gdcc-inline" attribute

      <script data-gdcc-inline data-gdcc-selector="gdcc-optin-googleanalytics" type="text/plain">
          // Insert your code here
      </script>

Missing cookie permissions / i18n

  • When the user clicks on an optout element (e.g. a note that YouTube is currently disabled), it is possible to ask the user, if she/he wants to proceed and enable the corresponding cookie category.

  • In order for this to work, a message shown to the user needs to be configured (optional).

  • The message shown is taken from an attribute data-gdcc-msg, which can be set on the GDCC wrapper or an input/label:

      <div id="gdcc-wrapper" data-gdcc-msg='You must accept cookies in the category "@cookie@" to show this content. Proceed?'>
  • The string "@cookie@" will automatically be replaced by the label of the cookies checkbox.

  • If the user confirms, the cookie will be enabled and the changed setting is saved.

  • A more specific message can be placed on the input or the label itself. If available, it will be taken instead of the general message from the wrapper:

      <input type="checkbox" id="image" data-gdcc-msg='Enable cookie category "@cookie@"?' />
      <label for="image" data-gdcc-msg='Enable cookie category "@cookie@"?'>Images</label>
  • It is possible to set the message on the wrapper only, on an input/label only or both.

Keep your cookies clean

  • If a cookie is activated even only for a short time, it will stay in memory of your browser

  • GDCC is capable of deleting those (first party) cookies, if the category gets disabled again.

  • To make this work, make sure you configure the name of the cookies to be deleted in an optional attribute "data-gdcc-delete-cookie". The attribute accepts one or more cookie names, separated by space or comma:

      <input type="checkbox" id="extfonts" data-gdcc-delete-cookie="cookieNo1, cookieNo2"/>