Skip to content

ショートカット実行時に iframe 内の選択テキストを使えるようにする #501

Open

Description

@ujiro99

概要

iframe 内で選択したテキストに対して、ショートカットからコマンドを実行できるようにする。

現在は、iframe 内だけでテキストを選択してショートカットを実行すると、選択なしとして扱われる(未選択時の動作の設定に従い、何もしないかクリップボードの値が使われる)。

#499 のレビューで指摘された点。#499 による回帰ではなく、修正前からの制約。

現状

対応方針(案)

A. content script を all_frames: true で全フレームに注入する(非推奨)

  • メニュー・ページアクションなどの UI 一式が広告等の iframe にも注入され、負荷が大きい
  • chrome.tabs.sendMessage が全フレームに届くため、executeAction が複数フレームで実行されるなど、既存の IPC の前提が崩れる

B. ショートカット実行時に chrome.scripting.executeScript で全フレームの選択を取得する(推奨)

const results = await chrome.scripting.executeScript({
  target: { tabId, allFrames: true },
  func: () => ({
    text: window.getSelection()?.toString().trim() ?? "",
    focused: document.hasFocus(),
  }),
})
// フォーカスのあるフレームの選択を使う

検討事項

  • scripting 権限の追加(権限警告は出ない。ホスト権限は既存の http://*/*, https://*/* で足りる)
  • 対象フレームの選び方(document.hasFocus() のフレーム。input / textarea 内の選択も getSelectionText() と同様に扱う)
  • 取得できないフレームの扱い(sandbox 属性付き iframe、about:blank、chrome-extension:// など)
  • ポップアップの表示位置(iframe 内の選択位置は取れないため、画面中央でよいか)
  • 右クリックメニュー経由の実行も対象にするか(chrome.contextMenus.OnClickData.selectionText で取得可能)
  • iframe 内で選択したときにページ内メニューを表示するかどうかは対象外とする(別テーマ)
  • e2e テスト(iframe を含むテストページ)

Generated by Claude Code

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions