コンテキストメニューで選択されたテキストでいくつかの作業を行うことができるはずのクロム拡張機能を書いています。選択したテキストを取得するために使用しているコードは、通常の HTML ページでコードを実行すると、通常のケースで機能します。しかし、拡張機能で同じコードを使用しようとすると、選択したテキストが返されません。選択したテキストを取得するために拡張機能で使用しているコードは次のとおりです。
function myClick2(){
var txt = '';
if (chrome.extension.getBackgroundPage().window.getSelection)
txt = chrome.extension.getBackgroundPage().window.getSelection();
else if (chrome.extension.getBackgroundPage().document.getSelection)
txt = chrome.extension.getBackgroundPage().document.getSelection();
else if (chrome.extension.getBackgroundPage().document.selection)
txt = chrome.extension.getBackgroundPage().document.selection.createRange().text;
console.log("this is the console message");
console.log("txt: " + txt);
}
そして、ここに別のリンクの私のマニフェストと完全な完全なスクリプトファイルがあります: Getting text selection in Chrome extension background page