chrome.tabs.executeScript()
は、スクリプトが実行される各タブ/フレームから「スクリプトの結果」を返します。Array
"スクリプトの結果"は、最後に評価されたステートメントの値です。関数(つまり、return
ステートメントを使用するIIFE)によって返された値になります。一般的には、Web Console(F12)からコード/スクリプトを実行した場合(たとえば、スクリプトvar foo='my result';foo;
の場合は、console.log()
ではなく、実行の結果として表示されるのと同じことになります) results
配列には、文字列 "my result
"が要素として含まれます)。コードが短い場合は、コンソールからコードを実行してみてください。ここで
がanother answer of mineから取られたいくつかのサンプルコードです:
chrome.browserAction.onClicked.addListener(function(tab) {
console.log('Injecting content script(s)');
//On Firefox document.body.textContent is probably more appropriate
chrome.tabs.executeScript(tab.id,{
code: 'document.body.innerText;'
//If you had something somewhat more complex you can use an IIFE:
//code: '(function(){return document.body.innerText;})();'
//If your code was complex, you should store it in a
// separate .js file, which you inject with the file: property.
},receiveText);
});
//tabs.executeScript() returns the results of the executed script
// in an array of results, one entry per frame in which the script
// was injected.
function receiveText(resultsArray){
console.log(resultsArray[0]);
}
これは、ブラウザのアクションボタンがクリックされたときに<body>
の.innerText
を取得するには、コンテンツのスクリプトを挿入します。 activeTab
権限が必要です。
これらが作り出すものの一例として、あなたが返されるかを確認するdocument.body.innerText;
または(function(){return document.body.innerText;})();
でWebページコンソール(F12)とタイプを開くことができます。
[chrome.tabs.executeScript(id、details、callback)について]の可能な複製(http://stackoverflow.com/questions/13166293/about-chrome-tabs-executescript-id-details-callback) – rsanchez
@ rsanchez重複した回答かもしれませんが、この質問にはより良い、より具体的な質問タイトルがあります。もう一つはかなり一般的です。 – RayfenWindspear