-
Notifications
You must be signed in to change notification settings - Fork 0
/
dropdown.js
250 lines (218 loc) · 7.82 KB
/
dropdown.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
const BUTTON_COLOR = "#6c757d";
const BUTTON_HOVER_COLOR = "#5a6268";
function loadDataScript(callback) {
var script = document.createElement('script');
script.src = 'https://htmlspecs.com/data.js';
script.onload = callback;
document.head.appendChild(script);
}
function createLink(href, text) {
var a = document.createElement("a");
a.href = href;
a.textContent = text;
a.title = text;
a.style.cssText = `
display: block;
padding: 10px 15px;
color: #333;
text-decoration: none;
transition: color 0.3s;
overflow: hidden;
white-space: nowrap;
text-overflow: ellipsis;
width: 100%;
box-sizing: border-box;
`;
a.onmouseover = () => a.style.color = "#007BFF";
a.onmouseout = () => a.style.color = "#333";
return a;
}
function createButton(id, text, onClick) {
var button = document.createElement("button");
button.id = id;
button.textContent = text;
setButtonStyle(button);
button.onclick = onClick;
return button;
}
function setButtonStyle(button) {
button.style.cssText = `
display: inline-block;
z-index: 3;
margin-left: 10px;
background-color: ${BUTTON_COLOR};
color: white;
padding: 10px 15px;
font-size: 14px;
border: none;
cursor: pointer;
border-radius: 4px;
box-shadow: 0 2px 6px rgba(0, 0, 0, 0.2);
transition: background-color 0.3s;
`;
}
loadDataScript(function () {
var githubButton = createLink(
window.location.hostname === 'ecma262.com' ?
"https://github.com/JinDX/ecma262.com" :
"https://github.com/JinDX/htmlspecs.com",
""
);
githubButton.style.cssText += `
display: inline-block;
width: 40px;
height: 40px;
background-image: url('https://github.githubassets.com/images/modules/logos_page/GitHub-Mark.png');
background-size: cover;
border-radius: 50%;
margin-right: 10px;
box-shadow: 0 2px 6px rgba(0, 0, 0, 0.2);
`;
githubButton.title = "查看 GitHub 源码,加星 ⭐";
githubButton.onmouseover = () => {
githubButton.style.transform = "scale(1.2)";
githubButton.style.boxShadow = "0 4px 12px rgba(0, 0, 0, 0.4)";
};
githubButton.onmouseout = () => {
githubButton.style.transform = "scale(1)";
githubButton.style.boxShadow = "0 2px 6px rgba(0, 0, 0, 0.2)";
};
var dropdownButton = createButton("dropdownButton", "其他标准", function () {
dropdownContent.style.display = dropdownContent.style.display === "block" ? "none" : "block";
});
var buttonContainer = document.createElement("div");
buttonContainer.style.cssText = `
position: absolute;
top: 20px;
right: 20px;
display: flex;
align-items: center;
`;
buttonContainer.appendChild(githubButton);
buttonContainer.appendChild(dropdownButton);
document.body.appendChild(buttonContainer);
var dropdownContent = document.createElement("div");
dropdownContent.id = "dropdownContent";
dropdownContent.style.cssText = `
display: none;
position: absolute;
top: 60px;
right: 20px;
width: 405px;
height: 80%;
max-height: 80vh;
background-color: white;
border: 1px solid #ddd;
border-radius: 4px;
box-shadow: 0 4px 8px rgba(0, 0, 0, 0.1);
z-index: 1000;
overflow-y: auto;
`;
document.body.appendChild(dropdownContent);
links.forEach(function (link) {
dropdownContent.appendChild(createLink(link.href, link.text));
if (link.text === "ECMA-262") {
var cssTitle = createLink("#", "CSS相关");
cssTitle.style.cursor = "pointer";
cssTitle.style.padding = "10px 15px";
var triangle = document.createElement("span");
triangle.style.cssText = `
border-top: 6px solid transparent;
border-bottom: 6px solid transparent;
border-left: 6px solid #333;
display: inline-block;
margin-right: 10px;
transition: transform 0.3s ease;
`;
cssTitle.prepend(triangle);
var cssContent = document.createElement("div");
cssContent.style.display = "none";
cssContent.style.padding = "0 15px";
cssLinks.forEach(function (link) {
cssContent.appendChild(createLink(link.href, link.text));
});
cssTitle.onclick = function () {
var isCollapsed = cssContent.style.display === "none";
cssContent.style.display = isCollapsed ? "block" : "none";
triangle.style.transform = isCollapsed ? "rotate(90deg)" : "rotate(0deg)";
};
dropdownContent.appendChild(cssTitle);
dropdownContent.appendChild(cssContent);
}
});
window.onclick = function (event) {
if (!event.target.matches('#dropdownButton') && !dropdownContent.contains(event.target)) {
dropdownContent.style.display = "none";
}
};
var style = document.createElement('style');
style.innerHTML = `
@media (max-width: 600px) {
#dropdownContent {
width: 100%;
left: 0;
right: 0;
}
}
table.def th {
min-width: 5em;
}
`;
document.head.appendChild(style);
// 这是一份志愿者翻译,译文中可能包含错误。本译文仅供参考,应以 W3C 网站上的原始英文版本.......
function extractRelevantLinks(data) {
return data.filter(link => link.src.includes('w3.org') || link.src.includes('wicg.github.io'));
}
const relevantLinksFromLinks = extractRelevantLinks(links);
const relevantLinksFromCssLinks = extractRelevantLinks(cssLinks);
const allRelevantLinks = [...relevantLinksFromLinks, ...relevantLinksFromCssLinks];
function isCurrentUrlMatchingLink(href) {
try {
return window.location.href.includes(href);
} catch (error) {
console.error('Error accessing window.location.href:', error);
return false;
}
}
function checkMatchingLinks(linksArray) {
return linksArray.filter(link => isCurrentUrlMatchingLink(link.href));
}
let matchingLinks = [];
try {
matchingLinks = checkMatchingLinks(allRelevantLinks);
} catch (error) {
console.error('Error while checking matching links:', error);
}
if (matchingLinks.length > 0) {
(function addDisclaimer() {
var disclaimer = document.createElement('div');
disclaimer.className = 'note';
disclaimer.innerHTML = `
<span class="marker">【注意】</span>
<p>这是一份志愿者翻译,译文中可能包含错误。本译文仅供参考,应以 W3C 网站上的原始英文版本(<a href='${matchingLinks[0].src}'>${matchingLinks[0].text}</a>)为准。</p>
`;
document.body.insertAdjacentElement('afterbegin', disclaimer);
})();
}
});
(function () {
var gaID;
var hostname = window.location.hostname;
if (/htmlspecs\.com$/.test(hostname)) {
gaID = 'G-Z0H3G08TCK';
} else if (hostname === 'ecma262.com') {
gaID = 'G-DEEXSBMHX9';
} else {
return;
}
var script = document.createElement('script');
script.async = true;
script.src = 'https://www.googletagmanager.com/gtag/js?id=' + gaID;
document.head.appendChild(script);
script.onload = function () {
window.dataLayer = window.dataLayer || [];
function gtag() { dataLayer.push(arguments); }
gtag('js', new Date());
gtag('config', gaID);
};
})();