-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpopup.js
More file actions
187 lines (168 loc) · 5.21 KB
/
Copy pathpopup.js
File metadata and controls
187 lines (168 loc) · 5.21 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
// Live Caption Transcript - popup
const $ = (id) => document.getElementById(id);
let tab = null;
let connected = false;
let capturing = false;
let entries = [];
function fmtTime(t) {
const d = new Date(t);
const p = (n) => String(n).padStart(2, '0');
return p(d.getHours()) + ':' + p(d.getMinutes()) + ':' + p(d.getSeconds());
}
function fmtDate(t) {
const d = new Date(t);
const p = (n) => String(n).padStart(2, '0');
return d.getFullYear() + p(d.getMonth() + 1) + p(d.getDate()) + '_' + p(d.getHours()) + p(d.getMinutes());
}
function toText() {
return entries
.map((e) => '[' + fmtTime(e.t) + ']' + (e.s ? ' ' + e.s + ':' : '') + ' ' + e.x)
.join('\n');
}
function render() {
const list = $('list');
list.innerHTML = '';
if (!entries.length) {
const div = document.createElement('div');
div.className = 'empty';
div.innerHTML = '尚無逐字稿。<br/>在會議中開啟即時字幕後,按「開始擷取」。';
list.appendChild(div);
} else {
for (const e of entries) {
const item = document.createElement('div');
item.className = 'entry';
const meta = document.createElement('div');
meta.className = 'meta';
meta.textContent = fmtTime(e.t);
if (e.s) {
const sp = document.createElement('span');
sp.className = 'speaker';
sp.textContent = e.s;
meta.appendChild(sp);
}
const text = document.createElement('div');
text.className = 'text';
text.textContent = e.x;
item.appendChild(meta);
item.appendChild(text);
list.appendChild(item);
}
list.scrollTop = list.scrollHeight;
}
$('count').textContent = entries.length + ' 句';
}
function setState() {
const dot = $('statusDot');
const st = $('statusText');
const btn = $('btnToggle');
if (!connected) {
dot.className = 'dot off';
st.textContent = '此頁面無法擷取';
btn.disabled = true;
$('btnPick').disabled = true;
return;
}
btn.disabled = false;
$('btnPick').disabled = false;
if (capturing) {
dot.className = 'dot on';
st.textContent = '擷取中…';
btn.textContent = '停止擷取';
btn.className = 'primary stop';
} else {
dot.className = 'dot';
st.textContent = '已連線,未擷取';
btn.textContent = '開始擷取';
btn.className = 'primary';
}
}
function notice(msg) {
const n = $('notice');
if (!msg) { n.classList.add('hidden'); return; }
n.textContent = msg;
n.classList.remove('hidden');
}
async function send(cmd) {
try {
return await chrome.tabs.sendMessage(tab.id, { cmd });
} catch (e) {
return null;
}
}
async function ensureContent() {
let res = await send('ping');
if (res && res.ok) return res;
// 尚未注入(非 Teams/Webex 網站)→ 用 activeTab 權限注入
try {
await chrome.scripting.executeScript({
target: { tabId: tab.id, allFrames: true },
files: ['content.js']
});
res = await send('ping');
if (res && res.ok) return res;
} catch (e) { /* chrome:// 等頁面會失敗 */ }
return null;
}
async function init() {
[tab] = await chrome.tabs.query({ active: true, currentWindow: true });
if (!tab) { setState(); return; }
const { lc_entries } = await chrome.storage.local.get('lc_entries');
entries = Array.isArray(lc_entries) ? lc_entries : [];
render();
const res = await ensureContent();
if (res) {
connected = true;
capturing = !!res.capturing;
if (res.host && !res.host.includes('teams') && !res.host.includes('webex') && !res.picked) {
notice('這個網站不是 Teams / Webex:請先按「框選字幕區域」,在頁面上點選字幕所在的區塊。');
}
} else {
connected = false;
}
setState();
}
// 即時更新:storage 變動就重新渲染
chrome.storage.onChanged.addListener((changes, area) => {
if (area !== 'local') return;
if (changes.lc_entries) {
entries = changes.lc_entries.newValue || [];
render();
}
if (changes.lc_capturing) {
capturing = !!changes.lc_capturing.newValue;
setState();
}
});
$('btnToggle').addEventListener('click', async () => {
const res = await send(capturing ? 'stop' : 'start');
if (res && res.ok) {
capturing = !!res.capturing;
setState();
}
});
$('btnPick').addEventListener('click', async () => {
const res = await send('pick');
if (res && res.ok) window.close(); // 關閉 popup,讓使用者在頁面上點選
});
$('btnCopy').addEventListener('click', async () => {
await navigator.clipboard.writeText(toText());
$('btnCopy').textContent = '已複製 ✔';
setTimeout(() => ($('btnCopy').textContent = '複製'), 1500);
});
$('btnDownload').addEventListener('click', () => {
const blob = new Blob(['' + toText()], { type: 'text/plain;charset=utf-8' });
const url = URL.createObjectURL(blob);
const a = document.createElement('a');
a.href = url;
a.download = 'transcript_' + fmtDate(entries.length ? entries[0].t : Date.now()) + '.txt';
a.click();
setTimeout(() => URL.revokeObjectURL(url), 5000);
});
$('btnClear').addEventListener('click', async () => {
if (!confirm('確定要清除目前的逐字稿嗎?')) return;
await send('clear');
entries = [];
await chrome.storage.local.set({ lc_entries: [] });
render();
});
init();