-
Notifications
You must be signed in to change notification settings - Fork 1
/
browser.js
279 lines (227 loc) · 11.6 KB
/
browser.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
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
const { getTranslations, isBrowser } = require("./index.js");
const { BRAND, PREV_SCRIPT_VERSION } = require("./utils/configs.js");
const detectRobot = require("./utils/detectRobot.js");
// const { checkPage } = require("./utils/translation/checkPage.js");
if (isBrowser()) {
window.translationScriptTag = document.currentScript;
window.translationScriptPrevVersion = PREV_SCRIPT_VERSION // called prev version because it will bumped after published to npm
// REQUIRED ATTRIBUTES
const DATA_API_KEY = `data-${BRAND}-key`
const DATA_ORIGINAL_LANG = "data-original-language";
const DATA_ALLOWED_LANGUAGES = "data-allowed-languages";
const apiKey = window.translationScriptTag.getAttribute(DATA_API_KEY);
// COMMON OPTIONAL ATTRIBUTES
const DATA_USE_BROWSER_LANG = "data-use-browser-language"; // default: true
const DATA_EXCLUDE_CLASSES = "data-exclude-classes";
const DATA_EXCLUDE_IDS = "data-exclude-ids";
const DATA_REPLACE_LINKS = "data-replace-links"; // default: true
const DATA_AUTO_CREATE_SELECTOR = "data-auto-create-selector"; // default: true
const DATA_DELAY = "data-timeout"; // default: 0
const DATA_DYNAMIC_TRANSLATION = "data-dynamic-translation"; // default: true
const DATA_TRANSLATE_ATTR = "data-translate-attributes"; // default: false
// ADVANCED OPTIONAL ATTRIBUTES
const DATA_LANG_PARAMETER = "data-lang-parameter"; // default: "lang"
const DATA_CUSTOM_LANG_CODE = "data-custom-language-code"; // format: "kk=kz, en=us, ru=rs"
const DATA_MERGE_INLINE = "data-translate-splitted-text"; // default: false
const DATA_EXCLUDE_CONTENTS = "data-exclude-contents"; // format: "{{content1}}, {{content2}}"
const DATA_DEBOUNCE = "data-debounce" // format: "2000"
const DATA_TRANSLATE_FORM_PLACEHOLDER = "data-translate-form-placeholder"
const DATA_TRANSLATE_SELECT_OPTIONS = "data-translate-select-options"
// FEATURE: Prevent Google Translate from translating the page
// Set the 'translate' attribute of the HTML tag to 'no'
document.documentElement.setAttribute('translate', 'no');
// Create a new meta element
const meta = document.createElement('meta');
meta.name = 'google';
meta.content = 'notranslate';
// Append the meta element to the head of the document
document.head.appendChild(meta);
// FEATURE: Get text inside brackets
const excludeContentsRegex = /{{((?:[^}]|\\})*)}}/g;
function getTextInsideBrackets(str) {
let match;
let matches = [];
while ((match = excludeContentsRegex.exec(str)) !== null) {
// Replace escaped closing brackets with a single closing bracket
let cleanedMatch = match[1].replace(/\\\\}/g, '}');
cleanedMatch = cleanedMatch.replace(/\\\\{/g, '{');
matches.push(cleanedMatch);
}
return matches;
}
const langParam = window.translationScriptTag.getAttribute(DATA_LANG_PARAMETER) || "lang";
const search = window.location.search;
const params = new URLSearchParams(search);
const paramsLang = params.get(langParam);
window.paramsLang = paramsLang;
const paramsUpdateTranslation = params.get('globalseo_update_translation');
// defined languages so dont need extra fetch
const originalLangAttr = window.translationScriptTag.getAttribute(DATA_ORIGINAL_LANG) || window.translationScriptTag.getAttribute("data-original-lanugage");
const originalLang = (originalLangAttr || "").trim().toLowerCase();
const allowedLangAttr = window.translationScriptTag.getAttribute(DATA_ALLOWED_LANGUAGES);
const allowedLangs = (allowedLangAttr || "").trim().toLowerCase().split(",").filter(lang => lang && lang.trim() != originalLang).map(lang => lang.trim());
const activeLang = window.globalseoActiveLang || paramsLang || originalLang;
if (document.documentElement.lang != activeLang) {
document.documentElement.lang = activeLang;
}
function handleLinkTags() {
// FEATURE: Create a canonical link tag for translated pages
// e.g. https://example.com/path?lang=es
// Cleanup the original canonical link tag
const existingCanonicalLinkTag = document.querySelector('link[rel="canonical"]');
if (existingCanonicalLinkTag) {
existingCanonicalLinkTag.remove();
}
const newCanonicalLinkTag = document.createElement('link');
if (!paramsLang || paramsLang == originalLang) {
newCanonicalLinkTag.href = window.location.pathname;
} else {
newCanonicalLinkTag.href = `${window.location.protocol}//${window.location.host}${window.location.pathname}?${langParam}=${paramsLang}`;
}
newCanonicalLinkTag.setAttribute('rel', 'canonical');
document.head.appendChild(newCanonicalLinkTag);
// Add alternate link tag for original languages
const alternateLinkTag = document.createElement('link');
alternateLinkTag.setAttribute('rel', 'alternate');
alternateLinkTag.setAttribute('hreflang', originalLang);
alternateLinkTag.href = window.location.pathname;
document.head.appendChild(alternateLinkTag);
// Add alternate link tags for allowed languages
for (let lang of allowedLangs) {
const alternateLinkTag = document.createElement('link');
// Create a new URL object
let url = new URL(window.location.href);
// Set the search parameter "lang" to lang
url.searchParams.set(langParam, lang);
alternateLinkTag.setAttribute('rel', 'alternate');
alternateLinkTag.setAttribute('hreflang', lang);
alternateLinkTag.href = `${window.location.protocol}//${window.location.host}${window.location.pathname}?${langParam}=${lang}`;
document.head.appendChild(alternateLinkTag);
}
}
handleLinkTags();
// console.log(process.env.NO_CACHE)
function replaceLinks(lang) {
// Select all anchor tags
let anchors = document.querySelectorAll('a');
// Loop through all anchor tags
for (let i = 0; i < anchors.length; i++) {
let anchor = anchors[i];
// Check if the link is internal and does not contain a hash
if ((anchor.hostname === window.location.hostname) && anchor.href !== `${window.location.href}#`) {
// Create a new URL object
let url = new URL(anchor.href);
// Set the search parameter "lang" to lang
url.searchParams.set(langParam, lang);
// Update the href of the anchor tag
anchor.href = url.href;
}
}
}
// // check if the page works without the trailing slash
// checkPage();
try {
// get the current date
const now = new Date();
// get the current date timestamp
const nowTimestamp = now.getTime();
// get existing expiration timestamp
const globalseoExpirationTimestamp = window.localStorage.getItem("globalseoExpirationTimestamp");
const expiration = Number(globalseoExpirationTimestamp);
if (!isNaN(expiration) && expiration < nowTimestamp) {
window.localStorage.removeItem("globalseoExpirationTimestamp");
window.localStorage.removeItem("translationCachePerPage");
}
} catch (e) {
console.log("Error checking expiration", e)
}
const userAgent = window.navigator.userAgent;
const isRobot = detectRobot(userAgent);
if (!isRobot) {
const translationCache = window.localStorage.getItem("translationCachePerPage");
try {
const parsedTranslationCache = JSON.parse(translationCache);
if (parsedTranslationCache && typeof parsedTranslationCache === "object") {
window.translationCache = parsedTranslationCache;
}
} catch (e) {
console.log("Error parsing translation cache", e)
}
} else {
window.translationCache = {};
}
const customLanguageCodeAttr = window.translationScriptTag.getAttribute(DATA_CUSTOM_LANG_CODE); // format is "kk=kz, en=us, ru=rs"
const customLanguageCode = customLanguageCodeAttr ? customLanguageCodeAttr.split(",").reduce((acc, lang) => {
const [key, value] = lang.trim().split("=");
if (!key || !value) return acc;
acc[key] = value;
return acc;
}, {}) : {};
// this is backward compatibility for use browser language
const disableAutoTranslateAttr = window.translationScriptTag.getAttribute("data-disable-auto-translate");
const disableAutoTranslate = disableAutoTranslateAttr == "true";
const useBrowserLanguageAttr = window.translationScriptTag.getAttribute(DATA_USE_BROWSER_LANG);
const useBrowserLanguage = useBrowserLanguageAttr != "false" && useBrowserLanguageAttr != false;
// exclude classes
const excludeClassesAttr = (window.translationScriptTag.getAttribute(DATA_EXCLUDE_CLASSES) || "").trim()
const excludeClassesByComma = excludeClassesAttr.split(",").filter(className => !!className).map(className => className.trim());
const excludeClassesBySpace = excludeClassesAttr.split(" ").filter(className => !!className).map(className => className.trim().replaceAll(",", ""));
const mergedExcludeClasses = [...excludeClassesByComma, ...excludeClassesBySpace];
const excludeClasses = [...new Set(mergedExcludeClasses)]; // get unique values
// exclude ids
const excludeIdsAttr = (window.translationScriptTag.getAttribute(DATA_EXCLUDE_IDS) || "").trim()
const excludeIdsByComma = excludeIdsAttr.split(",").filter(id => !!id).map(id => id.trim());
const excludeIdsBySpace = excludeIdsAttr.split(" ").filter(id => !!id).map(id => id.trim().replaceAll(",", ""));
const mergedExcludeIds = [...excludeIdsByComma, ...excludeIdsBySpace];
const excludeIds = [...new Set(mergedExcludeIds)]; // get unique values
// exclude contents
const excludeContentsAttr = (window.translationScriptTag.getAttribute(DATA_EXCLUDE_CONTENTS) || "").trim()
const splittedContents = getTextInsideBrackets(excludeContentsAttr);
const excludeContents = [...new Set(splittedContents)]; // get unique values
// timeout
const timeoutAttr = window.translationScriptTag.getAttribute(DATA_DELAY);
const timeout = isNaN(Number(timeoutAttr)) ? 0 : Number(timeoutAttr);
const createSelector = window.translationScriptTag.getAttribute(DATA_AUTO_CREATE_SELECTOR) != "false";
const translateAttributes = window.translationScriptTag.getAttribute(DATA_TRANSLATE_ATTR) == "true";
const dynamicTranslation = paramsUpdateTranslation == "true" || (window.translationScriptTag.getAttribute(DATA_DYNAMIC_TRANSLATION) != "false");
const translateSplittedText = window.translationScriptTag.getAttribute(DATA_MERGE_INLINE) == "true";
const shouldReplaceLinks = window.translationScriptTag.getAttribute(DATA_REPLACE_LINKS) != "false";
const debounceDuration = window.translationScriptTag.getAttribute(DATA_DEBOUNCE);
const translateFormPlaceholder = window.translationScriptTag.getAttribute(DATA_TRANSLATE_FORM_PLACEHOLDER) == "true";
const translateSelectOptions = window.translationScriptTag.getAttribute(DATA_TRANSLATE_SELECT_OPTIONS) == "true";
const options = {
useBrowserLanguage: !disableAutoTranslate && useBrowserLanguage,
createSelector: createSelector,
excludeClasses,
excludeIds,
excludeContents,
originalLanguage: originalLang,
allowedLanguages: allowedLangs,
timeout: timeout,
customLanguageCode,
translateAttributes,
dynamicTranslation,
translateSplittedText: false,
langParam,
debounceDuration,
translateFormPlaceholder,
translateSelectOptions
}
function initTranslation() {
// replace links with lang (for SEO purposes)
if (shouldReplaceLinks && paramsLang && (paramsLang != originalLang)) {
replaceLinks(paramsLang);
}
getTranslations(apiKey, options)
}
// console.log("document.readyState", document.readyState)
if (document.readyState == 'interactive' || document.readyState == 'complete') {
// DOM is already loaded, run the code
initTranslation();
} else {
// DOM is not loaded yet, wait for it
document.addEventListener("DOMContentLoaded", function() {
initTranslation();
});
}
}