orestbida / cookieconsent

:cookie: Simple cross-browser cookie-consent plugin written in vanilla js
https://playground.cookieconsent.orestbida.com/
MIT License
4k stars 410 forks source link

Can the configuration of cookieconsent be accessed ? #80

Closed ghost closed 3 years ago

ghost commented 3 years ago

Is possible to access the information set to configure the cc.run({ from onAccept function?

cc.run({
    delay : 0,
    autorun : true,     
    current_lang : 'en',
    force_consent : true,
    cookie_expiration : 365,
    languages : {
        en : {  
            consent_modal : {
                title :  "TITLE",
                description :  'DESCRIPTION',
              ......

Something like:

onAccept: function(cookies){ 
 my_cookie_consent.languages.en.(....)
orestbida commented 3 years ago

Unfortunately no ... what would you like to achieve?

ghost commented 3 years ago

Access the items under settings_modal to be able to access the details introduced in the cookie_tables for each category from the method onAccept/onChange

A (crap) way to achieve it as it is now, is with document.getElementById('.....') and getting the IDs cookieconsent uses when it generates the table in L261: var _createCookieConsentHTML = function(never_accepted, conf_params)

Does it make sense to add a method to cookieconsent that return conf_params?

Something like cookieconsent.myConfig()

orestbida commented 3 years ago

If you simply want to know the content of the modals, you could initialize the plugin using an external object like so:


let languages = {
    en: {
        consent_modal: {
            title :  "I use cookies",
            description :  'Your cookie consent message here',
            primary_btn: {
                text: 'Accept',
                role: 'accept_all'
            },
            secondary_btn: {
                text : 'Reject',
                role : 'accept_necessary' 
            }
        },
        settings_modal : {
          ...
        }
    }
};

const cc  = initCookieConsent();

cc.run({
    autorun: true,
    ....,

    onAccept: function(){
        console.log(`title=${languages.en.consent_modal.title}`);
    },
    languages: languages
});

If your intention was to be able to modify the data of the modals (modify the html markup inside them), this can't be done. You'll need to manually obtain references to existing elements using methods like document.getElmentById.