2012-07-27 17 views
58

クロムエクステンションがあります。私は現在のページのHTMLソースから分析する必要があります。バックグラウンドページとコンテンツスクリプトであらゆる種類のソリューションを見つけましたが、何も私を助けませんでした。
manifest.jsonを::現在のページのソースHTMLをクロムエクステンションから取得する

{ 
    "name": "Extension", 
    "version": "1.0", 
    "description": "Extension", 
    "browser_action": { 
    "default_icon": "bmarkred.ico", 
    "popup": "Test.html" 
    }, 
    "content_scripts": [ 
    { 
     "matches": ["http://*/*"], 
     "js": ["content.js"] 
    } 
    ], 
    "background": { 
    "page": "backgroundPage.html" 
    }, 
    "permissions": [ 
    "cookies", 
    "tabs", 
    "http://*/*", 
    "https://*/*" 
    ] 
} 

background.html:

<html> 
<head> 
<script type="text/javascript"> 
    try { 
     chrome.tabs.getSelected(null, function (tab) { 
      chrome.tabs.sendRequest(tab.id, {action: "getSource"}, function(source) { 
       alert(source); 
      }); 
     }); 
    } 
    catch (ex) { 
     alert(ex); 
    } 
</script> 
</head> 
</html> 

content.js:

chrome.extension.onRequest.addListener(function(request, sender, callback) { 
    if (request.action == "getSource") { 
     callback(document.getElementsByTagName('html')[0].innerHTML); 
    } 
}); 

アラートは常に未定義の警告をここに私はこれまで持っているものです。 content.jsファイル内でコールバック関数を変更しても:

callback('hello'); 

でも同じ結果になります。何が間違っているのですか?多分これを間違った方法で行っています。本当に必要なのはこれです。ユーザーが拡張ポップアップを開くと(そしてそのときだけ)、現在のページのHTMLが必要なので、解析することができます。助言がありますか?

+0

問題(コンテンツのスクリプトが注入される前に)あなたの背景ページ内のコードがすぐに実行されていることです。非常によく似た/重複した質問が以前に尋ねられました。その答えを[新しいGoogle Chromeタブを開いてソースを入手する]で見てください(http://stackoverflow.com/a/10162291/938089?open-a-new-google-chrome-tab-and-get-起源)。 –

+0

あなたの返信に感謝します。私はあなたの添付リンクのコードセグメントをコピーしましたが、それでも動作しません。問題は、私の拡張機能がポップアップであり、ユーザーが自分の拡張機能を開くときだけHTMLを取得する必要があるということです。たとえば、現在のタブがfacebook.comの場合、私の拡張機能を開くときだけ、HTMLソースをjsファイル(コンテンツスクリプトやバックグラウンドページではない)に取得します。 –

+0

質問をあなたの現在のコードで更新してください。コードには、問題を強調するコメントが含まれていなければなりません。 –

答えて

111

manifest.jsonを....あなたはそれ戻っポップアップへのソースとメッセージを取得したいページにスクリプトを挿入

{ 
    "name": "Get pages source", 
    "version": "1.0", 
    "manifest_version": 2, 
    "description": "Get pages source from a popup", 
    "browser_action": { 
    "default_icon": "icon.png", 
    "default_popup": "popup.html" 
    }, 
    "permissions": ["tabs", "<all_urls>"] 
} 

popup.html

<!DOCTYPE html> 
<html style=''> 
<head> 
<script src='popup.js'></script> 
</head> 
<body style="width:400px;"> 
<div id='message'>Injecting Script....</div> 
</body> 
</html> 

popup.js

chrome.runtime.onMessage.addListener(function(request, sender) { 
    if (request.action == "getSource") { 
    message.innerText = request.source; 
    } 
}); 

function onWindowLoad() { 

    var message = document.querySelector('#message'); 

    chrome.tabs.executeScript(null, { 
    file: "getPagesSource.js" 
    }, function() { 
    // If you try and inject into an extensions page or the webstore/NTP you'll get an error 
    if (chrome.runtime.lastError) { 
     message.innerText = 'There was an error injecting script : \n' + chrome.runtime.lastError.message; 
    } 
    }); 

} 

window.onload = onWindowLoad; 

getPagesSource.js

// @author Rob W <http://stackoverflow.com/users/938089/rob-w> 
// Demo: var serialized_html = DOMtoString(document); 

function DOMtoString(document_root) { 
    var html = '', 
     node = document_root.firstChild; 
    while (node) { 
     switch (node.nodeType) { 
     case Node.ELEMENT_NODE: 
      html += node.outerHTML; 
      break; 
     case Node.TEXT_NODE: 
      html += node.nodeValue; 
      break; 
     case Node.CDATA_SECTION_NODE: 
      html += '<![CDATA[' + node.nodeValue + ']]>'; 
      break; 
     case Node.COMMENT_NODE: 
      html += '<!--' + node.nodeValue + '-->'; 
      break; 
     case Node.DOCUMENT_TYPE_NODE: 
      // (X)HTML documents are identified by public identifiers 
      html += "<!DOCTYPE " + node.name + (node.publicId ? ' PUBLIC "' + node.publicId + '"' : '') + (!node.publicId && node.systemId ? ' SYSTEM' : '') + (node.systemId ? ' "' + node.systemId + '"' : '') + '>\n'; 
      break; 
     } 
     node = node.nextSibling; 
    } 
    return html; 
} 

chrome.runtime.sendMessage({ 
    action: "getSource", 
    source: DOMtoString(document) 
}); 
+0

@Gil Tankus最初の投稿に申し訳ありませんが、コメントに十分な注意を払っておらず、Rob Wが言ったことを逆戻りさせました。新しい投稿にはあなたが望むものがあるはずです。 – PAEz

+0

ありがとう、あなたの答えは本当に役立つ、私の問題はonMessageが非同期に発生することです。私のポップアップでは、ソースHTMLを中継する他のものがあります。どうすればソースをグローバル変数に保存してから、ページのロード機能を使用するだけですか? –

+0

私はあなたができるとは思わない。あなたのコールバックコードや関数に入れてコールバックでコールする必要があります.JSに 'goto'コマンドがあった場合はどうでしょうか? ; P – PAEz

関連する問題