在Zoho聊天小部件中使用JavaScript变量?

发布于 2025-02-04 09:01:27 字数 2393 浏览 5 评论 0原文

我已经做了几天的工作,几乎没有取得进步,因为我对JavaScript很烂。

目标:创建一个WP插件,该插件允许为每个网页选择替代聊天部门,或为域使用默认的聊天部门。我可以在插件中使用PHP来完成所有这些,但是...我所有的WordPress网站都使用Zoho Salesiq插件。我无法禁用此插件,因此我必须编辑插件设置中的聊天窗口小部件JavaScript,因此没有PHP。 y

许多研究都使我找到了一个潜在的解决方案 - 我将自定义元标记插入了头部,这些标签将保存正确的数据以插入JavaScript。但这就是我迷路的地方 - 如何用自定义元标记的内容替换Zoho JavaScript中的这两个值?

<meta name='my_zoho_widgetcode' content='123456789' />
<meta name='my_zoho_deptcode' content='deptA' />

<script id="zsiqchat">
    var $zoho=$zoho || {};
    $zoho.salesiq = $zoho.salesiq || {
        widgetcode: "GET-WIDGETCODE-FROM-META-TAG", 
        values:{},
        ready:function(){}
    };
    var d=document;
    s=d.createElement("script");
    s.type="text/javascript";
    s.id="zsiqscript";
    s.defer=true;
    s.src="https://salesiq.zoho.com/widget?plugin_source=wordpress";
    t=d.getElementsByTagName("script")[0];
    t.parentNode.insertBefore(s,t);
    $zoho.salesiq.ready=function() {
        $zoho.salesiq.chat.department("GET-DEPT-FROM-META-TAG"); 
    };
</script>

我设法使它以这种方式工作,但是如果条件缩短)需要很多条件,所以我正在寻找一个更清洁的解决方案。

<script type="text/javascript" id="zsiqchat">
    var zoho_meta_widgetcode = document.querySelector('meta[name="zoho_widgetcode"]').content;
    var zoho_meta_deptcode = document.querySelector('meta[name="zoho_deptcode"]').content;
    var $zoho=$zoho || {};
    if (zoho_meta_widgetcode === '123456789') {
        $zoho.salesiq = $zoho.salesiq || {
        widgetcode:"123456789",
        values:{},ready:function(){}
        };
    }
    if (zoho_meta_widgetcode === '') {
        $zoho.salesiq = $zoho.salesiq || {
        widgetcode:"000000000",
        values:{},ready:function(){}
        };
    }
    var d=document;
    s=d.createElement("script");
    s.type="text/javascript";
    s.id="zsiqscript";
    s.defer=true;
    s.src="https://salesiq.zoho.com/widget?plugin_source=wordpress";
    t=d.getElementsByTagName("script")[0];
    t.parentNode.insertBefore(s,t);
    $zoho.salesiq.ready=function()
    {
        if (zoho_meta_deptcode === 'deptA') {
            $zoho.salesiq.chat.department(["deptA"]); 
        }
        if (zoho_meta_deptcode === '') {
            $zoho.salesiq.chat.department(["Default"]); 
        }
    };
</script>

更新:我了解到Salesiq WP插件将使我可以对代码进行一些更改,而不是其他更改。这给我带来了很多悲伤,我最终通过插件禁用了该插件并插入了自己的代码。

I've worked on this for days and made little progress because I suck at javascript.

Goal: Create a wp plugin that allows choosing an alternate chat department for each web page, or use the default chat dept for the domain. I can do all of this with php in my plugin, BUT... all of my Wordpress websites are using Zoho SalesIQ plugin. I cannot disable this plugin, so instead I must edit the chat widget javascript that's in the plugin settings, so NO PHP. Yuck.

Much research lead me to a potential solution - I've inserted custom meta tags into the head that will hold the correct data to insert into the javascript. But that's where I get lost - how do I replace these two values in the zoho javascript with the content from the custom meta tags?

<meta name='my_zoho_widgetcode' content='123456789' />
<meta name='my_zoho_deptcode' content='deptA' />

<script id="zsiqchat">
    var $zoho=$zoho || {};
    $zoho.salesiq = $zoho.salesiq || {
        widgetcode: "GET-WIDGETCODE-FROM-META-TAG", 
        values:{},
        ready:function(){}
    };
    var d=document;
    s=d.createElement("script");
    s.type="text/javascript";
    s.id="zsiqscript";
    s.defer=true;
    s.src="https://salesiq.zoho.com/widget?plugin_source=wordpress";
    t=d.getElementsByTagName("script")[0];
    t.parentNode.insertBefore(s,t);
    $zoho.salesiq.ready=function() {
        $zoho.salesiq.chat.department("GET-DEPT-FROM-META-TAG"); 
    };
</script>

I managed to get it to work this way, but it's going to require a lot of IF conditions (shortened here), so I'm looking for a cleaner solution.

<script type="text/javascript" id="zsiqchat">
    var zoho_meta_widgetcode = document.querySelector('meta[name="zoho_widgetcode"]').content;
    var zoho_meta_deptcode = document.querySelector('meta[name="zoho_deptcode"]').content;
    var $zoho=$zoho || {};
    if (zoho_meta_widgetcode === '123456789') {
        $zoho.salesiq = $zoho.salesiq || {
        widgetcode:"123456789",
        values:{},ready:function(){}
        };
    }
    if (zoho_meta_widgetcode === '') {
        $zoho.salesiq = $zoho.salesiq || {
        widgetcode:"000000000",
        values:{},ready:function(){}
        };
    }
    var d=document;
    s=d.createElement("script");
    s.type="text/javascript";
    s.id="zsiqscript";
    s.defer=true;
    s.src="https://salesiq.zoho.com/widget?plugin_source=wordpress";
    t=d.getElementsByTagName("script")[0];
    t.parentNode.insertBefore(s,t);
    $zoho.salesiq.ready=function()
    {
        if (zoho_meta_deptcode === 'deptA') {
            $zoho.salesiq.chat.department(["deptA"]); 
        }
        if (zoho_meta_deptcode === '') {
            $zoho.salesiq.chat.department(["Default"]); 
        }
    };
</script>

UPDATE: I've learned that the SalesIQ WP plugin will allow me to make some changes to the code but not other changes. This caused me so much grief I ended up disabling that plugin and inserting my own code via my plugin.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文