| 1 | import { randomBytes } from "node:crypto"; |
| 2 | |
| 3 | |
| 4 | |
| 5 | |
| 6 | function prepareCopies(bindingName, token, contextSetter, limit, includeChanges) { |
| 7 | const emit = globalThis[bindingName]; |
| 8 | delete globalThis[bindingName]; |
| 9 | if (typeof emit !== "function") return; |
| 10 | const apply = Reflect.apply; |
| 11 | const define = Object.defineProperty; |
| 12 | const descriptor = Object.getOwnPropertyDescriptor; |
| 13 | const stringify = JSON.stringify; |
| 14 | const string = String; |
| 15 | const then = Promise.prototype.then; |
| 16 | const hasFocus = Document.prototype.hasFocus; |
| 17 | const visibility = descriptor(Document.prototype, "visibilityState").get; |
| 18 | const activated = descriptor(UserActivation.prototype, "isActive").get; |
| 19 | const userActivation = descriptor(Navigator.prototype, "userActivation").get; |
| 20 | const addListener = EventTarget.prototype.addEventListener; |
| 21 | const prevented = descriptor(Event.prototype, "defaultPrevented").get; |
| 22 | const clipboardData = descriptor( |
| 23 | ClipboardEvent.prototype, |
| 24 | "clipboardData", |
| 25 | ).get; |
| 26 | const blobText = Blob.prototype.text; |
| 27 | const lower = String.prototype.toLowerCase; |
| 28 | const exec = Document.prototype.execCommand; |
| 29 | const setData = DataTransfer.prototype.setData; |
| 30 | const clearData = DataTransfer.prototype.clearData; |
| 31 | const writeText = globalThis.Clipboard?.prototype.writeText; |
| 32 | const write = globalThis.Clipboard?.prototype.write; |
| 33 | const iterator = Symbol.iterator; |
| 34 | const getType = globalThis.ClipboardItem?.prototype.getType; |
| 35 | const clipboardPrototype = globalThis.Clipboard?.prototype; |
| 36 | const transferPrototype = DataTransfer.prototype; |
| 37 | const documentPrototype = Document.prototype; |
| 38 | const activeElement = descriptor(Document.prototype, "activeElement").get; |
| 39 | const shadowActiveElement = descriptor( |
| 40 | ShadowRoot.prototype, |
| 41 | "activeElement", |
| 42 | ).get; |
| 43 | const shadowRoot = descriptor(Element.prototype, "shadowRoot").get; |
| 44 | const tagName = descriptor(Element.prototype, "tagName").get; |
| 45 | const inputType = descriptor(HTMLInputElement.prototype, "type").get; |
| 46 | const inputStart = descriptor( |
| 47 | HTMLInputElement.prototype, |
| 48 | "selectionStart", |
| 49 | ).get; |
| 50 | const inputEnd = descriptor(HTMLInputElement.prototype, "selectionEnd").get; |
| 51 | const textareaStart = descriptor( |
| 52 | HTMLTextAreaElement.prototype, |
| 53 | "selectionStart", |
| 54 | ).get; |
| 55 | const textareaEnd = descriptor( |
| 56 | HTMLTextAreaElement.prototype, |
| 57 | "selectionEnd", |
| 58 | ).get; |
| 59 | const getSelection = Document.prototype.getSelection; |
| 60 | const selectionText = Selection.prototype.toString; |
| 61 | const selectionCollapsed = descriptor(Selection.prototype, "isCollapsed").get; |
| 62 | const schedule = globalThis.setTimeout; |
| 63 | const nativeFunctions = { |
| 64 | "": emit, |
| 65 | apply, |
| 66 | defineProperty: define, |
| 67 | getOwnPropertyDescriptor: descriptor, |
| 68 | stringify, |
| 69 | String: string, |
| 70 | then, |
| 71 | hasFocus, |
| 72 | "get visibilityState": visibility, |
| 73 | "get isActive": activated, |
| 74 | "get userActivation": userActivation, |
| 75 | addEventListener: addListener, |
| 76 | "get defaultPrevented": prevented, |
| 77 | "get clipboardData": clipboardData, |
| 78 | text: blobText, |
| 79 | toLowerCase: lower, |
| 80 | execCommand: exec, |
| 81 | setData, |
| 82 | clearData, |
| 83 | Symbol, |
| 84 | writeText, |
| 85 | write, |
| 86 | getType, |
| 87 | "get activeElement": activeElement, |
| 88 | "get activeElement#shadow": shadowActiveElement, |
| 89 | "get shadowRoot": shadowRoot, |
| 90 | "get tagName": tagName, |
| 91 | "get type": inputType, |
| 92 | "get selectionStart#input": inputStart, |
| 93 | "get selectionEnd#input": inputEnd, |
| 94 | "get selectionStart#textarea": textareaStart, |
| 95 | "get selectionEnd#textarea": textareaEnd, |
| 96 | getSelection, |
| 97 | toString: selectionText, |
| 98 | "get isCollapsed": selectionCollapsed, |
| 99 | setTimeout: schedule, |
| 100 | }; |
| 101 | const plain = (value) => { |
| 102 | const type = apply(lower, value, []); |
| 103 | return type === "text" || type === "text/plain"; |
| 104 | }; |
| 105 | return { |
| 106 | nativeFunctions, |
| 107 | install() { |
| 108 | const activation = apply(userActivation, navigator, []); |
| 109 | let epoch = 0; |
| 110 | let operation; |
| 111 | let context = ""; |
| 112 | |
| 113 | define(globalThis, contextSetter, { |
| 114 | value: function (value) { |
| 115 | if (typeof value === "string" && value.length <= 64) context = value; |
| 116 | }, |
| 117 | }); |
| 118 | const current = () => |
| 119 | apply(hasFocus, document, []) && |
| 120 | apply(visibility, document, []) === "visible"; |
| 121 | const begin = () => |
| 122 | context && current() && apply(activated, activation, []) |
| 123 | ? { epoch, context } |
| 124 | : null; |
| 125 | const focusedElement = () => { |
| 126 | let element = apply(activeElement, document, []); |
| 127 | let root; |
| 128 | while (element && (root = apply(shadowRoot, element, []))) |
| 129 | element = apply(shadowActiveElement, root, []); |
| 130 | return element; |
| 131 | }; |
| 132 | const selectedControlText = () => { |
| 133 | const element = focusedElement(); |
| 134 | if (!element) return; |
| 135 | const tag = apply(tagName, element, []); |
| 136 | if (tag !== "TEXTAREA" && tag !== "INPUT") return; |
| 137 | if (tag === "INPUT") { |
| 138 | const type = apply(inputType, element, []); |
| 139 | if ( |
| 140 | type !== "text" && |
| 141 | type !== "search" && |
| 142 | type !== "url" && |
| 143 | type !== "tel" |
| 144 | ) |
| 145 | return; |
| 146 | } |
| 147 | const start = apply( |
| 148 | tag === "INPUT" ? inputStart : textareaStart, |
| 149 | element, |
| 150 | [], |
| 151 | ); |
| 152 | const end = apply( |
| 153 | tag === "INPUT" ? inputEnd : textareaEnd, |
| 154 | element, |
| 155 | [], |
| 156 | ); |
| 157 | if (start === null || end <= start) return; |
| 158 | const selection = apply(getSelection, document, []); |
| 159 | return selection && apply(selectionText, selection, []); |
| 160 | }; |
| 161 | const hasCopySelection = () => { |
| 162 | const element = focusedElement(); |
| 163 | if (!element) return false; |
| 164 | const tag = apply(tagName, element, []); |
| 165 | if (tag === "INPUT" || tag === "TEXTAREA") { |
| 166 | if (tag === "INPUT") { |
| 167 | const type = apply(inputType, element, []); |
| 168 | if (type !== "text" && type !== "search" && type !== "url" && type !== "tel") return false; |
| 169 | } |
| 170 | const start = apply(tag === "INPUT" ? inputStart : textareaStart, element, []); |
| 171 | const end = apply(tag === "INPUT" ? inputEnd : textareaEnd, element, []); |
| 172 | return start !== null && end > start; |
| 173 | } |
| 174 | const selection = apply(getSelection, document, []); |
| 175 | return selection && !apply(selectionCollapsed, selection, []); |
| 176 | }; |
| 177 | const send = (text, started) => { |
| 178 | if ( |
| 179 | started === null || |
| 180 | epoch !== started.epoch || |
| 181 | !current() || |
| 182 | !includeChanges && (typeof text !== "string" || text.length > limit) |
| 183 | ) |
| 184 | return; |
| 185 | const content = typeof text === "string" && text.length <= limit |
| 186 | ? ',"text":' + stringify(text) |
| 187 | : ',"changed":true'; |
| 188 | emit( |
| 189 | '{"token":"' + |
| 190 | token + |
| 191 | '","context":' + |
| 192 | stringify(started.context) + |
| 193 | content + |
| 194 | "}", |
| 195 | ); |
| 196 | }; |
| 197 | const watch = (promise, text, started) => { |
| 198 | |
| 199 | apply(then, promise, [ |
| 200 | () => { |
| 201 | send(text, started); |
| 202 | }, |
| 203 | () => {}, |
| 204 | ]); |
| 205 | return promise; |
| 206 | }; |
| 207 | const cancel = () => { |
| 208 | epoch++; |
| 209 | }; |
| 210 | apply(addListener, window, ["blur", cancel, true]); |
| 211 | apply(addListener, window, ["pagehide", cancel, true]); |
| 212 | apply(addListener, document, [ |
| 213 | "visibilitychange", |
| 214 | () => { |
| 215 | epoch++; |
| 216 | }, |
| 217 | true, |
| 218 | ]); |
| 219 | |
| 220 | if (writeText) { |
| 221 | define(clipboardPrototype, "writeText", { |
| 222 | configurable: true, |
| 223 | writable: true, |
| 224 | value: function (value) { |
| 225 | if (!arguments.length || typeof value === "symbol") |
| 226 | return apply(writeText, this, arguments); |
| 227 | const started = begin(); |
| 228 | |
| 229 | const text = string(value); |
| 230 | return watch(apply(writeText, this, [text]), text, started); |
| 231 | }, |
| 232 | }); |
| 233 | if (write && getType) |
| 234 | define(clipboardPrototype, "write", { |
| 235 | configurable: true, |
| 236 | writable: true, |
| 237 | value: function (value) { |
| 238 | const started = begin(); |
| 239 | let items; |
| 240 | try { |
| 241 | items = [...value]; |
| 242 | } catch { |
| 243 | return apply(write, this, arguments); |
| 244 | } |
| 245 | define(items, iterator, { |
| 246 | value: function () { |
| 247 | let index = 0; |
| 248 | return { |
| 249 | next: () => |
| 250 | index < items.length |
| 251 | ? { value: items[index++], done: false } |
| 252 | : { done: true }, |
| 253 | }; |
| 254 | }, |
| 255 | }); |
| 256 | const promise = apply(write, this, [items]); |
| 257 | if (started === null) return promise; |
| 258 | if (includeChanges) return watch(promise, undefined, started); |
| 259 | if (items.length !== 1) return promise; |
| 260 | let text, |
| 261 | written = false; |
| 262 | const completed = () => { |
| 263 | if (written && typeof text === "string") send(text, started); |
| 264 | }; |
| 265 | try { |
| 266 | apply(then, apply(getType, items[0], ["text/plain"]), [ |
| 267 | (blob) => { |
| 268 | apply(then, apply(blobText, blob, []), [ |
| 269 | (value) => { |
| 270 | text = value; |
| 271 | completed(); |
| 272 | }, |
| 273 | () => {}, |
| 274 | ]); |
| 275 | }, |
| 276 | () => {}, |
| 277 | ]); |
| 278 | } catch { |
| 279 | return promise; |
| 280 | } |
| 281 | apply(then, promise, [ |
| 282 | () => { |
| 283 | written = true; |
| 284 | completed(); |
| 285 | }, |
| 286 | () => {}, |
| 287 | ]); |
| 288 | return promise; |
| 289 | }, |
| 290 | }); |
| 291 | } |
| 292 | |
| 293 | define(transferPrototype, "setData", { |
| 294 | configurable: true, |
| 295 | writable: true, |
| 296 | value: function (format, value) { |
| 297 | if ( |
| 298 | arguments.length < 2 || |
| 299 | typeof format === "symbol" || |
| 300 | typeof value === "symbol" |
| 301 | ) |
| 302 | return apply(setData, this, arguments); |
| 303 | const type = string(format); |
| 304 | const text = string(value); |
| 305 | const result = apply(setData, this, [type, text]); |
| 306 | if (operation?.data === this && plain(type)) operation.text = text; |
| 307 | return result; |
| 308 | }, |
| 309 | }); |
| 310 | define(transferPrototype, "clearData", { |
| 311 | configurable: true, |
| 312 | writable: true, |
| 313 | value: function (format) { |
| 314 | if (typeof format === "symbol") |
| 315 | return apply(clearData, this, arguments); |
| 316 | const type = format === undefined ? undefined : string(format); |
| 317 | const result = apply( |
| 318 | clearData, |
| 319 | this, |
| 320 | type === undefined ? [] : [type], |
| 321 | ); |
| 322 | if (operation?.data === this && (type === undefined || plain(type))) |
| 323 | operation.text = undefined; |
| 324 | return result; |
| 325 | }, |
| 326 | }); |
| 327 | const copied = |
| 328 | (event) => { |
| 329 | if (!event.isTrusted) return; |
| 330 | if (operation && !operation.native) { |
| 331 | operation.event = event; |
| 332 | operation.data = apply(clipboardData, event, []); |
| 333 | } else if (includeChanges) { |
| 334 | const copy = { native: true, event, data: apply(clipboardData, event, []), started: begin(), selected: hasCopySelection() }; |
| 335 | operation = copy; |
| 336 | |
| 337 | |
| 338 | apply(schedule, globalThis, [() => { |
| 339 | if (operation === copy) operation = undefined; |
| 340 | if (apply(prevented, event, [])) { |
| 341 | if (typeof copy.text === "string") send(copy.text, copy.started); |
| 342 | } else if (copy.selected) send(undefined, copy.started); |
| 343 | }, 0]); |
| 344 | } |
| 345 | }; |
| 346 | apply(addListener, document, ["copy", copied, true]); |
| 347 | if (includeChanges) apply(addListener, document, ["cut", copied, true]); |
| 348 | define(documentPrototype, "execCommand", { |
| 349 | configurable: true, |
| 350 | writable: true, |
| 351 | value: function (command, ...args) { |
| 352 | if (!arguments.length || typeof command === "symbol") |
| 353 | return apply(exec, this, arguments); |
| 354 | const name = string(command); |
| 355 | if (apply(lower, name, []) !== "copy" || this !== document) |
| 356 | return apply(exec, this, [name, ...args]); |
| 357 | const parent = operation; |
| 358 | const copy = { started: begin(), selected: includeChanges && hasCopySelection() }; |
| 359 | operation = copy; |
| 360 | let result; |
| 361 | try { |
| 362 | result = apply(exec, this, [name, ...args]); |
| 363 | } finally { |
| 364 | operation = parent; |
| 365 | } |
| 366 | if (result && copy.event) { |
| 367 | if (apply(prevented, copy.event, [])) { |
| 368 | if (typeof copy.text === "string") send(copy.text, copy.started); |
| 369 | } |
| 370 | else { |
| 371 | |
| 372 | |
| 373 | const text = selectedControlText(); |
| 374 | if (text) send(text, copy.started); |
| 375 | else if (copy.selected) send(undefined, copy.started); |
| 376 | } |
| 377 | } |
| 378 | return result; |
| 379 | }, |
| 380 | }); |
| 381 | }, |
| 382 | }; |
| 383 | } |
| 384 | |
| 385 | export class BrowserClipboard { |
| 386 | constructor(cdp, onCopy, { includeChanges = false } = {}) { |
| 387 | this.cdp = cdp; |
| 388 | this.onCopy = onCopy; |
| 389 | this.includeChanges = includeChanges; |
| 390 | this.sessions = new Map(); |
| 391 | this.copyContexts = new Map(); |
| 392 | } |
| 393 | |
| 394 | async attach(sessionId, tabId) { |
| 395 | if (this.sessions.has(sessionId)) return this.sessions.get(sessionId).ready; |
| 396 | const state = { |
| 397 | tabId, |
| 398 | contexts: new Map(), |
| 399 | name: "cageCopy" + randomBytes(16).toString("hex"), |
| 400 | token: randomBytes(32).toString("hex"), |
| 401 | contextSetter: "cageCopyContext" + randomBytes(16).toString("hex"), |
| 402 | }; |
| 403 | this.sessions.set(sessionId, state); |
| 404 | state.ready = this.#install(sessionId, state); |
| 405 | return state.ready; |
| 406 | } |
| 407 | |
| 408 | async #install(sessionId, state) { |
| 409 | const { tabId } = state; |
| 410 | try { |
| 411 | await this.cdp("Page.enable", {}, sessionId); |
| 412 | await this.cdp("Runtime.enable", {}, sessionId); |
| 413 | await this.cdp("Runtime.addBinding", { name: state.name }, sessionId); |
| 414 | const source = `(${prepareCopies})(${JSON.stringify(state.name)},${JSON.stringify(state.token)},${JSON.stringify(state.contextSetter)},65536,${this.includeChanges})`; |
| 415 | await this.cdp( |
| 416 | "Page.addScriptToEvaluateOnNewDocument", |
| 417 | { source: `(${source})?.install()` }, |
| 418 | sessionId, |
| 419 | ); |
| 420 | const skipped = []; |
| 421 | for (const contextId of state.contexts.keys()) { |
| 422 | const group = "cage-clipboard-" + randomBytes(8).toString("hex"); |
| 423 | try { |
| 424 | const prepared = await this.cdp( |
| 425 | "Runtime.evaluate", |
| 426 | { expression: source, contextId, objectGroup: group }, |
| 427 | sessionId, |
| 428 | ); |
| 429 | if (prepared.exceptionDetails) { |
| 430 | skipped.push({ |
| 431 | contextId, |
| 432 | reason: "Clipboard APIs unavailable or modified", |
| 433 | }); |
| 434 | continue; |
| 435 | } |
| 436 | if (!prepared.result?.objectId) continue; |
| 437 | const properties = await this.cdp( |
| 438 | "Runtime.getProperties", |
| 439 | { objectId: prepared.result.objectId, ownProperties: true }, |
| 440 | sessionId, |
| 441 | ); |
| 442 | const functions = properties.result.find( |
| 443 | (p) => p.name === "nativeFunctions", |
| 444 | )?.value?.objectId; |
| 445 | if (!functions) continue; |
| 446 | const refs = await this.cdp( |
| 447 | "Runtime.getProperties", |
| 448 | { objectId: functions, ownProperties: true }, |
| 449 | sessionId, |
| 450 | ); |
| 451 | const modified = refs.result.find( |
| 452 | (p) => |
| 453 | !( |
| 454 | ["writeText", "write", "getType"].includes(p.name) && |
| 455 | p.value?.type === "undefined" |
| 456 | ) && |
| 457 | (p.value?.type !== "function" || |
| 458 | p.value.description !== |
| 459 | `function ${p.name.split("#", 1)[0]}() { [native code] }`), |
| 460 | ); |
| 461 | if (modified) { |
| 462 | skipped.push({ |
| 463 | contextId, |
| 464 | reason: `Modified ${modified.name || "binding"}`, |
| 465 | }); |
| 466 | continue; |
| 467 | } |
| 468 | const binding = await this.cdp( |
| 469 | "Runtime.getProperties", |
| 470 | { |
| 471 | objectId: refs.result.find((p) => p.name === "").value.objectId, |
| 472 | ownProperties: true, |
| 473 | }, |
| 474 | sessionId, |
| 475 | ); |
| 476 | if ( |
| 477 | binding.internalProperties?.some( |
| 478 | (p) => p.name === "[[TargetFunction]]", |
| 479 | ) |
| 480 | ) { |
| 481 | skipped.push({ contextId, reason: "Modified binding" }); |
| 482 | continue; |
| 483 | } |
| 484 | const result = await this.cdp( |
| 485 | "Runtime.callFunctionOn", |
| 486 | { |
| 487 | objectId: prepared.result.objectId, |
| 488 | functionDeclaration: "function() { this.install(); }", |
| 489 | }, |
| 490 | sessionId, |
| 491 | ); |
| 492 | if (result.exceptionDetails) |
| 493 | skipped.push({ |
| 494 | contextId, |
| 495 | reason: "Clipboard observer installation failed", |
| 496 | }); |
| 497 | } catch { |
| 498 | skipped.push({ |
| 499 | contextId, |
| 500 | reason: "Document changed during clipboard observer installation", |
| 501 | }); |
| 502 | } finally { |
| 503 | await this.cdp( |
| 504 | "Runtime.releaseObjectGroup", |
| 505 | { objectGroup: group }, |
| 506 | sessionId, |
| 507 | ).catch(() => {}); |
| 508 | } |
| 509 | } |
| 510 | await this.cdp( |
| 511 | "Target.setAutoAttach", |
| 512 | { |
| 513 | autoAttach: true, |
| 514 | waitForDebuggerOnStart: false, |
| 515 | flatten: true, |
| 516 | filter: [{ type: "iframe" }, { exclude: true }], |
| 517 | }, |
| 518 | sessionId, |
| 519 | ); |
| 520 | if (this.copyContexts.has(tabId)) |
| 521 | await this.#applyContext( |
| 522 | sessionId, |
| 523 | state, |
| 524 | this.copyContexts.get(tabId), |
| 525 | ); |
| 526 | return { skipped }; |
| 527 | } catch (error) { |
| 528 | this.sessions.delete(sessionId); |
| 529 | throw error; |
| 530 | } |
| 531 | } |
| 532 | |
| 533 | async setContext(id, context) { |
| 534 | if (typeof context !== "string" || context.length > 64) |
| 535 | throw new Error("Invalid clipboard context"); |
| 536 | this.copyContexts.set(id, context); |
| 537 | const applied = new Set(); |
| 538 | for (;;) { |
| 539 | const sessions = [...this.sessions].filter( |
| 540 | ([, state]) => state.tabId === id && !applied.has(state), |
| 541 | ); |
| 542 | if (!sessions.length) return; |
| 543 | await Promise.all( |
| 544 | sessions.map(async ([sessionId, state]) => { |
| 545 | await state.ready; |
| 546 | if (this.sessions.get(sessionId) === state) |
| 547 | await this.#applyContext(sessionId, state, context); |
| 548 | applied.add(state); |
| 549 | }), |
| 550 | ); |
| 551 | |
| 552 | } |
| 553 | } |
| 554 | |
| 555 | async #applyContext(sessionId, state, context) { |
| 556 | const updates = []; |
| 557 | for (const contextId of state.contexts.keys()) { |
| 558 | updates.push( |
| 559 | this.cdp( |
| 560 | "Runtime.evaluate", |
| 561 | { |
| 562 | expression: `globalThis[${JSON.stringify(state.contextSetter)}]?.(${JSON.stringify(context)})`, |
| 563 | contextId, |
| 564 | }, |
| 565 | sessionId, |
| 566 | ).catch(() => {}), |
| 567 | ); |
| 568 | } |
| 569 | await Promise.all(updates); |
| 570 | } |
| 571 | |
| 572 | event(event) { |
| 573 | const state = this.sessions.get(event.sessionId); |
| 574 | if (!state) return; |
| 575 | const p = event.params || {}; |
| 576 | if ( |
| 577 | event.method === "Target.attachedToTarget" && |
| 578 | p.targetInfo?.type === "iframe" |
| 579 | ) { |
| 580 | void this.attach(p.sessionId, state.tabId).catch(() => {}); |
| 581 | } else if (event.method === "Target.detachedFromTarget") { |
| 582 | this.detachSession(p.sessionId); |
| 583 | } else if (event.method === "Runtime.executionContextCreated") { |
| 584 | if (p.context?.auxData?.isDefault) |
| 585 | state.contexts.set(p.context.id, p.context.auxData.frameId); |
| 586 | } else if (event.method === "Runtime.executionContextDestroyed") { |
| 587 | state.contexts.delete(p.executionContextId); |
| 588 | } else if (event.method === "Runtime.executionContextsCleared") { |
| 589 | state.contexts.clear(); |
| 590 | } else if ( |
| 591 | event.method === "Page.frameDetached" || |
| 592 | event.method === "Page.frameNavigated" |
| 593 | ) { |
| 594 | const id = p.frameId || p.frame?.id; |
| 595 | for (const [context, frame] of state.contexts) |
| 596 | if (frame === id) state.contexts.delete(context); |
| 597 | } else if ( |
| 598 | event.method === "Runtime.bindingCalled" && |
| 599 | p.name === state.name && |
| 600 | state.contexts.has(p.executionContextId) |
| 601 | ) { |
| 602 | if ( |
| 603 | typeof p.payload !== "string" || |
| 604 | p.payload.length > 6 * (65536 + 64) + 200 |
| 605 | ) |
| 606 | return; |
| 607 | let value; |
| 608 | try { |
| 609 | value = JSON.parse(p.payload); |
| 610 | } catch { |
| 611 | return; |
| 612 | } |
| 613 | if ( |
| 614 | value?.token !== state.token || |
| 615 | typeof value.context !== "string" || |
| 616 | value.context.length > 64 || |
| 617 | !(typeof value.text === "string" && value.text.length <= 65536 || this.includeChanges && value.changed === true && value.text === undefined) |
| 618 | ) |
| 619 | return; |
| 620 | this.onCopy({ |
| 621 | id: state.tabId, |
| 622 | ...(typeof value.text === "string" ? { text: value.text } : { changed: true }), |
| 623 | context: value.context, |
| 624 | }); |
| 625 | } |
| 626 | } |
| 627 | |
| 628 | detachSession(id) { |
| 629 | this.sessions.delete(id); |
| 630 | } |
| 631 | |
| 632 | detachTab(id) { |
| 633 | this.copyContexts.delete(id); |
| 634 | for (const [session, state] of this.sessions) |
| 635 | if (state.tabId === id) this.sessions.delete(session); |
| 636 | } |
| 637 | } |