-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathGoogleFlightsExport.js
More file actions
57 lines (53 loc) · 1.6 KB
/
GoogleFlightsExport.js
File metadata and controls
57 lines (53 loc) · 1.6 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
import {
extractLegsFromDom,
formatTripText,
formatTripHtml,
} from "./extract.js"
const TOAST_DURATION_MS = 3000
function showToast(message, { error = false } = {}) {
const div = document.createElement("div")
div.textContent = message
div.style.cssText = [
"position:fixed",
"top:20px",
"right:20px",
"z-index:2147483647",
"padding:12px 16px",
"border-radius:6px",
"font:14px -apple-system,BlinkMacSystemFont,sans-serif",
"color:#fff",
`background:${error ? "#d93025" : "#1a73e8"}`,
"box-shadow:0 2px 8px rgba(0,0,0,0.2)",
"max-width:360px",
].join(";")
document.body.appendChild(div)
setTimeout(() => div.remove(), TOAST_DURATION_MS)
}
async function run() {
const legs = extractLegsFromDom(document)
if (legs.length === 0) {
throw new Error(
"No flight legs found — is this the Google Flights booking/results page?"
)
}
const text = formatTripText(legs)
const html = formatTripHtml(legs)
await navigator.clipboard.write([
new ClipboardItem({
"text/plain": new Blob([text], { type: "text/plain" }),
"text/html": new Blob([html], { type: "text/html" }),
}),
])
return { count: legs.length, text, html }
}
;(async () => {
try {
const { count, text, html } = await run()
showToast(`Copied ${count} flight leg${count === 1 ? "" : "s"} to clipboard`)
window.__gfeResult = { ok: true, count, text, html }
} catch (err) {
console.error("GoogleFlightsExport failed:", err)
showToast(`Export failed: ${err.message}`, { error: true })
window.__gfeResult = { ok: false, error: err.message }
}
})()