Live Helper Chat support forum.. Old google groups forum can be found here

You are not logged in.

Announcement

#1 2020-12-28 05:30:42

kamioool
New member
Registered: 2020-12-28
Posts: 5

Change theme depend on body class or cookies

I looking for soultion.

I use script and try to make the theme in the LiveHelper script update after clicking the button that is responsible for the dark / light mode switch

So there is my code, i know this code is totaly wrong but i have no idea how to solve it now.


[== JavaScript ==]
    // Life Helper init
    var LHCChatOptionsPage = {'height':300,'mobile':false};
    LHCChatOptionsPage.opt = {};
    (function() {
        var po = document.createElement('script'); po.type = 'text/javascript'; po.async = true;
        var referrer = (document.referrer) ? encodeURIComponent(document.referrer.substr(document.referrer.indexOf('://')+1)) : '';
        var location  = (document.location) ? encodeURIComponent(window.location.href.substring(window.location.protocol.length)) : '';
        if (jQuery("body").hasClass('.dark-mode')) {
            po.src = '//www.site.pl/pomoc/index.php/chat/getstatusembed/(theme)/1/(operator)/1/(leaveamessage)/true?r='+referrer+'&l='+location;
        }
        else {
            po.src = '//www.site.pl/pomoc/index.php/chat/getstatusembed/(operator)/1/(leaveamessage)/true?r='+referrer+'&l='+location;
        }
        var s = document.getElementsByTagName('script')[0]; s.parentNode.insertBefore(po, s);
    })();
    
    
    // Switch dark mode button
    jQuery(document).ready(function(){
        jQuery('.switch').click(function(){
            jQuery(document.body).toggleClass('dark-mode');
            jQuery('.switch').toggleClass('on');
    
        });
    });

Theme argument is /(theme)/<theme_id> so you can just manually write what theme should use. E.g index.php/chat/start/(theme)/2,
but how to update it after script is already initiated?

>>>EDIT:

I modified the code to work with cookies, now wish I had a value added to the "after" variable.[screen][1]

[== JavaScript ==]
    // Switch dark mode button
    
    jQuery(document).ready(function($) {
        $(".switch").click(function() {
            $(".switch").toggleClass("on");
            $("body").toggleClass("dark-mode");
            $.cookie("toggle", $(".switch").hasClass('on'));
        });
    
        if ($.cookie("toggle") == "true") {
            $(".switch").addClass("on");
            $("body").addClass("dark-mode");
        }
    });
    value_or_null = (document.cookie.match(/^(?:.*;)?\s*toggle\s*=\s*([^;]+)(?:.*)?$/)||[,null])[1];
    switch(value_or_null) {
        case true:
            var theme = 1;
            break;
        case false:
            var theme = 3;
            break;
    }
    alert(value_or_null)
        var LHCChatOptionsPage = {'height':300,'mobile':false};
        LHCChatOptionsPage.opt = {};
        (function() {
        var po = document.createElement('script'); po.type = 'text/javascript'; po.async = true;
        var referrer = (document.referrer) ? encodeURIComponent(document.referrer.substr(document.referrer.indexOf('://')+1)) : '';
        var location  = (document.location) ? encodeURIComponent(window.location.href.substring(window.location.protocol.length)) : '';
        po.src = '//www.site.pl/pomoc/index.php/chat/getstatusembed/(hide_offline)/true/(theme)/'+theme+'/(leaveamessage)/true?r='+referrer+'&l='+location;
        var s = document.getElementsByTagName('script')[0]; s.parentNode.insertBefore(po, s);
    
    })();


https://i.stack.imgur.com/QVUfG.png

Last edited by kamioool (2020-12-28 05:31:35)

Offline

#2 2020-12-28 13:56:47

remdex
Administrator
From: Lithuania
Registered: 2012-09-23
Posts: 3,418
Website

Re: Change theme depend on body class or cookies

I would suggest you to use new widget embed code where you can pass theme id by variable just.
https://doc.livehelperchat.com/docs/jav … -arguments

Offline

#3 2020-12-28 15:36:41

kamioool
New member
Registered: 2020-12-28
Posts: 5

Re: Change theme depend on body class or cookies

Hi remdex, thanks for your answer.

I try to initialize the "theme" parameter in new code but it always shows me the default template even though I used "theme: 1"

[== JavaScript ==]

