Skip to content

Commit 541d175

Browse files
authored
fix(zendesk): Button not showing up sometimes (#2184)
Closes #2183
1 parent 88ea094 commit 541d175

File tree

1 file changed

+83
-77
lines changed

1 file changed

+83
-77
lines changed

src/content/zendesk.js

Lines changed: 83 additions & 77 deletions
Original file line numberDiff line numberDiff line change
@@ -7,71 +7,75 @@
77
'use strict';
88

99
// Zendesk new UI Jul 2021
10-
togglbutton.render(
11-
'.omni-conversation-pane>div>div:first-child:not(.toggl)',
12-
{ observe: true },
13-
(elem) => {
14-
const getProject = () => {
15-
const title = document.querySelector('title');
16-
return title ? title.textContent.trim() : '';
17-
};
18-
19-
const getDescription = () => {
20-
const ticketId = document.querySelector('header div[data-selected=true]').attributes['data-entity-id'].value || ''
21-
22-
const input = elem.querySelector('input[aria-label=Subject]');
23-
const title = (input ? input.value : '').trim();
24-
25-
return [`#${ticketId}`, title].filter(Boolean).join(' ');
26-
};
27-
28-
const link = togglbutton.createTimerLink({
29-
buttonType: 'minimal',
30-
className: 'zendesk--2021',
31-
description: getDescription,
32-
projectName: getProject
33-
});
34-
35-
elem.appendChild(link);
36-
}
37-
);
10+
setTimeout(() => {
11+
togglbutton.render(
12+
'.omni-conversation-pane>div>div:first-child:not(.toggl)',
13+
{ observe: true },
14+
(elem) => {
15+
const getProject = () => {
16+
const title = document.querySelector('title');
17+
return title ? title.textContent.trim() : '';
18+
};
19+
20+
const getDescription = () => {
21+
const ticketId = document.querySelector('header div[data-selected=true]').attributes['data-entity-id'].value || ''
22+
23+
const input = elem.querySelector('input[aria-label=Subject]');
24+
const title = (input ? input.value : '').trim();
25+
26+
return [`#${ticketId}`, title].filter(Boolean).join(' ');
27+
};
28+
29+
const link = togglbutton.createTimerLink({
30+
buttonType: 'minimal',
31+
className: 'zendesk--2021',
32+
description: getDescription,
33+
projectName: getProject
34+
});
35+
36+
elem.appendChild(link);
37+
}
38+
);
39+
}, 1000);
3840

3941
// Zendesk pre-2021
40-
togglbutton.render(
41-
'.pane_header:not(.toggl)',
42-
{ observe: true },
43-
function (elem) {
44-
let description;
45-
const projectName = $('title').textContent;
46-
47-
const titleFunc = function () {
48-
const titleElem = $('.editable .ember-view input', elem);
49-
const ticketNum = location.href.match(/tickets\/(\d+)/);
50-
51-
if (titleElem !== null) {
52-
description = titleElem.value.trim();
42+
setTimeout(() => {
43+
togglbutton.render(
44+
'.pane_header:not(.toggl)',
45+
{ observe: true },
46+
function (elem) {
47+
let description;
48+
const projectName = $('title').textContent;
49+
50+
const titleFunc = function () {
51+
const titleElem = $('.editable .ember-view input', elem);
52+
const ticketNum = location.href.match(/tickets\/(\d+)/);
53+
54+
if (titleElem !== null) {
55+
description = titleElem.value.trim();
56+
}
57+
58+
if (ticketNum) {
59+
description = '#' + ticketNum[1].trim() + ' ' + description;
60+
}
61+
return description;
62+
};
63+
64+
const link = togglbutton.createTimerLink({
65+
className: 'zendesk',
66+
description: titleFunc,
67+
projectName: projectName && projectName.split(' - ').shift()
68+
});
69+
70+
// Check for strange duplicate buttons. Don't know why this happens in Zendesk.
71+
if (elem.querySelector('.toggl-button')) {
72+
elem.removeChild(elem.querySelector('.toggl-button'));
5373
}
5474

55-
if (ticketNum) {
56-
description = '#' + ticketNum[1].trim() + ' ' + description;
57-
}
58-
return description;
59-
};
60-
61-
const link = togglbutton.createTimerLink({
62-
className: 'zendesk',
63-
description: titleFunc,
64-
projectName: projectName && projectName.split(' - ').shift()
65-
});
66-
67-
// Check for strange duplicate buttons. Don't know why this happens in Zendesk.
68-
if (elem.querySelector('.toggl-button')) {
69-
elem.removeChild(elem.querySelector('.toggl-button'));
75+
elem.insertBefore(link, elem.querySelector('.btn-group'));
7076
}
71-
72-
elem.insertBefore(link, elem.querySelector('.btn-group'));
73-
}
74-
);
77+
);
78+
}, 1000);
7579

7680
const getDescription = () => {
7781
const ticketNum = location.href.match(/tickets\/(\d+)/);
@@ -86,20 +90,22 @@ const getDescription = () => {
8690
return '#' + id + ' ' + titleElem.value.trim();
8791
};
8892

89-
togglbutton.render(
90-
'[data-test-id="customer-context-tab-navigation"]',
91-
{ observe: true },
92-
function (elem) {
93-
// Manual check for existence in this SPA.
94-
if (elem.querySelector('.toggl-button')) return;
95-
// If we can't get the description on this pass, let's skip and wait for the next one
96-
if (!getDescription()) return;
97-
98-
const link = togglbutton.createTimerLink({
99-
className: 'zendesk-agent-ws',
100-
description: getDescription
101-
});
102-
103-
elem.prepend(link);
104-
}
105-
);
93+
setTimeout(() => {
94+
togglbutton.render(
95+
'[data-test-id="customer-context-tab-navigation"]',
96+
{ observe: true },
97+
function (elem) {
98+
// Manual check for existence in this SPA.
99+
if (elem.querySelector('.toggl-button')) return;
100+
// If we can't get the description on this pass, let's skip and wait for the next one
101+
if (!getDescription()) return;
102+
103+
const link = togglbutton.createTimerLink({
104+
className: 'zendesk-agent-ws',
105+
description: getDescription
106+
});
107+
108+
elem.prepend(link);
109+
}
110+
);
111+
}, 1000);

0 commit comments

Comments
 (0)