-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathlogo-lang.js
More file actions
151 lines (134 loc) · 3.48 KB
/
logo-lang.js
File metadata and controls
151 lines (134 loc) · 3.48 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
(function () {
var koPrefix = "/ko";
var defaultHome = "/";
function homeForPath(pathname) {
if (pathname === koPrefix || pathname.indexOf(koPrefix + "/") === 0) {
return koPrefix;
}
return defaultHome;
}
function logoSelectors(anchorOnly) {
if (anchorOnly) {
return [
"a[data-testid=\"nav-logo\"]",
"a.nav-logo",
"a#nav-logo",
"a[aria-label=\"Home\"]",
"a[aria-label=\"Homepage\"]"
];
}
return [
"[data-testid=\"nav-logo\"]",
".nav-logo",
"#nav-logo",
"[aria-label=\"Home\"]",
"[aria-label=\"Homepage\"]"
];
}
function findLogoLink(root) {
var selectors = logoSelectors(true);
var scope = root || document;
if (scope instanceof Element) {
for (var j = 0; j < selectors.length; j += 1) {
var closest = scope.closest(selectors[j]);
if (closest) {
return closest;
}
}
}
for (var i = 0; i < selectors.length; i += 1) {
var el = scope.querySelector(selectors[i]);
if (!el) {
continue;
}
if (el.tagName === "A") {
return el;
}
var link = el.closest("a");
if (link) {
return link;
}
}
var imgLogo = scope.querySelector("img[alt*=\"logo\" i]");
if (imgLogo) {
return imgLogo.closest("a");
}
return null;
}
function isLogoTarget(target) {
if (!(target instanceof Element)) {
return false;
}
var selectors = logoSelectors(false);
for (var i = 0; i < selectors.length; i += 1) {
if (target.closest(selectors[i])) {
return true;
}
}
return false;
}
function updateLogoHref() {
var link = findLogoLink();
if (!link) {
return;
}
var target = homeForPath(window.location.pathname);
if (link.getAttribute("href") !== target) {
link.setAttribute("href", target);
}
}
function scheduleUpdate() {
updateLogoHref();
window.setTimeout(updateLogoHref, 50);
window.setTimeout(updateLogoHref, 250);
}
function wireLocationChange() {
var notify = function () {
window.dispatchEvent(new Event("locationchange"));
};
var pushState = history.pushState;
history.pushState = function () {
var result = pushState.apply(this, arguments);
notify();
return result;
};
var replaceState = history.replaceState;
history.replaceState = function () {
var result = replaceState.apply(this, arguments);
notify();
return result;
};
window.addEventListener("popstate", notify);
}
function wireLogoClick() {
document.addEventListener(
"click",
function (event) {
var target = event.target;
if (!target || typeof target.closest !== "function") {
return;
}
if (!isLogoTarget(target)) {
return;
}
var destination = homeForPath(window.location.pathname);
event.preventDefault();
event.stopPropagation();
window.location.assign(destination);
},
true
);
}
if (document.readyState === "loading") {
document.addEventListener("DOMContentLoaded", scheduleUpdate);
} else {
scheduleUpdate();
}
wireLocationChange();
wireLogoClick();
window.addEventListener("locationchange", scheduleUpdate);
var observer = new MutationObserver(function () {
updateLogoHref();
});
observer.observe(document.documentElement, { childList: true, subtree: true });
})();