2011-01-02 2 views
1

ので、私はChromeの拡張機能で、いくつかのJS変数の値を示す

に拡張モジュールをビルドサイトの背後にある情報が役立ちます見ることができます私が構築拡張で私のウェブサイト2,3 JS変数を取得することが可能です私のサイトを開発する

+0

を君は? ctrl-shift-j – Hemlock

+0

どういう意味ですか?開発者に変数の動的な値のいくつかを伝える必要があるいくつかの人やクライアントがいて、私が望むのはそれらのすべてを表示する拡張子です –

+1

この質問はあまり意味がありません。 「私のウェブサイトを取得する」、「2,3 js変数」、「私が構築した拡張機能」の意味を明確にしてください。英語があなたの最初の言語ではない(と私はそれがないと推測している)場合は、多分あなたの状況を1つの文の代わりに段落または2で説明しようとする? – machineghost

答えて

3

Content Scriptsを使用して)特定のウェブサイトの変数を見ることが可能です。独自のコンテンツスクリプトを挿入し、変数を読み込むスクリプトタグを作成するだけです。これらの変数を使用することはできません。また、Contentスクリプトが行うことのできる制限のために、拡張でその変数を変更することはできません。 Communication with the embedding pageの次のドキュメントを読むことができます。

たとえば、次のようにすると、WebページのJS変数が読み込まれ、その内容がバックグラウンドページに転送され、拡張機能を処理することができます。あなたは、変数が正常に渡されていること、背景ページインスペクタに気づくでしょう:インスペクタがのために働いていません

content_script.js

// JS script injection, so that we can read the JS class 'InternalJSVariable' 
var postFriendMap = function() { 
    var textarea = document.getElementById('transfer-dom-area'); 
    textarea.value = JSON.stringify(InternalJSVariable); 
}; 

// Create a dummy textarea DOM. 
var textarea = document.createElement('textarea'); 
textarea.setAttribute('id', 'transfer-dom-area'); 
textarea.style.display = 'none'; 
document.body.appendChild(textarea); 

// Start injecting the JS script. 
var script = document.createElement('script'); 
script.appendChild(document.createTextNode('(' + postFriendMap + ')();')); 
document.body.appendChild(script); 

// Inform our world that we have received the friend map data. 
chrome.extension.sendRequest({internalVariable: textarea.value}); 

// Clean up since we no longer need this. 
document.body.removeChild(textarea); 

background.html

chrome.extension.onRequest.addListener(function(request, sender, sendResponse) { 
    if (request.internalVariable) { 
    var internal_object = JSON.parse(request.internalVariable); 
    console.log(internal_object); 
    } 
}); 
関連する問題