我试图做的是控制台记录脚本中未读消息的数量。规则是:如果聊天窗口被最小化,在收到消息时添加一个,当聊天窗口最大化时,将计数设置为零。所有存储为持久化,因此页面重新加载或导航不会更改计数。这是我的剧本。
count = 0;
let is_minimized = true;
var chatServiceOptions = {
license: "*SECRET*", // ie '1234567'
group: 4, // ie 22,
customer: {
name: "Development Account",
timezone: "Europe/London"
},
plugins: [
// this is where the code starts
function countAdd() {
localStorage.count = count++;
},
//this is just calling the API, when a chat is available
function (chatService) {
chatService.register('ready', function() {
// when chat window is closed, ensure is_minimized is true
chatService.events.on('LC_on_chat_window_minimized', data => {
is_minimized = true
});
// when chat window is opened, set is_minimized to false
chatService.events.on('LC_on_chat_window_opened', data => {
count = 0
is_minimized = false
});
chatService.events.on('LC_on_message', function(data) {
// set the window minimized to a variable
var LC_API = LC_API || {};
LC_API.on_chat_window_minimized = function() {
is_minimized = true;
};
// output the data of the incoming/outgoing message
console.log(data);
// check that it comes from an agent and the chat window is minimized
// if it is, add +1 to unread message count
if (data.user_type ==='agent' && is_minimized == true) {
countAdd(); }
// if its maximized, set unread count to zero
else { localStorage.count = 0; };
// display number of unread messages
console.log('unread messages: ' + localStorage.getItem('count'));
});
})
}
],
};
var LC_API = LC_API || {};
LC_API.on_message = function(data) {
console.log(data)
alert("Message " + data.text + " sent by " + data.user_type);
};
</script>控制台给了我:
Uncaught : countAdd未定义为
发布于 2020-02-28 15:16:07
您需要首先定义countAdd和其他函数,然后将它们传递到插件数组中。由于您是内联地定义它们,所以第二个函数不知道countAdd
function countAdd() { }
function otherFn() {
// uses countAdd in here
}
var chatServiceOptions = {
license: "*SECRET*", // ie '1234567'
group: 4, // ie 22,
customer: {
name: "Development Account",
timezone: "Europe/London"
},
// provide both functions to the plugins property
plugins: [countAdd, otherFn]
}https://stackoverflow.com/questions/60454470
复制相似问题