百科.dev
全部条目在线工具AI 编程趋势榜开源项目技术资讯提交条目
登录
< 返回工具列表
C

consent-banner-js

> DevOps
开源

一个零依赖、轻量级(约 3kB)、不依赖任何平台的 Cookie 通知栏,可用于任何网站(与 Google 同意模式兼容)。

197 stars0 点赞3 次浏览
访问官网GitHub

工具介绍

一个零依赖、轻量级(约 3kB)、不依赖任何平台的 Cookie 通知栏,可用于任何网站(与 Google 同意模式兼容)。

Consent Banner JS

A zero-dependency, lightweight (~3kB), consent platform agnostic, cookie banner for any website.

How does it work?

  1. It takes JSON configuration that controls display of the banner.
  2. It fires JS callback when user interacts with the banner
  3. It provides simple JS object with consent state

Get started

First, include simple CSS for the banner in the `` of the page:

xml

Then in the footer you can include the actual JS:

xml

Or if you want to load the library script asynchronously, you can use the following consent-banner.ready event listener, which will execute the function only when the library is loaded:

xml

To make that work you need to prepare three things:

  1. A function to load the consent state from somewhere, for instance localStorage (see examples)
  2. A function to do something when the user provides their consent, for instance save it in localStorage (see examples)
  3. A config object that contains complete configuration for the banner content (see examples)

Config Object

…

Consent State Object

Both JS callback functions provided needs to either accept (on save) or return (on load) Consent State Object:

javascript
{
    ad_storage: 'granted',
    analytics_storage: 'denied'
}

Callbacks

javascript
cookiesBannerJs(
    function loadConsentState() {
        const consentState = {}; // get it from somewhere (e.g. localStorage);
        return consentState;
    },
    function saveConsentState(consentState) {
        // do something with consentState, which is basic JS object:
        // {
        //   ad_storage: 'granted',
        //   analytics_storage: 'denied'
        // }
    },
    config
);

Simple Markdown

All description fields in config object support simplified Markdown-like syntax:

  • links
  • bold or bold
  • italic or italic

Styling

This banner comes with mininal set of CSS with all elements prefixed with consent-banner-. Main elements have their IDs:

  • #consent-banner-main - main wrapper element
  • #consent-banner-modal - first view, centered modal or bottom bar
  • #consent-banner-settings - shown when user clicks "settings" button

Buttons can be styles using following CSS selectors:

  • .consent-banner-button - for all buttons
  • .consent-banner-button[href="#accept"] - for approval buttons

Examples

See following examples for complete setups:

  • Bottom bar without "wall"
  • Central modal with "wall"
  • Single JS Bundle
  • Google Consent Mode

Integrations

Instead of doing direct installation in HTML you can use one of the following integrations:

Google Tag Manager

Use this Google Tag Manager Template to quickly configure and deploy the Consent Banner on any GTM enabled website. It obviously integrates with Google Consent Mode.

WordPress

Simple WordPress plugin that provides UI for configuration, injects required files and integrates with Google Consent Mode.

PrestaShop

Simple PrestaShop plugin that provides UI for configuration, injects required files and integrates with Google Consent Mode.

Cloudflare Worker

Example CF Worker code to inject Consent Banner and the configuration without touching HTML code.

Development

We like docker so that's how get local dev server:

docker-compose up -d dev

But first, we need to install dependencies:

docker-compose run dev npm i

And if we need node cli we get into the a shell like that:

docker-compose run --rm dev bash

Finally, to build minified JS file we run:

docker-compose run dev npm run build

Issues· 0 开放

查看全部 Issues在 GitHub 打开

暂无开放 Issues,或尚未同步最近议题。

> 标签

JavaScriptconsentconsent-managementconsent-modeconsent-mode-v2

暂无评论,来聊聊你的看法吧

> 工具信息

发布日期2026年8月1日
最后更新2026年9月18日
分类DevOps
定价开源

> 相关工具

D
Docker
容器化平台,标准化应用交付
G
GitHub Actions
GitHub 原生 CI/CD 工作流
N
Nginx
高性能 Web 服务器与反向代理