-
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlegal.js
More file actions
207 lines (182 loc) · 6.5 KB
/
Copy pathlegal.js
File metadata and controls
207 lines (182 loc) · 6.5 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
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
// legal.js — Inline viewer for the AGPLv3 license and the privacy policy.
//
// The documents themselves are plain files (LICENSE, PRIVACY.md) that the
// build scripts copy into the extension package; this script fetches them at
// runtime instead of duplicating the text in JS. The popup and options pages
// both load this file and share the same modal markup (the #legalModal
// overlay). Triggers carry a data-legal-doc="license|privacy" attribute.
//
// Works in the browser (globalThis) only — it is not part of the Node test
// suite, so keep it free of any module imports.
(function (root) {
'use strict';
const browserAPI = typeof browser !== 'undefined' ? browser : chrome;
const DOCS = {
license: { file: 'LICENSE', title: 'GNU Affero General Public License', markdown: false },
privacy: { file: 'PRIVACY.md', title: 'Privacy Policy', markdown: true }
};
const LOADING_TEXT = 'Loading…';
const ERROR_TEXT = 'Could not load the document.';
let lastFocusedElement = null;
function getModal() {
return document.getElementById('legalModal');
}
function cleanInline(text) {
return text.replace(/\*\*/g, '').replace(/`/g, '').trim();
}
// Plain-text headings (AGPL-style): ALL-CAPS titles, numbered section
// headings like "1. Source Code." (which may run long, e.g. section 13),
// and short standalone titles such as "Preamble". Long lines are always
// treated as body text.
function isHeadingLine(line) {
if (/^\d+\.\s+[A-Z]/.test(line)) return true;
if (line.length > 70) return false;
if (/^[A-Z][A-Z0-9 .-]+$/.test(line)) return true;
return line.length <= 40 && /^[A-Za-z][A-Za-z0-9 ']*$/.test(line) && !line.endsWith('.');
}
function renderDocument(container, text, markdown) {
container.textContent = '';
const lines = text.split('\n');
let paragraph = [];
let bullets = [];
const flushParagraph = () => {
if (paragraph.length > 0) {
const p = document.createElement('p');
p.textContent = paragraph.join(' ').replace(/\s+/g, ' ').trim();
container.appendChild(p);
paragraph = [];
}
};
const flushBullets = () => {
if (bullets.length > 0) {
const ul = document.createElement('ul');
bullets.forEach(item => {
const li = document.createElement('li');
li.textContent = item;
ul.appendChild(li);
});
container.appendChild(ul);
bullets = [];
}
};
lines.forEach(rawLine => {
const line = rawLine.trim();
if (!line) {
flushParagraph();
flushBullets();
return;
}
if (markdown && line.startsWith('#')) {
flushParagraph();
flushBullets();
const h = document.createElement('h3');
h.textContent = cleanInline(line.replace(/^#+\s*/, ''));
container.appendChild(h);
return;
}
if (isHeadingLine(line)) {
flushParagraph();
flushBullets();
const h = document.createElement('h3');
h.textContent = cleanInline(line);
container.appendChild(h);
return;
}
if (line.startsWith('- ')) {
flushParagraph();
bullets.push(cleanInline(line.slice(2)));
return;
}
flushBullets();
paragraph.push(cleanInline(line));
});
flushParagraph();
flushBullets();
}
function focusableElements(dialog) {
return Array.from(dialog.querySelectorAll(
'a[href], button:not([disabled]), [tabindex]:not([tabindex="-1"])'
)).filter(el => el.offsetParent !== null || el === document.activeElement);
}
function openLegalModal(doc) {
const modal = getModal();
const config = DOCS[doc];
if (!modal || !config) return;
const titleEl = document.getElementById('legalModalTitle');
const bodyEl = document.getElementById('legalModalBody');
if (titleEl) titleEl.textContent = config.title;
if (bodyEl) bodyEl.textContent = LOADING_TEXT;
lastFocusedElement = document.activeElement;
modal.hidden = false;
document.body.style.overflow = 'hidden';
fetch(browserAPI.runtime.getURL(config.file))
.then(response => {
if (!response.ok) throw new Error('HTTP ' + response.status);
return response.text();
})
.then(text => renderDocument(bodyEl, text, config.markdown))
.catch(() => {
if (bodyEl) bodyEl.textContent = ERROR_TEXT;
})
.then(() => {
const closeBtn = modal.querySelector('[data-legal-close]');
if (closeBtn) closeBtn.focus();
});
}
function closeLegalModal() {
const modal = getModal();
if (!modal) return;
modal.hidden = true;
document.body.style.overflow = '';
if (lastFocusedElement && typeof lastFocusedElement.focus === 'function') {
lastFocusedElement.focus();
}
lastFocusedElement = null;
}
function initLegalModals() {
const modal = getModal();
if (!modal) return;
// Delegated click handling so triggers added after init (e.g. the
// localized Data & Privacy intro link built by options.js) still work.
document.addEventListener('click', event => {
const trigger = event.target.closest('[data-legal-doc]');
if (trigger) openLegalModal(trigger.dataset.legalDoc);
});
modal.querySelectorAll('[data-legal-close]').forEach(btn => {
btn.addEventListener('click', closeLegalModal);
});
modal.addEventListener('click', event => {
if (event.target === modal) closeLegalModal();
});
document.addEventListener('keydown', event => {
if (modal.hidden) return;
if (event.key === 'Escape') {
closeLegalModal();
return;
}
if (event.key === 'Tab') {
const dialog = modal.querySelector('.legal-dialog');
if (!dialog) return;
const items = focusableElements(dialog);
if (items.length === 0) return;
const first = items[0];
const last = items[items.length - 1];
if (event.shiftKey && document.activeElement === first) {
event.preventDefault();
last.focus();
} else if (!event.shiftKey && document.activeElement === last) {
event.preventDefault();
first.focus();
}
}
});
}
root.HaiiloLegal = { openLegalModal, closeLegalModal, initLegalModals, renderDocument };
if (typeof document !== 'undefined') {
if (document.readyState === 'loading') {
document.addEventListener('DOMContentLoaded', initLegalModals, { once: true });
} else {
initLegalModals();
}
}
})(globalThis);