forked from adobecom/dc
-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathhead.html
More file actions
175 lines (152 loc) · 7.71 KB
/
Copy pathhead.html
File metadata and controls
175 lines (152 loc) · 7.71 KB
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
<meta name="viewport" content="width=device-width, initial-scale=1" />
<link rel="preconnect" href="https://acrobat.adobe.com" crossorigin>
<script src="/acrobat/scripts/scripts.js" type="module"></script>
<style>body { display: none; }</style>
<link rel="icon" href="data:,">
<script nomodule src="/acrobat/scripts/fallback.js"></script>
<script>
window.setLibs = (prodLibs, location = window.location) => {
const { hostname, search } = location;
if (!['.aem.', '.hlx.', '.stage.', 'local', '.da.'].some((i) => hostname.includes(i))) return prodLibs;
const branch = new URLSearchParams(search).get('milolibs') || 'main';
if (!/^[a-zA-Z0-9_-]+$/.test(branch)) throw new Error('Invalid branch name.');
if (branch === 'main' && hostname === 'www.stage.adobe.com') return '/libs';
if (branch === 'local') return 'http://localhost:6456/libs';
const env = hostname.includes('.hlx.') ? 'hlx' : 'aem';
return `https://${branch}${branch.includes('--') ? '' : '--milo--adobecom'}.${env}.live/libs`;
};
window.getUnityLibs = (prodLibs = '/unitylibs') => {
const { hostname, search } = window.location;
if (!['.aem.', '.hlx.', '.stage.', 'local', '.da.'].some((i) => hostname.includes(i))) return prodLibs;
const branch = new URLSearchParams(search).get('unitylibs') || 'main';
if (!/^[a-zA-Z0-9_-]+$/.test(branch)) throw new Error('Invalid branch name.');
if (branch === 'main' && hostname === 'www.stage.adobe.com') return prodLibs;
const env = hostname.includes('.hlx.') ? 'hlx' : 'aem';
return `https://${branch}${branch.includes('--')? '' : '--unity--adobecom'}.${env}.live/unitylibs`;
}
window.extractLocalePrefix = (pathname = window.location.pathname) => {
const segments = pathname.split('/').filter(Boolean);
if (!segments[0] || segments[0] === 'acrobat') {
return '';
}
return `/${segments[0]}`;
}
window.loadLink = (href, options = {}) => {
const { as, callback, crossorigin, rel, fetchpriority } = options;
let link = document.head.querySelector(`link[href="${href}"]`);
if (!link) {
link = document.createElement('link');
link.setAttribute('rel', rel);
if (as) link.setAttribute('as', as);
if (crossorigin) link.setAttribute('crossorigin', crossorigin);
if (fetchpriority) link.setAttribute('fetchpriority', fetchpriority);
link.setAttribute('href', href);
if (callback) {
link.onload = (e) => callback(e.type);
link.onerror = (e) => callback(e.type);
}
document.head.appendChild(link);
} else if (callback) {
callback('noop');
}
return link;
}
const miloLibs = setLibs('/libs');
const unityLibs = getUnityLibs();
const localePrefix = extractLocalePrefix();
const preloads = document.querySelector('meta[name="preloads"]')?.content?.split(',').map(x => x.trim().replace('$MILOLIBS', miloLibs).replace('$UNITYLIBS', unityLibs).replace('$LOCALEPREFIX', localePrefix)) || [];
preloads.forEach((link) => {
if (link.endsWith('.js')) {
loadLink(link, { as: 'script', rel: 'preload', crossorigin: 'anonymous' });
} else if (link.endsWith('.css')) {
loadLink(link, { as: 'style', rel: 'preload' });
} else if (/\.(json|html|svg)$/.test(link)) {
loadLink(link, { as: 'fetch', rel: 'preload', crossorigin: 'anonymous' });
} else if (/\.(png|jpg|jpeg|gif|webp)$/.test(link)) {
loadLink(link, { as: 'image', rel: 'preload', fetchpriority: 'high' });
}
});
const userAgentMeta = document.querySelector('meta[name="hreflinksuseragents"]');
const allowedAgents = userAgentMeta && userAgentMeta.content.split(',');
const userAgentString = window.navigator.userAgent;
const isAllowedAgent = allowedAgents && allowedAgents.some((agent) => userAgentString.includes(agent.trim()));
const ssrFlag = document.querySelector('meta[name="head-loaded"]');
const LOCALES = ['ae_ar', 'ae_en', 'africa', 'ar', 'at', 'au', 'be_en', 'be_fr', 'be_nl', 'bg', 'br', 'ca_fr', 'ca', 'ch_de', 'ch_fr', 'ch_it', 'cl', 'cn', 'co', 'cr', 'cy_en', 'cz', 'de', 'dk', 'ec', 'ee', 'eg_ar', 'eg_en', 'el', 'es', 'fi', 'fr', 'gr_el', 'gr_en', 'gt', 'hk_en', 'hk_zh', 'hu', 'id_en', 'id_id', 'ie', 'il_en', 'il_he', 'in_hi', 'in', 'it', 'jp', 'kr', 'kw_ar', 'kw_en', 'la', 'lt', 'lu_de', 'lu_en', 'lu_fr', 'lv', 'mena_ar', 'mena_en', 'mt', 'mx', 'my_en', 'my_ms', 'ng', 'nl', 'no', 'nz', 'pe', 'ph_en', 'ph_fil', 'pl', 'pr', 'pt', 'qa_ar', 'qa_en', 'ro', 'ru', 'sa_ar', 'sa_en', 'se', 'sg', 'si', 'sk', 'th_en', 'th_th', 'tr', 'tw', 'ua', 'uk', 'vn_en', 'vn_vi', 'za'];
(() => {
function buildLink(language, url) {
const link = document.createElement('link');
link.setAttribute('rel', 'alternate');
link.setAttribute('hreflang', language);
link.setAttribute('href', url);
return link;
}
async function fetchAndParseSitemap() {
const sitemapOrigin = 'https://www.adobe.com';
const { origin, pathname } = window.location;
let sitemapPath = '/dc-shared/assets/sitemap.xml';
const localeMatch = LOCALES.find(locale => pathname.startsWith(`/${locale}/`));
if (localeMatch) {
sitemapPath = `/${localeMatch}/dc-shared/assets/sitemap.xml`;
}
try {
const controller = new AbortController();
const timeoutId = setTimeout(() => controller.abort(), 5000);
let response;
try {
response = await fetch(`${origin}${sitemapPath}`, { signal: controller.signal });
} finally {
clearTimeout(timeoutId);
}
if (!response.ok) {
console.warn('Failed to fetch sitemap:', response.status);
return;
}
const xmlText = await response.text();
const parser = new DOMParser();
const xmlDoc = parser.parseFromString(xmlText, 'text/xml');
const parseError = xmlDoc.querySelector('parsererror');
if (parseError) {
return;
}
const urlElements = xmlDoc.querySelectorAll('url');
let currentUrlElement = null;
const correctedPath = pathname.endsWith('.html') ? pathname : `${pathname}.html`;
const isLocalRoot = localeMatch && `${sitemapOrigin}/${localeMatch}/` === `${sitemapOrigin}${pathname}`;
const rootPage = isLocalRoot ? `${sitemapOrigin}/${localeMatch}/` : `${sitemapOrigin}/`;
const currentPageUrl = pathname !== '/' && pathname !== `/${localeMatch}/` ? `${sitemapOrigin}${correctedPath}` : `${rootPage}`;
for (const urlElement of urlElements) {
const loc = urlElement.querySelector('loc')?.textContent;
if (loc === currentPageUrl || loc === currentPageUrl.replace(/\/$/, '')) {
currentUrlElement = urlElement;
break;
}
}
if (!currentUrlElement) {
console.warn('Current page not found in sitemap');
return;
}
const alternateLinks = currentUrlElement.querySelectorAll('link[rel="alternate"]');
const linkElements = [];
alternateLinks.forEach(altLink => {
const hreflang = altLink.getAttribute('hreflang');
const href = altLink.getAttribute('href');
if (hreflang && href) {
linkElements.push(buildLink(hreflang, href));
}
});
const titleElement = document.head.querySelector('title');
if (linkElements.length > 0 && titleElement) {
titleElement.after(...linkElements);
}
} catch (error) {
console.error('Error fetching or parsing sitemap:', error);
}
}
if (isAllowedAgent && !ssrFlag) {
fetchAndParseSitemap();
}
})();
const meta = document.createElement('meta');
meta.setAttribute('name', 'head-loaded');
document.head.append(meta);
</script>