()
| 380 | let selectionToolsTouchInitialized = false |
| 381 | |
| 382 | async function prepareForSelectionToolsTouch() { |
| 383 | if (selectionToolsTouchInitialized) { |
| 384 | console.debug('[content] Touch selection tools already initialized, skipping.') |
| 385 | return |
| 386 | } |
| 387 | selectionToolsTouchInitialized = true |
| 388 | console.log('[content] Initializing touch selection tools.') |
| 389 | document.addEventListener('touchend', (e) => { |
| 390 | try { |
| 391 | if (toolbarContainer?.contains(e.target)) { |
| 392 | console.debug('[content] Touchend inside toolbar, ignoring.') |
| 393 | return |
| 394 | } |
| 395 | if ( |
| 396 | window.getSelection()?.rangeCount > 0 && |
| 397 | toolbarContainer?.contains(window.getSelection()?.getRangeAt(0).endContainer.parentElement) |
| 398 | ) { |
| 399 | console.debug('[content] Touchend selection is inside toolbar, ignoring.') |
| 400 | return |
| 401 | } |
| 402 | |
| 403 | deleteToolbar() |
| 404 | setTimeout(async () => { |
| 405 | try { |
| 406 | const selection = window |
| 407 | .getSelection() |
| 408 | ?.toString() |
| 409 | .trim() |
| 410 | .replace(/^-+|-+$/g, '') |
| 411 | if (selection) { |
| 412 | console.debug('[content] Text selected via touch:', selection) |
| 413 | const touch = e.changedTouches[0] |
| 414 | toolbarContainer = createElementAtPosition(touch.pageX + 20, touch.pageY + 20) |
| 415 | await createSelectionTools(toolbarContainer, selection) |
| 416 | } else { |
| 417 | console.debug('[content] No text selected on touchend.') |
| 418 | } |
| 419 | } catch (err) { |
| 420 | console.error( |
| 421 | '[content] Error in touchend setTimeout callback for touch selection tools:', |
| 422 | err, |
| 423 | ) |
| 424 | } |
| 425 | }, 0) |
| 426 | } catch (error) { |
| 427 | console.error('[content] Error in touchend listener for touch selection tools:', error) |
| 428 | } |
| 429 | }) |
| 430 | |
| 431 | document.addEventListener('touchstart', (e) => { |
| 432 | try { |
| 433 | if (toolbarContainer?.contains(e.target)) { |
| 434 | console.debug('[content] Touchstart inside toolbar, ignoring.') |
| 435 | return |
| 436 | } |
| 437 | console.debug('[content] Touchstart outside toolbar, removing existing toolbars.') |
| 438 | document.querySelectorAll('.chatgptbox-toolbar-container').forEach((el) => el.remove()) |
| 439 | toolbarContainer = null |
no test coverage detected