var LHC_API = LHC_API||{};
    LHC_API.args = {mode:'widget',lhc_base_url:'//www.site.pl/pomoc/index.php/',wheight:450,wwidth:350,pheight:520,pwidth:500,leaveamessage:true,theme:1,check_messages:false};
    (function() {
        var po = document.createElement('script'); po.type = 'text/javascript'; po.setAttribute('crossorigin','anonymous'); po.async = true;
        var date = new Date();po.src = '//www.site.pl/pomoc/design/defaulttheme/js/widgetv2/index.js?'+(""+date.getFullYear() + date.getMonth() + date.getDate());
        var s = document.getElementsByTagName('script')[0]; s.parentNode.insertBefore(po, s);
    })();

I have been trying to solve this for several days already. Wrrr! Could you give me an example of how to do this based on the cookies variable? $CookieToggle = True / False

Last edited by kamioool (2020-12-28 15:37:10)

Offline

#4 2020-12-28 15:42:58

remdex
Administrator
From: Lithuania
Registered: 2012-09-23
Posts: 3,418
Website

Re: Change theme depend on body class or cookies

You are mixing old widget javascript with new widget parameters...

https://doc.livehelperchat.com/docs/jav … arguments/

Offline

#5 2020-12-28 15:44:01

remdex
Administrator
From: Lithuania
Registered: 2012-09-23
Posts: 3,418
Website

Re: Change theme depend on body class or cookies

After you change new widget theme reload widget as i showed above

Offline

#6 2020-12-28 15:58:53

kamioool
New member
Registered: 2020-12-28
Posts: 5

Re: Change theme depend on body class or cookies

I downloaded this code from the dashboard:
https://i.imgur.com/PHUsVqP.png

Last edited by kamioool (2020-12-28 17:25:05)

Offline

#7 2020-12-28 16:23:05

remdex
Administrator
From: Lithuania
Registered: 2012-09-23
Posts: 3,418
Website

Re: Change theme depend on body class or cookies

I don't know what you are doing but if you try these codes with different `theme` you get a different widget. One would be with shadow another one without.

[== Undefined ==]
<script>var LHC_API = LHC_API||{};
LHC_API.args = {mode:'widget',lhc_base_url:'//demo.livehelperchat.com/',wheight:450,theme: 1,wwidth:350,pheight:520,pwidth:500,leaveamessage:true,check_messages:false};
(function() {
    var po = document.createElement('script'); po.type = 'text/javascript'; po.setAttribute('crossorigin','anonymous'); po.async = true;
    var date = new Date();po.src = '//demo.livehelperchat.com/design/defaulttheme/js/widgetv2/index.js?'+(""+date.getFullYear() + date.getMonth() + date.getDate());
    var s = document.getElementsByTagName('script')[0]; s.parentNode.insertBefore(po, s);
})();
</script>

Status widget itself can't be reloaded on the fly

[== Undefined ==]
<script>var LHC_API = LHC_API||{};
LHC_API.args = {mode:'widget',lhc_base_url:'//demo.livehelperchat.com/',wheight:450,theme: 2,wwidth:350,pheight:520,pwidth:500,leaveamessage:true,check_messages:false};
(function() {
    var po = document.createElement('script'); po.type = 'text/javascript'; po.setAttribute('crossorigin','anonymous'); po.async = true;
    var date = new Date();po.src = '//demo.livehelperchat.com/design/defaulttheme/js/widgetv2/index.js?'+(""+date.getFullYear() + date.getMonth() + date.getDate());
    var s = document.getElementsByTagName('script')[0]; s.parentNode.insertBefore(po, s);
})();
</script>

But widget content as you see here respects themes.
https://livehelperchat.com/lhcsamples/t … witch.html

So for sure you are missing something smile

Offline

#8 2020-12-28 17:34:30

kamioool
New member
Registered: 2020-12-28
Posts: 5

Re: Change theme depend on body class or cookies

Thanks!

I'm to fool, I found duplicate code elsewhere - it started this event. mad

you're rocking remdex <3, I still have a question
Actually, I am building my website in the localhost environment, LHC is installed on online domain Is it possible to skip CORS somehow so that I can test the code on localhost?

Last edited by kamioool (2020-12-28 17:56:41)

Offline

#9 2020-12-28 17:59:24

remdex
Administrator
From: Lithuania
Registered: 2012-09-23
Posts: 3,418
Website

Re: Change theme depend on body class or cookies

Offline

#10 2020-12-28 18:13:10

kamioool
New member
Registered: 2020-12-28
Posts: 5

Re: Change theme depend on body class or cookies

Best and fastest support ever! Thank you VERY MUCH!

<CLOSE_TOPIC>

Offline

Board footer