Repository navigation
Expand file tree
/
Copy pathlink.js
More file actions
55 lines (48 loc) · 1.57 KB
/
Copy pathlink.js
File metadata and controls
55 lines (48 loc) · 1.57 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
'use strict';
const SPEC_MESSAGE_TYPE = 'cfc-flow-spec';
const SPEC_REQUEST_TYPE = 'cfc-flow-spec-request';
const ERROR_REPORT_TYPE = 'cfc-flow-error';
let cachedSpec;
async function getSpec() {
if (cachedSpec !== undefined) return cachedSpec;
try {
const res = await chrome.runtime.sendMessage({ type: 'getSpec' });
cachedSpec = res?.ok && res.spec ? res.spec : null;
} catch {
cachedSpec = null;
}
return cachedSpec;
}
function postSpecToWindow(spec) {
window.postMessage({ type: SPEC_MESSAGE_TYPE, spec }, location.origin);
}
window.addEventListener('message', (event) => {
if (event.source !== window) return;
if (event.data?.type === SPEC_REQUEST_TYPE) {
getSpec().then(postSpecToWindow);
return;
}
if (event.data?.type === ERROR_REPORT_TYPE && event.data.error) {
chrome.runtime.sendMessage({
type: 'reportError',
error: event.data.error,
}).catch(() => {});
}
});
chrome.runtime.onMessage.addListener((message) => {
if (message?.type === 'cfc-flow-spec-update' && message.spec) {
cachedSpec = message.spec;
postSpecToWindow(message.spec);
}
});
// Auto-recover from /unsupported-country if FlowPass is active
if (typeof location !== 'undefined' && location.pathname.includes('/unsupported-country')) {
getSpec().then((spec) => {
if (spec) {
const cleanPath = location.pathname.replace(/\/unsupported-country\/?$/, '') || '/';
const targetUrl = location.origin + cleanPath + location.search + location.hash;
location.replace(targetUrl);
}
}).catch(() => {});
}
getSpec().then(postSpecToWindow);