在Zoho聊天小部件中使用JavaScript变量?
我已经做了几天的工作,几乎没有取得进步,因为我对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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论