Skip to content

独自文言入力@AI

最終更新日時: 2025年08月25日 12:57

  • ChatGPT, ClaudeのProseMirrorEditorを入力画面で使っていることに着目し、chrome上でショートカットを定義し、いつも同じ指示文を出せるようにした。
  • ショートカットキー変更機能が完了していない

Chrome仕様変更により、content scriptから直接chrome.storage.localへアクセスできなくなったため、background経由に修正した。

  • content.js内でchrome.storage.local.getをawaitしていた
  • 直近のChromeアップデートにより、セキュリティ制約が強化
  • content scriptがページコンテキストに注入されるため、chrome APIの一部アクセスが制限
  • 特にセキュリティの高いサイト (ChatGPT, Claudeなど) 上で影響発生
  • Alt-1押下時に、コンソールにTypeError発生
  • chrome.storage.localがundefined、もしくはアクセス不能
  • content.js全体の動作が停止し、ショートカット機能も無効化
  • 新たにmessage listenerを追加
    • 受信: {type: 'getShortcuts'}
    • 応答: chrome.storage.local.get(‘shortcuts’) の結果を返却
chrome.runtime.onMessage.addListener((message, sender, sendResponse) => {
if (message.type === 'getShortcuts') {
chrome.storage.local.get('shortcuts', (result) => {
sendResponse(result.shortcuts || {});
});
return true;
}
});
  • 直接chrome.storage.local.getするのを廃止
  • chrome.runtime.sendMessage({type: 'getShortcuts'})でbackgroundに問い合わせる形に変更
  • fetchShortcuts関数を新設し、insertShortcut内部で使用
async function fetchShortcuts() {
return new Promise((resolve) => {
chrome.runtime.sendMessage({ type: 'getShortcuts' }, (response) => {
resolve(response);
});
});
}
const shortcuts = await fetchShortcuts();
const text = shortcuts[shortcutNumber];
  • Manifest V3ではbackground.jsはservice_workerとして動作し、 content scriptとの通信はmessage passingが推奨される Google Developer Documentation
  • chrome.storage APIの直接使用は引き続き許可されているが、context isolationが強いページでは動作保証されない Chrome Extensions Documentation