-
Notifications
You must be signed in to change notification settings - Fork 0
/
script.js
150 lines (142 loc) · 5.07 KB
/
script.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
const translations = {
optional: 'Optional',
submit: 'Submit',
YourContactEmail: 'Your contact email',
loadMore: 'load more',
thanks: 'Thanks!',
addAttachment: 'Add attachment',
placeholder: 'How can we help?',
contactUsAbout: 'Contact us about',
learnAbout: 'Learn about',
poweredByAtlassian: 'Powered by Atlassian',
};
async function translateOptionalText(reactRoot) {
const elements = reactRoot.querySelectorAll('.optional:not([data-translated="true"])');
for (let i = 0; i < elements.length; i++) {
if (elements[i].textContent === ' (optional)') {
elements[i].textContent = ` (${translations.optional})`;
elements[i].setAttribute('data-translated', 'true');
}
}
}
async function translateSubmitButton(reactRoot) {
const element = reactRoot.querySelector('#submit-button:not([data-translated="true"])');
if (element) {
element.textContent = translations.submit;
element.setAttribute('data-translated', 'true');
}
}
async function translateEmailLabel(reactRoot) {
const element = reactRoot.querySelector('label[for="email"]:not([data-translated="true"])')
if (element) {
element.innerHTML = element.innerHTML.replace(
'Your contact e-mail',
translations.YourContactEmail,
)
element.setAttribute('data-translated', 'true')
}
}
async function translateHowCanWeHelp(reactRoot) {
const element = reactRoot.querySelector('[data-ds--text-field--input="true"]:not([data-translated="true"])');
if (element) {
if (element && element.placeholder === 'How can we help?') {
element.placeholder = translations.placeholder;
element.setAttribute('data-translated', 'true');
}
}
}
async function translateFooter(reactRoot) {
const footerElement = reactRoot.querySelector('.powered-by');
if (footerElement) {
const aref = footerElement.querySelector('a:not([data-translated="true"])');
if (aref) {
aref.textContent = translations.poweredByAtlassian;
aref.setAttribute('data-translated', 'true');
}
}
}
async function translateElements(reactRoot, element) {
// Loop through all h1 elements and change the text if needed
const elements = reactRoot.querySelectorAll(`${element}:not([data-translated="true"])`);
for (let i = 0; i < elements.length; i++) {
if (elements[i].textContent === 'Contact us about') {
elements[i].textContent = translations.contactUsAbout;
elements[i].setAttribute('data-translated', 'true');
}
if (elements[i].textContent === 'Learn about') {
elements[i].textContent = translations.learnAbout;
elements[i].setAttribute('data-translated', 'true');
}
if (elements[i].textContent == 'Thanks!') {
elements[i].textContent = translations.thanks;
elements[i].setAttribute('data-translated', 'true');
}
if (elements[i].textContent === 'Choose file') {
elements[i].textContent = translations.addAttachment;
elements[i].setAttribute('data-translated', 'true');
}
if (elements[i].textContent == 'Load more') {
elements[i].textContent = translations.loadMore;
elements[i].setAttribute('data-translated', 'true');
}
if (elements[i].textContent === 'Choose file') {
elements[i].textContent = translations.addAttachment;
elements[i].setAttribute('data-translated', 'true');
}
if (elements[i].textContent == 'Load more') {
elements[i].textContent = translations.loadMore;
elements[i].setAttribute('data-translated', 'true');
}
}
}
async function translateTexts(reactRoot) {
translateHowCanWeHelp(reactRoot);
translateElements(reactRoot, 'h1');
translateElements(reactRoot, 'h2');
translateElements(reactRoot, 'h3');
translateElements(reactRoot, 'span');
translateFooter(reactRoot);
translateEmailLabel(reactRoot);
translateSubmitButton(reactRoot);
translateOptionalText(reactRoot);
}
async function iframeLoad(iframe) {
// wait for the iframe to load
iframe.onload = () => {
const iframeDoc = iframe.contentDocument || iframe.contentWindow.document;
const reactRoot = iframeDoc.querySelector('#react-root');
// Create a new observer for reactRoot
const observer = new MutationObserver((mutations) => {
mutations.forEach((mutation) => {
if (mutation.type === 'childList') {
translateTexts(reactRoot);
}
});
});
// Init the new observer
observer.observe(reactRoot, { childList: true, subtree: true });
};
}
async function addListenerForIframe() {
const observer = new MutationObserver((mutations) => {
mutations.forEach((mutation) => {
if (mutation.type === 'childList') {
// Check if the element has been added
const element = document.querySelector('#jsd-widget');
if (element) {
iframeLoad(element);
observer.disconnect();
}
}
});
});
observer.observe(document.body, { childList: true, subtree: true });
window.setTimeout(() => {
observer.disconnect();
}, 5000); // Something wierd happends. Lets still clean it up to spead up browser
}
function init() {
if (window.MutationObserver) {
addListenerForIframe();
}
}